[Tex/LaTex] Table column width “fill to margin”

horizontal alignmenttables

I kind of would expect tables to be able to "fill to margin", instead of choosing a number and iteratively rebuilding the document until it is the right size.

For example, I want this 2-column table to have the first column as wide as it needs to be, and the 2nd column to "fill to margin":

\begin{tabular}{ l p{5cm} }

Of course, that's not gonna do it (2nd column will be 5cm), is there anyway to "fill to margin"?

Best Answer

The tabularx package provides the tabularx environment, which was designed for just this purpose.

Here is a MWE:

\documentclass{article}
\usepackage{tabularx}

\begin{document}

\begin{table}
 \begin{tabularx}{\textwidth}{XX}
    foo & bar
\end{tabularx}
\caption{My table}
\end{table}

\end{document}  

Note that tabularx's first argument is the width of the table. In the above, I have chosen \textwidth; when combined with the X columntype (defined in the tabularx package), it produces the 'fill-to-margin' effect that you want.

Related Question