16

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"?

3 Answers 3

17

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.

4
  • 2
    Note that if you do not put the tabularx in a table environment and just use it in text, prepend it with \notindent to avoid it sticking out the right margin by \parindent.
    – Werner
    Nov 21, 2011 at 3:15
  • I think that I may be having the problem you describe in your comment. The right column of my table is extending too far to the right, beyond the text column (I'm using a two-column article). Where do I put \notindent? Or presumably, \noindent... Mar 31, 2012 at 23:30
  • @NelsBeckman if you're not using the table environment, then you could use \noindent\begin{tabular}...
    – cmhughes
    Apr 1, 2012 at 19:32
  • This inserts itself at the top of my document.
    – user132925
    Aug 31, 2018 at 13:05
6

You can also do this without any additional packages, by simply using the tabular* environment, and add \extracolsep{\fill} to fill the column. Here is an example of before and after (I also removed the left column spacing in column 1, and the right column spacing after column 2) where the geometry package was used to include the frame:

enter image description here

Custom Column Type:

If you use this type of column often, you can use the array package and define a custom column type as I have done for the last table. So, with this just using L for your column will achieve what you want. This produces results identical to the second example in the above picture.

Code:

\documentclass{article}
\usepackage[showframe]{geometry}

\usepackage{array}% Only needed if you want to be able to use the `L` column type
\newcolumntype{L}{@{}l@{\extracolsep{\fill}}}% Needs `array` package

\newcommand*{\Text}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam porta tincidunt velit, at lacinia ipsum dictum eget.}

\begin{document}\vspace*{0.1cm}

\noindent
\begin{tabular}{l p{5cm} }\hline
abc & \Text\\\hline
\end{tabular}

\bigskip
\noindent
\begin{tabular*}{\linewidth}{@{}l@{\extracolsep{\fill}} p{5cm}@{}}\hline
abc & \Text\\\hline
\end{tabular*}

\bigskip
\noindent
\begin{tabular*}{\linewidth}{L p{5cm}@{}}\hline% Using the L column type
abc & \Text\\\hline
\end{tabular*}
\end{document}
4
\documentclass{article}
\usepackage{tabularx,lipsum}
\begin{document}

\noindent
\begin{tabularx}{\textwidth}{@{} l X @{} }
    as long as its contents & \lipsum[1]
\end{tabularx}

\lipsum[1]
\end{document}

The left and right columns are algned with the text

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .