[Tex/LaTex] Table layout with tabularx (column widths: 50%|25%|25%)

tablestabularx

This code produces a table with three centered column:

\begin{table}[htbp]
    \centering
    \begin{tabularx}{\textwidth}{| c | c | c |}
        \hline
        Alpha     & Beta     & Gamma     \\ \hline
        0         & 2        & 4         \\ \hline
        1         & 3        & 5         \\ \hline
    \end{tabularx}
\end{table}

This code produces a full-width table with columns of equal width:

\begin{table}[htbp]
    \centering
    \begin{tabularx}{\textwidth}{| X | X | X |}
        \hline
        Alpha     & Beta     & Gamma     \\ \hline
        0         & 2        & 4         \\ \hline
        1         & 3        & 5         \\ \hline
    \end{tabularx}
\end{table}

I can't get how to produce full-width table, with centered header (~ first row) and columns of width |50%|25%|25%| respectively?

Would be nice if I could define these styles somewhere at the beginning of the document, so that all further tables will be able to use it.

Best Answer

The code below defines two new columntypes: b for 'big' and s for 'small'

I followed the details of section 4.3 of the documentation to create these.

screenshot

\documentclass{article}
\usepackage{tabularx}

\newcolumntype{b}{X}
\newcolumntype{s}{>{\hsize=.5\hsize}X}

\begin{document}

\begin{table}[htbp]
    \centering
    %\begin{tabularx}{\textwidth}{| X | X | X |}
    \begin{tabularx}{\textwidth}{bss}
        \hline
        Alpha     & Beta     & Gamma     \\ \hline
        0         & 2        & 4         \\ \hline
        1         & 3        & 5         \\ \hline
    \end{tabularx}
\end{table}
\end{document}

If you'd like to center the heading, then you can use something like the following, which defines a \heading command to center its argument

\documentclass{article}
\usepackage{tabularx}

\newcolumntype{b}{X}
\newcolumntype{s}{>{\hsize=.5\hsize}X}
\newcommand{\heading}[1]{\multicolumn{1}{c}{#1}}

\begin{document}

\begin{table}[htbp]
    \centering
    %\begin{tabularx}{\textwidth}{| X | X | X |}
    \begin{tabularx}{\textwidth}{bss}
        \hline
        \heading{Alpha}     & \heading{Beta}     & \heading{Gamma}     \\ \hline
        0         & 2        & 4         \\ \hline
        1         & 3        & 5         \\ \hline
    \end{tabularx}
\end{table}
\end{document}
Related Question