Table layout with tabularx (column widths: 50%|25%|25%)
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}
\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}
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
.
An alternative is to use the tabu
environment. You can give "weights" (width coefficients) to X
columns (X[2]
):
\documentclass{article}
\usepackage{tabu}
\begin{document}
\begin{table}[htbp]
\centering
\begin{tabu} to \textwidth {| X[2] | X | X |}
\hline
\centering Alpha & \centering Beta & \centering Gamma \\ \hline
0 & 2 & 4 \\ \hline
1 & 3 & 5 \\ \hline
\end{tabu}
\end{table}
\end{document}