22

Whenever I try to compile a table that has a "[" as first character in a row it gives me the error:

Missing number, treated as zero.
Illegal unit of measure

Example:

\begin{table}[htb]
    \centering
    \begin{tabular}{r|r}
        [some text ] & ... \\
        [some more text ] & ...
    \end{tabular}
    \caption{...}
    \label{tab:...}
\end{table}

How can I print "[" as first character in a row?

0

3 Answers 3

13

No error for me.

Anyway, you can protect with braces { }.

 {[some text]}

edit: as suggested by @tohecz here is the right solution

7
  • Yes, this works. Ty
    – Mq_
    Commented Jan 24, 2013 at 19:07
  • 4
    Put \relax after \\, it's better.
    – egreg
    Commented Jan 24, 2013 at 19:23
  • 2
    @Sigur The original example doesn't show the problem. It's for a [ after \\ that the problem shows. It is cured simply by saying \\ \relax
    – egreg
    Commented Jan 24, 2013 at 20:56
  • 4
    @Sigur Of course you don't need \\ to terminate the last line unless you have an \hline after it. But the \relax is needed only if a square bracket follows, instead of your braces
    – egreg
    Commented Jan 24, 2013 at 21:00
  • 2
    Please remember that the solution with {} is wrong in math mode (array and friends), as shown here: tex.stackexchange.com/questions/86385/…
    – yo'
    Commented Jan 26, 2013 at 16:20
25

Here is a MWE that illustrates your problem:

\documentclass{article}
\begin{document}
\begin{table}
    \begin{tabular}{r|r}
        [some text ] & ... \\
        [more text ] & ... \\
    \end{tabular}
\end{table}
\end{document}

The issue is that \\[1 in] is used at the end of a line to skip 1in (for instance) of additional vertical space. So \\ is a macro and [1in] is an optional argument to that macro.

When TeX reads \\, then newline, then [, it ignores the newline (as it normally does with all whitespace after a macro) and proceeds as if it were expanding \\[more text ]. But this clearly doesn't make any sense since more text is not a length, in particular it doesn't start with a number. Hence the error: Missing number, treated as zero. Illegal unit of measure.

It wasn't reproduced in your original MWE because you had only one line with bracketed text at the beginning and \\ at the end. It happens between the end of one line and the beginning of another.

As Sigur suggests, you can keep TeX from expanding this way by putting braces around the bracketed text. TeX will expand \\{[more text ]} the expected way since it doesn't "look" like an optional argument is being used anymore. egreg's suggestion of \\\relax will similarly inhibit the macro processor from reading the bracketed text as an optional argument.

3
  • 1
    I think it's much easier to remember to use {} (an idiom that solves some other problems too) than to remember \relax. But it's nice to know both. Commented Jan 24, 2013 at 23:16
  • 2
    @EthanBolker It's better to use \relax It doesn't make any difference here but if you are using array rather than tabular or a math column in a tabular with >{$} or similar {} can alter the spacing whereas \relax will not. Commented Jan 26, 2013 at 15:19
  • Please remember that the solution with {} is wrong in math mode (array and friends), as shown here: tex.stackexchange.com/questions/86385/…
    – yo'
    Commented Jan 26, 2013 at 16:21
10

The problem is, as Matthew Leingang explains, that \\ looks for a following bracket for a vertical spacing specification.

The best solution, which works in all cases, be they in tabular, array and even eqnarray (which shouldn't be used anyway), is to add \relax after \\, when a [ follows:

\documentclass{article}
\begin{document}
\begin{table}
    \begin{tabular}{r|r}
        [some text] & ... \\ \relax
        [more text] & ... \\ 
    \end{tabular}
\end{table}
\end{document}

enter image description here

It's a bit of a nuisance, but the case shouldn't be so frequent.

Note that amsmath environments don't suffer from this problem:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align*}
v&=\begin{bmatrix}
  a\\
  [b]\\
  c
\end{bmatrix}\\
[w]&=x
\end{align*}
\end{document}

enter image description here

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