Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 8 down vote accepted

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.

share|improve this answer
1  
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 '11 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... –  Nels Beckman Mar 31 '12 at 23:30
    
@NelsBeckman if you're not using the table environment, then you could use \noindent\begin{tabular}... –  cmhughes Apr 1 '12 at 19:32

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}
share|improve this answer
\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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.