Captioning and referencing figures

Captioning, labeling and referencing

Captioning images to add a brief description and labeling them for further reference are two important tools when working on a lengthy text.

Captions

Let's start with a caption example:

\begin{figure}[h]
\caption{Example of a parametric plot ($\sin (x), \cos(x), x$)}
\centering
\includegraphics[width=0.5\textwidth]{spiral}
\end{figure}
Example of parametric plot with a caption above

Just add the \caption{Some caption} and write the text to be displayed inside the braces . The placement of the caption depends on where you place the command. If it's above the \includegraphics command, then the caption will be on top of the figure. If it's below, then the caption will also be set below the figure.

Captions can also be placed right after the figures. The sidecap package uses similar code to that used in the previous example to accomplish this.

\documentclass{article}
\usepackage[rightcaption]{sidecap}

\usepackage{graphicx} %package to manage images
\graphicspath{ {images/} }

\begin{SCfigure}[0.5][h]
\caption{Using again the picture of the universe.
This caption will be on the right}
\includegraphics[width=0.6\textwidth]{universe}
\end{SCfigure}
Example of an image with a caption to the right

There are two new commands:

\usepackage[rightcaption]{sidecap}

As you may expect, this line will import a package named sidecap, but there is an additional parameter: rightcaption. This parameter establishes the placement of the caption at the right of the picture. You can also use leftcaption. In book-like documents, outercaption and innercaption are also available. The names of these are self-descriptive.

\begin{SCfigure}[0.5][h] \end{SCfigure}

The above example defines an environment similar to figure. The first parameter is the width of the caption relative to the size of the image, as declared in \includegraphics. The second parameter h works exactly as in the figure environment. See the placement section for more information.

For more advanced management of caption formatting, check the further reading section for references.

Labels and cross-references

Figures, like many other elements in a LaTeX document (equations, tables, plots, etc.), can be referenced within the text. Just add a \label to the figure or SCfigure environment, then later use that label to refer to the picture.

\begin{figure}[h]
    \centering
    \includegraphics[width=0.25\textwidth]{mesh}
    \caption{a nice plot}
    \label{fig:mesh1}
\end{figure}

As you can see in the figure \ref{fig:mesh1}, the 
function grows near 0. Also, in the page \pageref{fig:mesh1} 
is the same example.
Example of figure with label

There are three commands that generate cross-references in this example.

\label{fig:mesh1}

This will set a label for this figure. Since labels can be used in several types of elements within the document, it's a good practice to use a prefix, such as fig: in the example.

\ref{fig:mesh1}

This command will insert the number assigned to the figure. It's automatically generated and will be updated if you insert another figure before the referenced one.

\pageref{fig:mesh1}

This prints out the page number where the referenced image appears.

The \caption is mandatory to reference a figure.

Another great characteristic in a LaTeX document is the ability to automatically generate a list of figures. This is straightforward.

\listoffigures
Example of list of figures

This command only works on captioned figures since it uses the caption in the table. The example above lists the images in this article.

Last updated

Was this helpful?