66

I've put in the following code:

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

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.

4
  • 6
    Use p{4cm}p{5cm} not l{4cm}... It will be left aligned still then, but text will wrap around too. Please note that the width of the table is slightly larger than 9cm then
    – user31729
    Jan 10, 2016 at 15:43
  • Have you seen any documentation that suggests l{4cm} should work? If you provide a link to it we could try to get it fixed. Jan 10, 2016 at 15:50
  • @DavidCarlisle sharelatex.com/learn/Tables says m{5em}. It doesn't say what m does but I wanted my text left aligned. Anyway, it didn't work even with m instead of l. Jan 10, 2016 at 16:04
  • 1
    m is vertical mid alignment and requires the array package. Jan 10, 2016 at 16:55

2 Answers 2

96

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.

\documentclass{article}



\begin{document}

\begin{tabular}{|p{4cm}|p{5cm}|}
\hline
foo & bar
\end{tabular}

\begin{tabular}{|@{}p{4cm}@{}|@{}p{5cm}@{}|}
\hline
foo & bar
\end{tabular}


{%
 \renewcommand{\arrayrulewidth}{0pt}%
 \begin{tabular}{@{}p{4cm}@{}@{}p{5cm}@{}}
   foo & bar
 \end{tabular}
}

\end{document}
2
  • 1
    In the last paragraph, it should be 4cm + 6cm given {|p{4cm}|p{6cm}|}
    – mloning
    Nov 25, 2020 at 17:27
  • How to put bottom line of the table (\hline), an error is coming in beamer environment (error msg: Misplaced \noalign. ^^I^^I\hline) May 23, 2022 at 15:42
30

This workaround in the preamble solves the problem. You can simply use C{width} for centered entries. Replace C with L or R if raggedright or raggedleft is favoured.

\usepackage{array}
\newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp}
\newcolumntype{C}[1]{>{\PreserveBackslash\centering}p{#1}}
\newcolumntype{R}[1]{>{\PreserveBackslash\raggedleft}p{#1}}
\newcolumntype{L}[1]{>{\PreserveBackslash\raggedright}p{#1}}

You must log in to answer this question.

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