latex

TabularX - Fullwidth Table with Centered Text. Having issues


I've tried many combinations. I am trying to get the text on this table to be centered in the cells and also keep it the full width of the page. Currently I have this.

            \begin{table}[h]
            \begin{tabularx}{\textwidth}{|l|X|X|X|}
              \hline
              \rowcolor[HTML]{C70F0F}
              % START HEADER 
              \multicolumn{1}{|c}{\cellcolor[HTML]{C70F0F}{\color[HTML]{FFFFFF} \textbf{\#}}} & \multicolumn{1}{|c}{\cellcolor[HTML]{C70F0F}{\color[HTML]{FFFFFF} \textbf{Status}}} & \multicolumn{1}{|c}{\cellcolor[HTML]{C70F0F}{\color[HTML]{FFFFFF} \textbf{Time}}} & \multicolumn{1}{|c|}{\cellcolor[HTML]{C70F0F}{\color[HTML]{FFFFFF} \textbf{Reason}}} \\ \hline
              % END HEADER

              % START Reason
              1 & \textbf{Open} & \today & Client Request \\ \hline
              % END Reason
            \end{tabularx}
        \end{table}

Which results in the following. How can I update this to center the text.

Table


Solution

  • You can define a new column type that combines X and \centering with the array package:

    \documentclass{article}
    \usepackage{tabularx}
    \usepackage[table]{xcolor}
    \usepackage{array}
    \newcolumntype{Y}{>{\centering\arraybackslash}X}
    
    \begin{document}
    
        \begin{table}[h]
        \begin{tabularx}{\textwidth}{|l|Y|Y|Y|}
        \hline
        \rowcolor[HTML]{C70F0F}
        % START HEADER 
        \multicolumn{1}{|c}{\cellcolor[HTML]{C70F0F}{\color[HTML]{FFFFFF} \textbf{\#}}} & \multicolumn{1}{|c}{\cellcolor[HTML]{C70F0F}{\color[HTML]{FFFFFF} \textbf{Status}}} & \multicolumn{1}{|c}{\cellcolor[HTML]{C70F0F}{\color[HTML]{FFFFFF} \textbf{Time}}} & \multicolumn{1}{|c|}{\cellcolor[HTML]{C70F0F}{\color[HTML]{FFFFFF} \textbf{Reason}}} \\ \hline
        % END HEADER
    
        % START Reason
        1 & \textbf{Open} & \today & Client Request \\ \hline
        % END Reason
        \end{tabularx}
        \end{table}
    
    
    
    \end{document}
    

    enter image description here