31
\begin{table}[ht]
\makebox[\textwidth][c]{
    \begin{tabular}{ccccc}
        \hline
        \hline
        [] & [] & [] & [] & []\\
        [] & [] & [] & [] & []\\
        \hline
        \hline
    \end{tabular}
}
\caption{test}
\end{table}

I cannot run the above code, latex returned an error about missing number, but I can't see anything I am missing.

And the weirdest thing is that

\begin{table}[ht]
\makebox[\textwidth][c]{
    \begin{tabular}{ccccc}
        \hline
        \hline
        [] & [] & [] & [] & []\\
        \hline
        \hline
    \end{tabular}
}
\caption{test}
\end{table}

This code works.

Anyone have any idea?

Moriambar
  • 11,466
Benjamin
  • 413

3 Answers3

36

Since \\ allows an optional argument, the [ in the following line is mistaken for it.

......         \\\relax
[whatever] ... \\
egreg
  • 1,121,712
  • Thanks!! I was fighting against this error for half an hour. – Benjamin Nov 11 '11 at 10:48
  • I thought about this myself, and, as a workaround, tried to put a comment % after \. To my surprise, it did not work. Could you enlighten me, why putting \% does not solve this issue? – Matsmath May 21 '21 at 06:17
  • 2
    @Matsmath When \\ is seen, TeX will look whether * follows, gobbling spaces. Then it will look whether [ follows, still gobbling spaces. An endline is converted to a space, but with % it’s not even seen. Worse, in amsmath environments, the space gobbling fearure is disabled, so \\<newline>[ works, but ``\%[` wouldn’t. – egreg May 21 '21 at 08:40
  • \relax did not work for me. Even without square brackets. Making curly braces, as described in the other answer, worked. – John Sep 28 '23 at 09:36
  • @John Sorry, but it must work. Can you give an example? – egreg Sep 28 '23 at 10:16
  • Yeah it does not fit exactly. I found one case. When you put a \relax after the newline before a \midrule, it crashes maybe in the same way as when you do not have a newline before a \midrule. However, the \relax is not necessary at this location for the square bracket. For the other locations the crashes might have been caused by another person, who edited something else simultaneously (on overleaf). Sorry for the noise. – John Sep 28 '23 at 11:24
13

Another option is to add brackets around, like this:

{[} .... {]}
  • 3
    ...or add the braces before the next line, as in {} [...]. In this case, either would work, but in some math situations, the spacing could come out differently. – Steven B. Segletes May 31 '18 at 10:09
0

I recently encountered the same problem, but I (accidentally) found a simple solution. Although this code doesn't work:

\begin{table}[ht]
\makebox[\textwidth][c]{
    \begin{tabular}{ccccc}
        \hline
        \hline
        [] & [] & [] & [] & []\\
        [] & [] & [] & [] & []\\
        \hline
        \hline
    \end{tabular}
}
\caption{test}
\end{table}

This code works, just by adding an extra empty line in between the rows:

\begin{table}[ht]
\makebox[\textwidth][c]{
    \begin{tabular}{ccccc}
        \hline
        \hline
        [] & [] & [] & [] & []\\
    [] &amp; [] &amp; [] &amp; [] &amp; []\\
    \hline
    \hline
\end{tabular}

} \caption{test} \end{table}

  • The problem was described in @egreg 's answer, but this answer is also a possible elegant solution. – Elad Den Sep 12 '22 at 13:09
  • 1
    Although a blank line works in this situation, random blank lines are not, in general, a good idea, since in invoking the \par machinery, they reset a lot of other things that may cause surprising results. – barbara beeton Sep 12 '22 at 18:06