jump to navigation

Is this a LaTeX bug? March 11, 2010

Posted by David Speyer in Latex.

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.


1. Jim Humphreys - March 11, 2010

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. Ben Webster - March 11, 2010

I have no idea, but can verify the same thing has happened to me many times.

3. Ben Webster - March 11, 2010

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.

4. Dylan Thurston - March 11, 2010

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

5. David Speyer - March 11, 2010

Oh, I see. I had forgotten that square brackets, unlike curly, don’t need to be escaped in normal text. Thanks!

6. Carsten S - March 11, 2010

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

7. Carsten S - March 11, 2010

I wasn’t fast enough…

8. D. Eppstein - March 11, 2010

Wrap it in another pair of brackets:

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

9. Ben Webster - March 11, 2010

You mean braces….

10. Jim Humphreys - March 12, 2010

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.

11. Maarten Bergvelt - March 12, 2010

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?

12. David Speyer - March 12, 2010

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

13. Maarten Bergvelt - March 12, 2010


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

14. Pasha Zusmanovich - March 12, 2010

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}}]

15. Noah Snyder - March 12, 2010

People coming late to the thread: Dylan answered the question in comment #4.

16. Rasmus B - March 14, 2010


\cite{Gauss}*{Theorem 1}

instead of

\cite[Theorem 1]{Gauss}.

17. D. Mandal - December 16, 2011

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

Sorry comments are closed for this entry


Get every new post delivered to your Inbox.

Join 769 other followers

%d bloggers like this: