Figure caption and label in LaTeX (caption, label, ref)
\caption{text} inside a figure environment prints "Figure 1: text" and numbers the figure. A \label after it names the figure, and \ref prints its number in the text.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{graphicx}
\usepackage{caption}Add a caption and refer to the figure
The caption stands below the image when \caption follows \includegraphics. The label name is free; the prefix fig: keeps figures apart from tables and sections. Compile twice so that \ref shows the number.
\begin{figure}
\centering
\includegraphics[width=0.5\textwidth]{image.png}
\caption{The measurement setup}
\label{fig:setup}
\end{figure}
Figure~\ref{fig:setup} shows the setup.Give the list of figures a shorter text
The optional argument of \caption is the text for \listoffigures. Use it when the caption is long or contains a citation that should not appear in the list.
\listoffigures
\begin{figure}
\centering
\includegraphics[width=0.5\textwidth]{image.png}
\caption[Measurement setup]{The measurement setup with the laser on the left
and the detector on the right}
\end{figure}Change the look and the name
The caption package formats all captions with \captionsetup. font=small reduces the font size, labelfont=bf sets "Figure 1" in bold, and name=Fig. in the settings for figure replaces the word "Figure".
This goes into the preamble.
\captionsetup{font=small, labelfont=bf}
\captionsetup[figure]{name=Fig.}Caption without a figure environment
\captionof{figure}{text} from the caption package numbers an image that does not float, for example in a minipage or a center environment. \caption*{text} inside a figure prints the text without "Figure" and a number.
\begin{center}
\includegraphics[width=0.5\textwidth]{image.png}
\captionof{figure}{The measurement setup}
\label{fig:setup-here}
\end{center}Pitfalls
\labelhas to come after\caption. Before the caption it takes the number of the current section, and\refprints that number.- After the first compile run
\refprints??and the log shows Reference `sec:methods' on page 1 undefined. The second run fills in the number. - With
\usepackage[ngerman]{babel}, LaTeX prints "Abbildung" instead of "Figure". Do not type the word into the caption yourself.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
caption - How-to Cross-references in LaTeX
- How-to List of figures and list of tables in LaTeX
- How-to Subfigures in LaTeX
- Error \caption outside float
- Package
graphicx