# 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:

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

<figure><img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2FEP8w7Jdhe6NCz26mSztv%2FInsertingImagesEx9Overleaf.png?alt=media&#x26;token=67a1b052-4000-4596-aca0-661889fdca87" alt="Example of parametric plot with a caption above" width="375"><figcaption></figcaption></figure>

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.

```latex
\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}
```

<figure><img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2FOyV2oiWV5Z7WlAW19thX%2FInsertingImagesEx10Overleaf.png?alt=media&#x26;token=d84c914f-2ac5-4c79-91c9-19676e97d773" alt="Example of an image with a caption to the right" width="375"><figcaption></figcaption></figure>

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](#positioning) section for more information.

For more advanced management of caption formatting, check the [further reading](#further-reading) section for references.

### Labels and cross-references

{% hint style="warning" %}
When using cross-references your LaTeX project must be compiled twice, otherwise the references, the page references and the table of figures won't work—Overleaf takes care of that for you.
{% endhint %}

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.

```latex
\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.
```

<figure><img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2F25cBZE656533eaT1uW1M%2FInsertingImagesEx11Overleaf.png?alt=media&#x26;token=3652260b-a2fe-4114-a747-dcac4242a3d9" alt="Example of figure with label" width="375"><figcaption></figcaption></figure>

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.&#x20;

`\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.

```latex
\listoffigures
```

![Example of list of figures](https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2FwDXpHOe77HzOooTUYZjX%2FInsertingImagesEx12Overleaf.png?alt=media\&token=c7577092-f13c-4bc9-86b9-12ada008c933)

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