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!
LOL ! That’s exactly the kind of memory I have from my LaTeX years… ever since then I switched to DocBook, mainly because it’s a much simpler “semantic formating” system when you don’t need picometer precise text formating ;)
I never used DocBook, but it looks interesting. Nice part about it is that you could just post your source file on the web, and style it with CSS for a nice display.
Here is the thing – how does it deal with mathematical symbols, formulas and etc?
I’ve never played with it (mathematics are far behind me now !) but this might do it :
http://www.docbook.org/specs/wd-docbook-mathml-1.1CR1.html
+
http://www.w3.org/TR/MathML2/
Let me know if you have a chance to try ;-)
Thanks! I will check it out.
Rock. You saved me, and I only pulled out one clump of hair.
-Jack
Awesome! Glad it helped you Jack. It took me a while to figure this out, and I was going nuts over it. Good to know that I saved at least one person from similar fate. ;)
Thank-you so much. I hit on this post from a google search, as I was having this exact problem, and was literally about to embark on “hours of extensive debugging, cursing under your breath, commenting out custom code dealing with figure layout and much frustration”. You have saved me that.
That’s why I posted this – to save people from this experience. Glad I could help, :)
Luvvvvvvvvvv you dude…was scratching my head the whole day…redrew some of figures not knowing if there was some crappy thing going on with \psfrag or watever…thanks a lot man…
Thank you for this! You’ve definitely saved me from hours of painful debugging :) I only spent 15 minutes getting frustrated…
Thank you! Fortunately, this came up on my google search for “latex \ref troubleshooting.” Otherwise I, like you, would have wasted precious hours.
…and another one who’s thankful for your hint! Although I have already been debugging for quite a while it at least saved me some more hours of my life! :) Thanks a lot!
yeeeha! you saved me! one day before I have to submit my bachelor-thesis. Of all things, I do not need any trouble with latex today…
big thanks!
Man, you saved me a lot of time with your tip, thank you! I was starting to have Latex…
THANKS THANKS THANKS THANKS… I’ll save you other 996 ones ^_^ you saved my master thesis!!
Oh yeah, thanks a lot! I’m really glad that I found your hint. In my first document I switched to manually numbering the figures… :( Now I’m happy with Latex again. :)
Many thanks to your webpage that turned up from a Google search. Previously, I had the same problem but I never did figure out what went wrong. I ended up removing the \ref so that the numbering problem is not apparent.
Now I know better, all thanks to you. :D
Now back to my #*&$% physics reports…
Thanks a ton. I was working on a latex doc and was having this problem. BTW, when you search for “figure numbers wrong in latex” in google, this page comes up as #1. Pretty cool.
Thanks… wondering about wrong references has already cost me a good deal of my time. Because of my rather mystic appreciation of the LaTeX rendering process (which necessitates such wondrous steps as multiple runs, interleaved with a few calls of BibTeX for good measure), I’ve never been able to figure out, where this problem really originates.
Thanks man, you saved me a lot of troubles and time.. Two days before my Master’s dissertation deadline!
A big thanks,
Luca
Like everyone else, THANKS SO MUCH FOR FIGURING THAT OUT AND POSTING IT. I was on the brink of insanity.
Thank you, thank you, thank you. I was hard-coding the references out of desperation. You are awesome!
thanks! great post, and great explaination
Thankyou! I had the exact same problem, and luckily found this post with a google search. Thanks for saving (at least three) hours of my life!
You saved me hours!
Thank you! That is all….
CRAP. Thanks a lot!!
Another thing on the same topic is that if the \caption command is inside a center environment the numbering will be screwed up if the \label is outside the center environment, even if they are in the correct order.. took me 2 hours to figure this out.. if u hadn’t told me about the order though, I probably would have sat longer =)
Oh wow… I never really had that one happen to me. I can kinda see how that would make a huge difference for the latex interpreter, even though it logically shouldn’t matter.
Thanks for the tip. :)
WOO! Thanks Luke! That took 10 minutes instead of 3 hours!
Luke, a long list of happy replies. I’ll add to it that you made my day today! I’m going to have a look at your other post with the label “latex” !
Dude you are awsome. I have 45 minutes to turn in my thesis and you just saved my ass
thank u very much… I had to submit the document after a few hours n all chapters except chapter 1 had \label above \caption n i was so *&^*&%^&$%* luckily n i found on google search this entry. It helped me n i m really thankful to owner of this blog…. i’ve put this entry to my blog as well :)
Thanks for this!
Saved me a few hours of frustration!
Thanks a bunch! I spent about half an hour debugging this and thought someone must have solved this before. Then I found you post!
Many thanks.. now off to bed.
omg thank you YOU!
Excellent advice. It was very kind of you to share your knowledge for free. You should be very proud of yourself. Keep up the good work.
YES!! After hours of frustration, google finally hit jackpot and I can sleep to night! Thank you!!!
Best Help EVER!!!!!!
You saved my life dude…thanks a ton
That’s twice in one hour that I’ve googled latex problem and found a link to your site. Thanks for solving my problems :)
Glad I could help guys. :) I find these things so that you don’t have to!
you saved me !!!
thanks a lot!
Thanks :-)
Saved me a lot frustration :-)
Thank you very much from saving me!
Thanks for this Post!
god bless you :))
Yeah, cheers dude!
For the record, the label needs to come directly after the caption for it to work properly!
Thankyouthankyouthankyou! This has indeed saved me a lot of time. I thought it must be something about ordering, but this got it all sorted very quickly. Thankyou for putting this information on the web.
You saved me!
I sometimes mutter to myself everytime someone says how great Sweave and Latex are that nobody comes saves you when you run into these things….
I just can repeat the others praises: Thank you very much! You just solved my problem!
Thanks! That helped me a lot :-)
I love you man, you saved my day!
thanks from a desperate student!!
Another way to avoid this problem is as follows.
dude, you just saved me at least 2 hours in going through my whole code, line by line.
THANK YOU
Thanks a lot dude! It was wonderful.
This bug is ridiculous. Why would latex do that?
Thanks again :)
Thanks from Brazil! God bless you.
oh.. thanks a lot. i have been trying out all to get the numbering proper… finally google right away pointed me to your page… indeed helped.
thanks a lot..
You the man!
Heh… This post always gets bunch of new comments at the end of the semester. :) I’m saving lives, and dissertations here!
@Luke Maciak: And here’s one more. Thanks a lot man!
Thanks ! You solved a long-standing puzzle I have!
I noticed it from time to time but was not able to figure out why. I guess sometimes I put caption first, and at times I put label first as I type my doc and didn’t notice the subtle difference. Time to ask LaTeX people to set thing right !!
Thanks man! saved me a ton of anxiety … add me to the long list of people whom u’d helped :)
Thanks a ton for the help. I too, like others, was trapped by the same mistake. Will remember this for long time now…
GENIOUS!
Thank you so much! :)
You are awesome…
And happy new year! ;)
Thanks a lot! I was going mad about wrong figure numbers. Your post is very helpful!
Fantastic. Just saved my life.
Ah! Exactly what I was looking for. Thank you so much!!
Thank you for saving me another 3 hours or more!!
You star!!!Was going bang my head on a wall but you saved it..latex is moody…
Had the same problem, saved me time, thanks!
Thank you..You just saved me hours!!
Thank you very much
You made me fix my problem in ten seconds instead of hours. I owe you time!
You are a good man! Finding lifesaving posts like this one makes me think there is hope for the world after all ;) Thank you!
well… you just have saved a lot of man-hour effort to all of us! thanks!
Thanks a lot for that. You save my time as well.
Label should be directly under caption.
I used “\end{center}” between caption and label and I had the problem as well.
Thanks.
It was also my problem for a very long time.
Thank U!
THANKS! Solve my problem too!!!
I’ll join this group of extremely happy people
THANKS MAN!!
Thanks, I was trying to figure out a solution for last three days :-)
Thank you so much for this advice!
Thanks the gods for you and google!
Thank you so much!
Ahah! You really save me some debugging hours!
Thank u :D
Thought I’d chime in with my thanks too…
Cheers.
Many thanks. That’s my dissertation saved right there!
Hi,
You saved my life :) This is the exact problem I had with referencing. Solved in 2 min thanks to that post.
Cheers!
Thank you! It saved me too. Why hasn’t this bug been squashed?
Thanks man!
You can count another student rescued! xD
PS: you should probably add a counter for people (like me) who wrote something like: “hey thank man! You saved my life” :-)
A big THANK YOU from a PhD student in Greece :-)
Thanks a lot!
Have a nice day!
You just saved me 3 hours. Holy crap I love the internet! Thanks!
Here is another soul that you saved!
Holy crap. You are a saint! Thank you so much!
Thank you very, very much! I was so desperate that I’ve changed manually the index of almost all figures. thanx a lot!.
Holy cow… Ah, and be sure to keep the \begin{center} – end{center} order clean, means: encapsulate caption and label and don’t mix it up…
Man – this is the gift that keeps on giving. Thanks!!!!
Just another thank you ;). Keep \label and \caption within the same block… don’t split them.