[Tex/LaTex] Insert a reference in a caption

captionscross-referencing

After visiting wiki on labels and cross-referencing, a couple of tex sites (1,2), and a stackexchange solution, the following reference will not appear in the caption. I'm using pdflatex on ubuntu.

The section label that is being referenced (appen) is defined at the end of the doc. All of the following attempts to insert the reference in the caption produce ?? or [?]:

\begin{figure}[!ht]
\caption*{See the ~\ref{appen} for language abbreviations.}
\makebox[\textwidth]{\includegraphics[width=100mm]{{/filepath/hourly.update}.pdf}}
\end{figure}

\begin{figure}[!ht]
\caption*{See the ~\cite{appen} for language abbreviations.}
\makebox[\textwidth]{\includegraphics[width=100mm]{{/filepath/hourly.update}.pdf}}
\end{figure}

\begin{figure}[!ht]
\caption*{See the ~\protect\ref{appen} for language abbreviations.}
\makebox[\textwidth]{\includegraphics[width=100mm]{{/filepath/hourly.update}.pdf}}
\end{figure}

\begin{figure}[!ht]
\caption*{See the ~\protect\cite{appen} for language abbreviations.}
\makebox[\textwidth]{\includegraphics[width=100mm]{{/filepath/hourly.update}.pdf}}
\end{figure}

\section{Appendix}
\label{appen}
\subsection{Language Abbreviations}
 ...words, figures, tables, and such.


edit 2013-02-12

Error received:

LaTeX Warning: Reference `appen' on page 2 undefined on input line 94

Any idea how to insert a reference within a caption?

Best Answer

The first one (using \ref{appen}) will give you, after two compilations, the expected result. The ones using \cite (which is to be used for bibliographical items) are incorrect. The one using \protect\ref{appen} will also yield the correct result, but \protect is not necessary. An example (notice that leaving a blank space before ~ will produce a superfluous space):

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}

\begin{document}

\begin{figure}[!ht]
\caption*{See the Section~\ref{appen} for language abbreviations.}
\makebox[\textwidth]{\includegraphics[width=100mm]{{/filepath/hourly.update}.pdf}}
\end{figure}

\section{Appendix}
\label{appen}

\end{document}

enter image description here

The general mechanism for cross-referencing is to place

\label{<key>}

after an anchor has been generated (typically, after some automatic numbering has been done), and to use

\ref{<key>}

to cross-reference the labelled object.

The demo option for graphicx simply replaces actual figures with black rectangles; do not use that option in your actual document.

Related Question