> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/more-topics/42-referencing-figures.md).

# Referencing Figures

In LaTeX, we can *label* entities that are numbered (sections, formulas, etc), and then use that *label* to *refer* to them elsewhere, and the same commands apply to the figure environment as well (they are numbered).

## Basics of Labels and Referencing

```latex
\label{marker}
```

The *marker* can be seen as a name that we give to the object that we want to reference. It's important to add `\label` *after* a numbered element e.g. `\section`, `\subsection`, `\caption` etc, otherwise the label won't "latch on" to the correct number or counter.

```latex
\ref{marker}
```

This prints the number assigned to the object labeled by *marker*.

```latex
\pageref{marker}
```

This prints the number of the page where the object labeled by *marker* appears.

## Compiling a LaTeX document with labels and references

The *marker* used to label objects is not shown anywhere in the document, and references to it are replaced with the appropriate numbers. If we reference a non-existent marker, LaTeX will compile successfully but with a warning about undefined references. The reference to the unknown marker will be replaced by *??*.

In the example from Overleaf included above, you can see that we could successfully add reference to figures included in the document later. To make it possible, LaTeX has to be run twice - the first run compiles and stores all labels and their positions, and in the second run all the references are replaced with the appropriate numbers. Thus, we have to compile our document twice to see the correct output.

## Using meaningful names to refer to figures

Since we can use any string as a label, it's a common practice to add a few letters to the label (as prefix) to indicate what is being labeled. This becomes important when a lot of different types of objects are referenced in a document, as it might be useful to remember the kind of object a label refers to it. Besides, it also makes it possible to reference different kind of objects using a common string.

For example, if a document contains

* a section on population of nations,
* a table containing the population numbers, and
* a figure containing a bar chart of the population sizes,

it might be convenient to refer to all of them using variants of *population*. This can be accomplished by using the labels

* *sec:population* for the section,
* *tab:population* for the table, and
* *fig:population* for the figure.

The following is an example for figures -

```latex
\begin{figure}[h!]
  \includegraphics[scale=1.7]{birds.jpg}
  \caption{The birds}
  \label{fig:birds}
\end{figure}
```

Again, note that `\label` is given *after* `\caption`.

&#x20;[Open an example in Overleaf](https://www.overleaf.com/project/new/template/26046?id=111048636\&templateName=Example+to+cross-reference+figures\&latexEngine=pdflatex\&texImage=texlive-full%3A2022.1\&mainFile=)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/more-topics/42-referencing-figures.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
