Representation theory, geometry and whatever else we decide is worth writing about today.

Menu

Is this a LaTeX bug?

I just discovered the following behavior, which seems counterintuitive to me. I’d like to claim I actually found a bug in LaTeX, but that seems unlikely. Still, I’m curious whether someone can explain the logic behind this.

Try to compile the following\begin{Theorem}[ \cite[Theorem 1]{Gauss} ]
Every integer is the sum of three triangular numbers.
\end{Theorem}

That doesn’t make sense to me. The parentheses matching is completely unambiguous; the entire string “\cite[Theorem 1]{Gauss}” must be the optional argument of the Theorem environment. Why can’t LaTeX parse this?

Post navigation

17 thoughts on “Is this a LaTeX bug?”

Speaking as a nonexpert, I can see a potential problem
in placing square brackets around the \cite …. (which
normally produces its own square brackets). But I’ve
never tried to put a citation in this position, so would have
to experiment further with it. Somehow you’ve confused
the program.

The problem has to do with how square brackets are parsed. A square bracket in ordinary text produces a square bracket in the output; it’s not a special character. It’s only in some specific contexts (as in those above) that it becomes a delimiter. But then this causes problems with parsing nested brackets. How is LaTeX supposed to distinguish (for instance)

\begin{Theorem}[Ga[uss] …

where the bracket is just an ordinary character, and should not be matched, with the type of example you gave above?

(Probably you’ll argue that my silly example should not be accepted, but I think it’s still non-trivial to get that behaviour.)

It’s been a couple of decades since I looked into the TeXbook, so I do not know what exactly is going on. And anyway you couldn’t from just reading the TeXbook, you would also have to know something about the implementation of LaTeX. So believe nothing that I am going to tell you.

TeX does not know optional arguments. The square brackets mean nothing to it. So the \begin command has look ahead to see if after its real argument there is a `[‘ and then do the parsing until the `]’ itself. All of this is messy and can go wrong. Apparently here it sees the wrong `]’, which in turn means that it will nor be handed to the \cite command, which is why that command complains that its argument terminates unexpectedly.

Probably the LaTeX documentation mentions this problem somewhere, which would mean that it is not a bug ;-)

I want to know if I use subfig package then is the package graphic not required?

How to solve the following errors
Error: Paragraph ended before \@citex was complete.\@citex was complete

Comments are closed.

Secret Blogging Seminar

A group blog by 8 recent Berkeley mathematics Ph.D.'s. Commentary on our own research, other mathematics pursuits, and whatever else we feel like writing about on any given day. Sort of like a seminar, but with (even) more rude commentary from the audience.