# Table layout with tabularx (column widths: 50%|25%|25%)

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.

-
–  mythealias Nov 27 '12 at 0:17
@mythealias Yes, of course I've been there, but haven't found solution (or maybe haven't understood it), so I am asking here. :) –  Edward Ruchevits Nov 27 '12 at 0:22
It is the third example for tabularx, but for some reason they do it in a convoluted manner by combining two columns. Also no harm in asking, I was just being lazy to write an answer :) –  mythealias Nov 27 '12 at 0:34
OK, thank you. I think, reading convoluted code is often useful for general understanding. So, I'll try to figure out example from your link. –  Edward Ruchevits Nov 27 '12 at 0:41
it is actually similar to the answer posted by cmhughes below. only that the space between second and third column is removed and the columns are right and left justified instead of being centered. –  mythealias Nov 27 '12 at 0:55

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.

\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}

\begin{document}

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

-
Great, thanks! That is what I wanted. –  Edward Ruchevits Nov 27 '12 at 0:28
@EdwardRuchevits you're very welcome, happy TeXing :) –  cmhughes Nov 27 '12 at 0:32

Knowing that each column has two "tab separators" and 4/3's of an \arrayrulewidth, you can subtract that from a regular p-column specifier:

\documentclass{article}
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\begin{document}

\noindent
\begin{tabularx}{\textwidth}{| X | X | X |}
\hline
Alpha     & Beta     & Gamma     \\ \hline
0         & 2        & 4         \\ \hline
1         & 3        & 5         \\ \hline
\end{tabularx}

\noindent
\begin{tabular}{
|p{\dimexpr.5\linewidth-2\tabcolsep-1.3333\arrayrulewidth}% column 1
|p{\dimexpr.25\linewidth-2\tabcolsep-1.3333\arrayrulewidth}% column 2
|p{\dimexpr.25\linewidth-2\tabcolsep-1.3333\arrayrulewidth}|% column 3
}
\hline
\centering Alpha     & \centering Beta     & \centering\arraybackslash Gamma     \\ \hline
0         & 2        & 4         \\ \hline
1         & 3        & 5         \\ \hline
\end{tabular}
\end{document}


Centering is obtained using \centering within the required cell (knowing that you're dealing with a p-column, this is possible). The modification of this alignment requires a small correction (via \arraybackslash) to re-establish the use of \\ in a tabular.

-