LaTeX figure captions

Last week I learned something the hard way about LaTeX captions and thought I should write about it before I forget. I don’t want to learn it the hard way again.

I write reports for work in Markdown, then convert them to LaTeX via MultiMarkdown and Sablotron using a version of Fletcher Penney’s XHTML-to-LaTeX XSLT script that I customized to fit the LaTeX packages I like to use. Normally, all the formatting comes out fine and I don’t have to dig into the LaTeX file at all; I just run it through pdflatex and print or email the resulting PDF to my client.

Last week, though, I had some long skinny plots that needed to go in a report, and they fit best when turned 90°. I learned about the rotating package, which provides the nice sidewaysfigure environment—just what I needed. All I had to do was go into the LaTeX file, change the figure environments to sidewaysfigure environments, adjust the figure widths, and the file would be ready for processing.

Well, almost. The code I first used looked like this

Reference to \autoref{fall-elevations} in the middle of all this.

\begin{sidewaysfigure}
\begin{center}
\includegraphics[width=9in]{all-elevations.pdf}
\end{center}
\caption{Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}
\label{fall-elevations}
\end{sidewaysfigure}

and the PDF came out looking like this

The figure was sideways, all right, but the caption was just way too wide. No problem, I’ll just put the caption in a parbox and add some space in front of it to center it under the figure:

Reference to \autoref{fall-elevations} in the middle of all this.

\begin{sidewaysfigure}
\begin{center}
\includegraphics[width=9in]{all-elevations.pdf}
\end{center}
\hspace{1.5in}\parbox{6in}{\caption{Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}}
\label{fall-elevations}
\end{sidewaysfigure}

This gave me the caption format I wanted

but the reference to the figure in the text was screwed up. Instead of the autoref call yielding “Figure 1” in the text, I was getting “Figure 2.”

After yelling at the computer for a while, I realized that the only place the “2” could have been coming from was the section number. A bit of Googling led me to this hint in the LaTeX wikibook, which says

A label may sometimes pick up the section or list number instead of the figure number. In this case, put the label inside the caption to ensure correct numbering.

So I changed the LaTeX file to this

Reference to \autoref{fall-elevations} in the middle of all this.

\begin{sidewaysfigure}
\begin{center}
\includegraphics[width=9in]{all-elevations.pdf}
\end{center}
\hspace{1.5in}\parbox{6in}{\caption{Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\label{fall-elevations}}}
\end{sidewaysfigure}

and the reference in the text came out just right.

A quick test confirmed that this

Reference to \autoref{fall-elevations} in the middle of all this.

\begin{sidewaysfigure}
\begin{center}
\includegraphics[width=9in]{all-elevations.pdf}
\end{center}
\hspace{1.5in}\parbox{6in}{\caption{Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}\label{fall-elevations}}
\end{sidewaysfigure}

would also work. Here, the label was not inside the caption, but was right after it in the parbox.

I’d read long ago that labels had to immediately follow captions in order to pick up the right number, but I didn’t realize that even formatting commands that produce no text couldn’t come between them.

Tags: