10

Is it somehow possible to put a citation including page specification, as in \cite[page]{book}, inside the list-of-figures-relevant caption of a figure, i.e. \caption[*HERE*]{...}?

I can do

\caption[\protect\cite{book}]{other text}

but

\caption[\protect\cite[page]{book}]{other text}

yields book] other text as the caption in the first run and produces an error "Argument of \NAT@@cite has an extra }" in the second LaTeX run.

I'm using pdfLaTeX and natbib.

4
  • 7
    LaTeX mistakes the bracket after page for the closing bracket of the optional argument; \caption[{\protect\cite[page]{book}}]{other text}
    – egreg
    Commented Feb 28, 2012 at 23:15
  • Oh wow, thanks, that was quick and easy! Could've thought of that myself... Care to post that as an answer so that I can accept it? Commented Feb 28, 2012 at 23:20
  • note that citations in lists-of-*, toc, etc., play havoc with unsrt-type biblio styles. i think there's a faq answer about that... Commented Feb 28, 2012 at 23:53
  • Side note (if anyone come across this later, question is old) \cite is already robust, so you don't need \protect.
    – user202729
    Commented Apr 1, 2023 at 12:31

1 Answer 1

19

The scanning of an optional argument (in brackets, [ and ]) works in a slight different way than the scanning of a mandatory argument (in braces, { and }); TeX counts the braces, but doesn't do the same for brackets. So, with your input, the optional argument that is seen is

\protect\cite[page

that is, everything from [ to the first ] (which is balanced with respect to braces). In order to avoid this problem, brackets inside an optional argument must be "masked" with braces:

\caption[{\protect\cite[page]{book}}]{other text}

The argument cannot have unbalanced braces, so the bracket after page doesn't end it, in this case.

3
  • Nice, thanks! Out of curiosity: Do you know the reasoning behind the different behavior for optional vs mandatory arguments / [] vs {}? Commented Feb 28, 2012 at 23:41
  • 4
    The balancing of braces is built in in TeX; optional arguments in LaTeX are treated in a rather different way.
    – egreg
    Commented Feb 28, 2012 at 23:45
  • 1
    @ egreg, I want to congratulate you for such a complete answer! Even though it seems something simple, this is the kind of question to be expected in Tex.SX
    – henrique
    Commented Feb 29, 2012 at 1:07

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