by Stefan Kottwitz » Thu Oct 21, 2021 4:03 am
\caption takes care of the figure counter that
\label should ise. If
\label comes before
\caption, it takes some previous counter, which is wrong.
Let's look at this in this example:
Code: Select all
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\section{Introduction}
\section{Figures}
\begin{figure}[hb]
\centering
\includegraphics[height=2cm,width=3cm]{example-image}
\label{fig:example}% before \caption, which is wrong
\caption{An example figure}
\end{figure}
\section{Appendix}
\begin{figure}[hb]
\centering
\includegraphics[height=2cm,width=3cm]{example-image}
\label{fig:other}% before \caption, which is wrong
\caption{Another example figure}
\end{figure}
% Now we get wrong reference numbers:
See example figure \ref{fig:example} and another figure \ref{fig:other}.
\end{document}
When we compile it, we get:

- figures-labels.png (24.28 KiB) Viewed 64878 times
We can see that the references at the end are clearly wrong. If we take a closer look, we see that they refere to the section number, where they are in. Move
\label after
\caption, and the issue is fixed.
Stefan
[latex]\caption[/latex] takes care of the figure counter that [latex]\label[/latex] should ise. If [latex]\label[/latex] comes before [latex]\caption[/latex], it takes some previous counter, which is wrong.
Let's look at this in this example:
[code]\documentclass{article}
\usepackage{graphicx}
\begin{document}
\section{Introduction}
\section{Figures}
\begin{figure}[hb]
\centering
\includegraphics[height=2cm,width=3cm]{example-image}
\label{fig:example}% before \caption, which is wrong
\caption{An example figure}
\end{figure}
\section{Appendix}
\begin{figure}[hb]
\centering
\includegraphics[height=2cm,width=3cm]{example-image}
\label{fig:other}% before \caption, which is wrong
\caption{Another example figure}
\end{figure}
% Now we get wrong reference numbers:
See example figure \ref{fig:example} and another figure \ref{fig:other}.
\end{document}[/code]
When we compile it, we get:
[attachment=0]figures-labels.png[/attachment]
We can see that the references at the end are clearly wrong. If we take a closer look, we see that they refere to the section number, where they are in. Move [latex]\label[/latex] after [latex]\caption[/latex], and the issue is fixed.
Stefan