[Tex/LaTex] How to create a table with fixed column widths


I've put in the following code:

\begin{tabular} {l{4cm} l{5cm}}
hello & foo\\
hello & bar\\

and it gives me an error:

! LaTeX Error: Illegal character in array arg.

See the LaTeX manual or LaTeX Companion for explanation. Type H
for immediate help. …

                                               l.199 \begin{tabular} {l{4cm} l{5cm}}

Why doesn't it work? I'm trying to make a table with left justified text with fixed cell widths. If I just specify {l l} it works, but with the widths it gives me that error.

Best Answer

The standard column types are l, c, r and p which stands for left aligned, centered or right alignment and parbox.

Only the p - type can have a width argument, i.e. p{somewidth}, where width can be specified with any dimension TeX/LaTeX knows of.

The p - type is effectively \parbox which allows wrapping of text and content, which isn't possible (directly) in a l etc. type, for example an itemize or enumerate list.

The array package provides for example for the m type which is meant for vertical ('middle') aligned. In addition to this, array has the \newcolumntype macro which allows to define other column types, however, based on the existing 'base' types, i.e. r, l, c or p.

The tabularx package provides for the X columntype and the siunitx enables to use the S columntype.

Here's a small sample code for showing the width effects:

\begin{tabular}{|p{4cm}|p{6cm}|} will provide a tabular, that has the width of 4cm + 5cm + 4\tabcolsep + 3\arrayrulewidth, since each column has a left and right \tabcolsep space, the later defaulting to 6pt; and there are three |, so 3\arrayrulewidth lengths are added.

In total, even p{4cm}p{5cm} will be wider than the expected 9cm.



foo & bar

foo & bar

   foo & bar

Related Question