[comp.text.tex] LaTeX question: \ref in \caption

marcus@ecrc.de (Marcus Clemente) (06/03/91)

Hello,

I want to refer to a figure in the caption of another figure. LaTeX runs fine
on the first run, but crashes the second run. Is there any LaTeX specialist
being able to tell me how to do it?

Following is a sample code as well as the output of TeX:

%%%%%%%%%%%%  try.tex %%%%%%%%%%%

\documentstyle{article}
\begin{document}

Here is the first figure

\begin{figure}[htp]
an example
\caption{example}
\label{example}
\end{figure}

and this is the second figure with the reference

\begin{figure}[htp]
another example
\caption{another example of figure \ref{example}}
\label{another}
\end{figure}

\end{document}


%%%%%%%%%%% TeX run %%%%%%%%%%%

europa1( ~/tmp ) 8 -> latex try.tex
This is TeX, C Version 2.95 (no format preloaded)
(try.tex
LaTeX Version 2.09 <4 Aug 1988>
(/usr/local/lib/tex/inputs/article.sty
Document Style `article' <16 Mar 88>.
(/usr/local/lib/tex/inputs/art10.sty))
No file try.aux.
LaTeX Warning: Reference `example' on page 1 undefined.
[1] (try.aux)
LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.
Output written on try.dvi (1 page, 496 bytes).
Transcript written on try.log.
europa1( ~/tmp ) 9 -> latex try.tex
This is TeX, C Version 2.95 (no format preloaded)
(try.tex
LaTeX Version 2.09 <4 Aug 1988>
(/usr/local/lib/tex/inputs/article.sty
Document Style `article' <16 Mar 88>.
(/usr/local/lib/tex/inputs/art10.sty)) (try.aux)
! Argument of \@caption has an extra }.
<inserted text>
                \par
<to be read again>
                   }
\addcontentsline ...\protect \contentsline {#2}{#3
                                                  }{\the \@temptokena }}}}\@...

\@caption ... the#1\endcsname }{\ignorespaces #2}}
                                                  \begingroup \@parboxrestor...
l.19 ...n{another example of figure \ref{example}}

? h
I've run across a `}' that doesn't seem to match anything.
For example, `\def\a#1{...}' and `\a}' would produce
this error. If you simply proceed now, the `\par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your `}' was spurious, just type `2' and it will go away.

?

--
Marcus Clemente                          Internet: marcus@ecrc.de
ECRC GmbH     
Arabella Str. 17
D 8000 Munich 81                         Tel.:  +49/089/92699-109
Germany 

------------------------------------------------------------------
The difference between practice and theory in practice is always
greater than the difference between practice and theory in theory.

krab@iesd.auc.dk (Kresten Krab Thorup) (06/05/91)

>>>>> On 3 Jun 91 15:03:30 GMT, marcus@ecrc.de (Marcus Clemente) said:

     I want to refer to a figure in the caption of another figure. LaTeX
     runs fine on the first run, but crashes the second run. Is there any
     LaTeX specialist being able to tell me how to do it?
     
     Following is a sample code as well as the output of TeX:
     
     :
     :

     \caption{another example of figure \ref{example}}


As the LaTeX manual points out, `\ref' is a so-called `fragile'
command, that must be `protected' when used in text that is written to
one of the auxillary files .aux, .toc, etc.  So. you should have
written:

     \caption{another example of figure \protect\ref{example}}


The problem is, that the macro `\ref' is expanded to early. The
`\protect' ensures, that it is not expanded until the actual
typesetting is taking place.  

/Kresten

jhgillespie@ucdavis.edu (06/05/91)

In article <1991Jun3.150330.21344@ecrc.de> marcus@ecrc.de (Marcus Clemente)
writes:
>Hello,
>
>I want to refer to a figure in the caption of another figure.>\caption{another
example of figure \ref{example}}

Protect it: \protect\ref{example}