### LaTeX: Wrong figure numbers in subfloat

Tuesday, August 4 2009, 16:14
I've been puzzled by some incorrect figure numbers in my LaTeX document recently. I have a figure, 2, with two subfigures, 2a and 2b, but when referencing them, I get figures 1a and 1b.
It turned out you have to put the \label commands in the correct order, like so:
\begin{figure}[b]  \caption{Detail of cameras on a Nokia N95 smartphone}  \label{fig:cameras}  \centering  \subfloat[User camera located above screen]{    \label{fig:user_camera}    \includegraphics{images/front_camera.png}}  \hspace{2cm}  \subfloat[Perspective camera on backside of device]{    \label{fig:perspective_camera}    \includegraphics{images/back_camera.png}}\end{figure}
Also, make sure that \caption always precedes \label as Luke Maciak suggests.
Note that as Matthias mentions in the comments below, you may have to put the \caption and \label after the \subfloats. I haven't determined yet when one or the other should be done, but just remember that moving them around may resolve your problem.
Rating:
Actions:
 Matthias (guest) Saturday, August 15 2009, 1:16 Thanks for your post! I had a similar problem: several figure environments which of each contained several subfloats. Even when I had my code in your order, it was wrong: e.g. figure1 had 2 subfloats and figure2 with 3. Then, it referenced figure1 correctly as fig. 1, but the subfloats as 2a and 2b, then figure2 correctly as fig.2, but its subfloats as 3a, 3b and 3c. I don't know whats wrong with the package but it obviously needs a lot of debugging to be done! And now the solution: put the caption and the label of the figure AFTER the subfigures. Do it with both. And: caption first... Graham (guest) Monday, November 9 2009, 19:21 Matthias- that has allowed me to heave a sigh of relief after a *lot* of head-scratching. Thanks! Post reply Hide comments