## Friday, December 7, 2012

### Figure Labeling in LaTeX

I recently finished writing my master's thesis, which is easily the biggest document I've ever composed.  But because of all my time in the past few weeks in LaTeX, I've learned a lot of little things about it.  This post is a quick note about labeling figures, but I'll post more tips and tricks in the next week or so.

One of the best features about LaTeX is the ability to label the figures, and reference to them by name so that all the references are dynamic.  This makes it much easier to go insert a figure in your document, so you don't have to change all the figure numbers that occur after the inserted one.  This is done by using \label{} inside the figure environment, and using \ref{} in the document text.

### Problem

When I was writing my thesis, I noticed that this didn't always work, sometimes I'd get a "figure ??" for a reference I definitely knew I'd typed in correctly.  However, I missed one small point about labeling figures.

### Solution

It's very important to place the label after the caption in the figure, otherwise LaTeX will not know what you're labeling.  Here's the proper way to insert a figure:

\begin{figure}[htbp]
\centering
\includegraphics{figures/Foo_img}
\caption{Foo}
\label{Foo}
\end{figure}

If you place the label before the caption, LaTeX will render a broken reference.  If you have what appear to be random broken references, go through and make sure your label is the last thing before you close the environment.