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.

You’ll get
Runaway argument?
Theorem 1)
! Paragraph ended before \@citex was complete.
l.8 \begin{Theorem}[ \cite[Theorem 1]
{Gauss} ]

Putting the citation in curly brackets fixes the problem.

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?

Details: Document class amsart, pdfTeX, Version 3.1415926-1.40.10, LaTeX2e.

17 thoughts on “Is this a LaTeX bug?

  1. 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.

  2. Jim- The weird thing is (at least in my experience) if the optional argument on \cite is left out and is just a bare citation, it compiles fine.

  3. It’s not really a fixable bug.

    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.)

  4. 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 ;-)

  5. Wrap it in another pair of brackets:

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

  6. If you really need a square bracket where it might cause
    parsing problems, it’s apparently OK to surround it with
    braces as in {[} or {]}

    Aside from this, a citation to the theorem’s originator
    could always be placed just before the theorem is stated.
    Unlike placing a name like [Gauss] to get boldface
    (Gauss) printed just after Theorem.

  7. When you ask a question like this it is a good idea to give a complete minimal example. I don’t know where you got the Theorem environment from:

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

    gives an error message:
    ERROR: LaTeX Error: Environment Theorem undefined.

    So what did you do, exactly?

  8. The minimal example is on my laptop at home, so I can’t post it right now. But it is basically your example plus \newtheorem*{Theorem}{Theorem}
    and perhaps \usepackage{amsthm}.

  9. Right,

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

    works, or rather doesn’t work in the way you describe.

    To get an explanation why LaTeX behaves like this I would try the news group comp.text.tex, where the friendly experts hang out.

  10. While I cannot offer a logical explanation (and this seems to me a wrong behavior too), I can offer a remedy:

    \begin{Theorem}[\protect{\cite[Theorem 1]{Gauss}}]

  11. 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.