What I tell you right now may save you hours of extensive debugging, cursing under your breath, commenting out custom code dealing with figure layout and much frustration. Whenever you use figures, always (and I mean ALWAYS EVER FOREVER ALWAYS) put \caption first, and \label second like this:
\begin{figure}[htp]
\centering
\includegraphics{image.eps}
\caption{Some Image} % \caption IS ALWAYS FIRST
\label{fig:some-image} % \label IS ALWAYS SECOND
\end{figure}
Easy way to remember this – order your image tags alphabetically. C goes before L. Always. No exception.
If you put the \label above \caption you will run into trouble when referencing figures inside subsections. In my case, the caption underneath the figure would say Fig. 4.2 but the output of \ref would be 4.3.10 because somehow it was picking up the section numbers wrong. The whole damn chapter 4 had the caption/label pairs flipped – but the rest of the document was fine. I have no clue what possessed me to write it this way.
Now I know better. This is the 3 hours of my life that I will never get back. All because I put label before a caption. Do not do that to yourself!
Why this happens
Perhaps if people know why this happens, it will be much easier to remember what goes where.
The short version : caption command within a figure or table acts much like a sectioning command within the document. So if you don’t put a caption, the label has nothing to “label”, and the closest thing it can find is a section in which you are in.
Long version
To understand what is going on we must establish a few things. First, a label command when placed anywhere in the document will label the section of the document in which it is places. So something like this:
Section \ref{sec-one} is great!
\subsection{First section}\label{sec-one} This is the first section
Will produce:
Section 2.11 is great!
2.11 First section
This is the first section
Second, label command appearing in a numbered environment acts differently. Numbered environments are things like equation, eqnarray, enumerate, figure, table… In each of these environments the label command acts differently. For example, in eqnarray environment the label can go anywhere before the \\ that ends the current equation – effectively giving each equation it’s own label!
The figure environment has different rules. In particular, the thing that creates numbering inside the figure environment is the caption command. A figure may have multiple captions – effectively creating multiple “sections” within the figure. On other hand, if the caption command is never used, the label still uses the text numbering (which is the number of the section of the document it is in).
So, if we have something like:
\begin{figure}
BLAH
\label{sec:one}
\caption{Blah figure!}
\label{fig:blah}
\end{figure}
we now know that
\ref{sec:one}
will refer to the section of the document in the text (something like 2.11), whileFigure \ref{fig:blah}
will actually refer to the thing we want (something like Figure 1).Thanks for saving my three hours!
Well it’s been almost 2 months since your last ‘thank you,’ so let me just say:
YOU.SAVED.MY.LIFE.
Thanks man.
Dude! The one and the only exact solution of all times! Thanks :brofist
A big thanks to you sir – this post saved my day.
Thank you very much! You have saved my life… lol!
Thank you dude! You saved me!! :D
Just to let you know: it still helps people like me a loooot! thank you very very much!
Thanks mate, saved me some time :)
Thank you! Think you’ve just saved me from hours of frustration!
Thanks for figuring this out! I had one other issue, however. For some reason putting \label after \caption still seems to be picking up the section number for me. I’ve changed it a few times, changed the label name a few times, compiled and recompiled and it still says Figure 4.3 when it should say Figure 8. The Figure includes a TikzPicture (a simple graph, in this case). Do you think it has something to do with that? Any ideas you might have would be appreciated. Thanks.
Actually, I just figured it out. Sorry about that. Thanks, though.
You’re a life saver and I think I love you! I was going crazy trying to figure out what was wrong.
+1
Thanks a bunch!
Cheers
70 minutes before a manuscript submission deadline, you are my savior.
:) You are a lifesaver!
Thanks bro! You help me a lot with your post. Ty Ty Ty
I’ll join the long list:
Thank you SO MUCH for this post!
You can add 1 to your list of lives saved… :)
WOW!! Super awesome. Many, many thanks! ^_^
Looks like it’s been a while since this page saved someone… but it just saved me!
Thanks very much!
Wow, great post. This makes me wonder what people did in the days before huge quantities of advice was available and easily searchable.
Thanks mate! that was an awesome post… Spent the last couple of day trying to fix it but finally got it done! thanks!!
Thanks from Israel! Very helpful!
GREAT! Many, many thanks!
Had the same problem just now and thought I would have to spent the 3 or more hours, too.
THANK YOU.
life is a bit better now.
I wasted three days figuring out why figures were being referenced by section number. Could not find it anywhere till I saw your post. Thanks for
taking the time to post this.
Thanks, helped save time for my Master’s thesis!
Thank you! Saved me 3 hours obviously :) Your hint changed my 4.1.1 labeled figure into a correct 4.1 in the text.
THANK YOU
Thanks a lot! Saved at least three hours from my life
YOU SAVED MY TIME!!! THANK YOU!!!
I know it has been 6 years since this post but you still helped me!
Thank you!
Helping people since 2007… lol Thanks!
\caption{you saved my life}
\label{fig:this latex glitch almost made me crap my pants}
\comment{thank you}
\begin{figure}[!htbp]
\centering
\caption{You saved my life as well}
\label{fig:thank you so much}
\end{figure}
Dude, 5 years on and you just saved a South African undergrad from doing what you described. Big up.
Needless to say, thank you! You saved me so many hours of debugging!
THANK YOU
THANK YOU
THANK YOU
I found your tip after wasting an hour. Thanks for not wasting more hours.
I was fighting this problem for a half hour until just finding this post. Thanks.
THANK YOU VERY MUCH !! I think I would have never figured this out by myself. You have saved me a lot of hours of work, and a lot of frustration.
Thanks man! You saved my time :-)
Thank you! You’re 3 hour struggle has turned into a 3 minute google search. You’re a lifesaver.
7 years on, you continue to make our life easy!!! Thanks again.
Well, thanks for saving me 3 hours. It seems like the 15 minutes you spent on top writing it up were well spent for everyone. :)
Thanks a million!!!
Thanks very much, 3 hours of my life has indeed been saved!
Thank you, saved me a lot of time editing my thesis.
Thanks for saving my time and pain with this problem!
That concept is rather ..stupid, the links in the document are not linking to the figure itself but only the caption..
Regards
Thank you so much (7+ years later)! For some reason I never had a problem putting \label early in the figure block, but for some reason this time it decided to mess up! Switching it to below \caption solved the problem, with minimal hair loss.
@ Luke:
It’s 2015 now and you saved me :D
Thanks for posting this and for highlighting the solution. Saved me quite some time….
Thanks! You just saved me hours and hours of frustration.
You have just saved me those three hours of suffering and reduced them to fifteen minutes. Thanks!
A lot of debugging saved with this post.
Thank you VERY I mean VERY much! I’ve spent a lot of hours looking for some reason, why I have ?? instead of correct reference!
9 years later, this is exactly the problem i was having, and you have spared me the 3+ hours. thank you!