5

If we want to put an array in the caption of some table environment, we cannot do this (which causes an error)

\caption{A matrix here: $\begin{array}{lr} a & b \\ c & d \end{array}$}

Instead, we can put an array in the caption of some table environment as

\caption[]{A matrix here: $\begin{array}{lr} a & b \\ c & d \end{array}$}

which works with no error.

Question: What is the reason behind why putting [] in the \caption[] works? Thanks!

5
  • 3
    With a recent LaTeX, the first version should work. If you're using an older LaTeX, try \protect\begin... and \protect\end... (and maybe \protect\\...). Mar 15, 2021 at 14:46
  • thanks very much for the comments +1 (so I am using the old LaTex? TexShop Version 4.44 (4.44))
    – wonderich
    Mar 15, 2021 at 14:47
  • TeXShop is only an editor. What is your LaTeX version?
    – Matthias
    Mar 15, 2021 at 14:48
  • The version of TeXShop doesn't matter much. Open the .log file (if your .tex file is main.tex, open main.log) in a text editor and show me the first 10 lines or so. Mar 15, 2021 at 14:49
  • 2
    A possible reason why the version with [] works is when you have a List of Tables in your document. In that case the table caption needs to be written to this list, which is problematic for complex captions (for example containing an array environment). The optional argument between [] is used to provide an alternative (usually shorter) caption for the List of Tables, i.e., \caption[short title]{longer title}. If the optional caption is provided then the complex caption does not need to be moved around and therefore does not cause any more trouble.
    – Marijn
    Mar 15, 2021 at 15:00

1 Answer 1

10

If no optional argument is used the main caption text moves into the list of tables. And this moving is dangerous: the text has to be written into a file and some commands break then. It happens nowadays less often than years ago as many commands has been made robust but it still happens. It then often helps to protect such a command. See also https://tex.stackexchange.com/a/585716/2388

\documentclass{article}

\begin{document}

\listoftables

\begin{table}
\caption[opt]{main1}
\end{table}

\begin{table}
\caption{main2}
\end{table}

\end{document}

enter image description here

You must log in to answer this question.

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