Frequently Asked Question List for TeX
This is a slightly different take on the problem addressed in “fixed-width tables“ — here we have a column whose size we can’t absolutely predict when we design the document.
While the basic techniques (the tabularx
, tabulary
and ltxtable
packages) are the same for this problem as for the
fixed-width table problem, there’s one extra tool that we can
call to our aid, which may be preferable in some situations.
Suppose we have data in one column which we read from an external
source, and the source itself isn’t entirely predictable. The data in
the column may end up pretty narrow in every row of the table, or it
may be wide enough that the table would run over the edge of the page;
however, we don’t want to make the column as wide as possible “just
in case”, by defining a fixed size for the table. We would like the
column to be as small as possible, but have the possibility to spread
to a maximum width and (if even that width is exceeded) turn into a
p
-style column.
The varwidth
package, discussed in
“automatic sizing of minipages”, provides
a solution. If you load it together with the LaTeX “required”
array
package, i.e.:
\usepackage{array}
\usepackage{varwidth}
varwidth
defines a new column-type V
, which you
can use as follows:
\begin{tabular}{l V{3.5cm} r}
foo & blah & bar \\
foo & blah blah & bar \\
\end{tabular}
when the second column ends up less than 3.5cm wide; or you can use it as follows:
\begin{tabular}{l V{3.5cm} r}
foo & blah & bar \\
foo & blah blah & bar \\
foo & blah blah blah blah blah blah
& bar \\
\end{tabular}
where the second column will end up noticeably wider, and will wrap to a second line in the third row.
FAQ ID: Q-varwidcol
Tags: tables–figures