LaTex: Fixing Wrong Figure Numbers

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!

This entry was posted in programming and tagged . Bookmark the permalink.



241 Responses to LaTex: Fixing Wrong Figure Numbers

  1. Stephane FRANCE Mozilla Firefox Debian GNU/Linux says:

    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 ;)

    Reply  |  Quote
  2. Luke UNITED STATES Mozilla Firefox Windows says:

    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?

    Reply  |  Quote
  3. Stephane FRANCE Mozilla Firefox Debian GNU/Linux says:

    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 ;-)

    Reply  |  Quote
  4. Luke UNITED STATES Mozilla Firefox Windows says:

    Thanks! I will check it out.

    Reply  |  Quote
  5. Jack UNITED STATES Mozilla Firefox Windows says:

    Rock. You saved me, and I only pulled out one clump of hair.

    -Jack

    Reply  |  Quote
  6. Luke UNITED STATES Mozilla Firefox Ubuntu Linux says:

    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. ;)

    Reply  |  Quote
  7. Kieran Mozilla Firefox Linux says:

    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.

    Reply  |  Quote
  8. Luke UNITED STATES Mozilla Firefox Ubuntu Linux says:

    That’s why I posted this – to save people from this experience. Glad I could help, :)

    Reply  |  Quote
  9. mini UNITED STATES Mozilla Firefox Windows says:

    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…

    Reply  |  Quote
  10. Steven CANADA Mozilla Firefox Ubuntu Linux says:

    Thank you for this! You’ve definitely saved me from hours of painful debugging :) I only spent 15 minutes getting frustrated…

    Reply  |  Quote
  11. Ben UNITED STATES Mozilla Firefox Linux says:

    Thank you! Fortunately, this came up on my google search for “latex \ref troubleshooting.” Otherwise I, like you, would have wasted precious hours.

    Reply  |  Quote
  12. gehho GERMANY Mozilla Firefox Windows says:

    …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!

    Reply  |  Quote
  13. marve GERMANY Mozilla Firefox Ubuntu Linux says:

    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!

    Reply  |  Quote
  14. Man, you saved me a lot of time with your tip, thank you! I was starting to have Latex…

    Reply  |  Quote
  15. Gabrief ITALY Opera Windows says:

    THANKS THANKS THANKS THANKS… I’ll save you other 996 ones ^_^ you saved my master thesis!!

    Reply  |  Quote
  16. Peter UNITED KINGDOM Safari Mac OS says:

    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. :)

    Reply  |  Quote
  17. Marcel SINGAPORE Internet Explorer Windows says:

    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…

    Reply  |  Quote
  18. Mike UNITED STATES Mozilla Firefox Ubuntu Linux says:

    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.

    Reply  |  Quote
  19. Leo AUSTRIA Mozilla Firefox Ubuntu Linux says:

    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.

    Reply  |  Quote
  20. Luca ITALY Mozilla Firefox Ubuntu Linux says:

    Thanks man, you saved me a lot of troubles and time.. Two days before my Master’s dissertation deadline!

    A big thanks,
    Luca

    Reply  |  Quote
  21. Rodolphe Courtier UNITED STATES Mozilla Firefox Windows says:

    Like everyone else, THANKS SO MUCH FOR FIGURING THAT OUT AND POSTING IT. I was on the brink of insanity.

    Reply  |  Quote
  22. Natalie Schluter IRELAND Internet Explorer Windows says:

    Thank you, thank you, thank you. I was hard-coding the references out of desperation. You are awesome!

    Reply  |  Quote
  23. iontic SWITZERLAND Mozilla Firefox Windows says:

    thanks! great post, and great explaination

    Reply  |  Quote
  24. Robert JAPAN Opera Mac OS says:

    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!

    Reply  |  Quote
  25. JohnFlux UNITED KINGDOM Mozilla Firefox Ubuntu Linux says:

    You saved me hours!

    Reply  |  Quote
  26. Leela UNITED KINGDOM Internet Explorer Windows says:

    Thank you! That is all….

    Reply  |  Quote
  27. Jim UNITED STATES Mozilla Firefox Ubuntu Linux says:

    CRAP. Thanks a lot!!

    Reply  |  Quote
  28. Tomas SWEDEN Mozilla Firefox Ubuntu Linux says:

    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 =)

    Reply  |  Quote
  29. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    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. :)

    Reply  |  Quote
  30. LD UNITED STATES Mozilla Firefox Linux says:

    WOO! Thanks Luke! That took 10 minutes instead of 3 hours!

    Reply  |  Quote
  31. Luite NETHERLANDS Mozilla Firefox Linux says:

    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” !

    Reply  |  Quote
  32. Shannon UNITED STATES Internet Explorer Windows says:

    Dude you are awsome. I have 45 minutes to turn in my thesis and you just saved my ass

    Reply  |  Quote
  33. Zeeshan PAKISTAN Flock Windows says:

    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 :)

    Reply  |  Quote
  34. Emil SWEDEN Mozilla Firefox Fedora Linux says:

    Thanks for this!
    Saved me a few hours of frustration!

    Reply  |  Quote
  35. Bayes Bloomington CANADA Mozilla Firefox Windows says:

    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.

    Reply  |  Quote
  36. grace UNITED KINGDOM Mozilla Firefox Ubuntu Linux says:

    omg thank you YOU!

    Reply  |  Quote
  37. Nick UNITED KINGDOM Mozilla Firefox Windows says:

    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.

    Reply  |  Quote
  38. Eirik UNITED STATES Internet Explorer Windows says:

    YES!! After hours of frustration, google finally hit jackpot and I can sleep to night! Thank you!!!

    Reply  |  Quote
  39. Kim DENMARK Mozilla Firefox Windows says:

    Best Help EVER!!!!!!

    Reply  |  Quote
  40. Pankaj UNITED STATES Opera Windows says:

    You saved my life dude…thanks a ton

    Reply  |  Quote
  41. goodmami UNITED STATES Mozilla Firefox Linux says:

    That’s twice in one hour that I’ve googled latex problem and found a link to your site. Thanks for solving my problems :)

    Reply  |  Quote
  42. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    Glad I could help guys. :) I find these things so that you don’t have to!

    Reply  |  Quote
  43. Sebastian GERMANY Mozilla Firefox Linux says:

    you saved me !!!
    thanks a lot!

    Reply  |  Quote
  44. Christian DENMARK Mozilla Firefox Mac OS says:

    Thanks :-)

    Saved me a lot frustration :-)

    Reply  |  Quote
  45. Galiya ITALY Safari Mac OS says:

    Thank you very much from saving me!

    Reply  |  Quote
  46. Michael GERMANY Mozilla Firefox Debian GNU/Linux says:

    Thanks for this Post!

    Reply  |  Quote
  47. izzet TURKEY Mozilla Firefox Linux says:

    god bless you :))

    Reply  |  Quote
  48. Speed UNITED KINGDOM Mozilla Firefox Windows says:

    Yeah, cheers dude!

    For the record, the label needs to come directly after the caption for it to work properly!

    Reply  |  Quote
  49. Rob Egginton UNITED KINGDOM Mozilla Firefox Mac OS says:

    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.

    Reply  |  Quote
  50. Saved! UNITED STATES Internet Explorer Windows says:

    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….

    Reply  |  Quote
  51. Manuela SWITZERLAND Mozilla Firefox Linux says:

    I just can repeat the others praises: Thank you very much! You just solved my problem!

    Reply  |  Quote
  52. Knut NORWAY Mozilla Firefox Windows says:

    Thanks! That helped me a lot :-)

    Reply  |  Quote
  53. Kristian SWEDEN Mozilla Firefox Debian GNU/Linux says:

    I love you man, you saved my day!

    Reply  |  Quote
  54. javier SWEDEN Mozilla Firefox Windows says:

    thanks from a desperate student!!

    Reply  |  Quote
  55. Shida UNITED STATES Mozilla Firefox Linux says:

    Another way to avoid this problem is as follows.

    \begin{figure}[htp]
     	\centering
     		\includegraphics{image.eps}
     	\caption{Some Image \label{fig:some-image}}
    \end{figure}
    Reply  |  Quote
  56. firas CANADA Safari Mac OS says:

    dude, you just saved me at least 2 hours in going through my whole code, line by line.

    THANK YOU

    Reply  |  Quote
  57. Hossein UNITED STATES Mozilla Firefox Ubuntu Linux says:

    Thanks a lot dude! It was wonderful.

    This bug is ridiculous. Why would latex do that?

    Thanks again :)

    Reply  |  Quote
  58. Pedro Ribeiro BRAZIL Epiphany Linux says:

    Thanks from Brazil! God bless you.

    Reply  |  Quote
  59. Sreejith GERMANY Mozilla Firefox Debian GNU/Linux says:

    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..

    Reply  |  Quote
  60. Carol UNITED STATES Safari Mac OS says:

    You the man!

    Reply  |  Quote
  61. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    Heh… This post always gets bunch of new comments at the end of the semester. :) I’m saving lives, and dissertations here!

    Reply  |  Quote
  62. Joe UNITED STATES Mozilla Firefox Mac OS says:

    @Luke Maciak: And here’s one more. Thanks a lot man!

    Reply  |  Quote
  63. centguy SINGAPORE Mozilla Firefox Fedora Linux says:

    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 !!

    Reply  |  Quote
  64. Aditya UNITED STATES Google Chrome Windows says:

    Thanks man! saved me a ton of anxiety … add me to the long list of people whom u’d helped :)

    Reply  |  Quote
  65. Ashesh INDIA Mozilla Firefox Windows says:

    Thanks a ton for the help. I too, like others, was trapped by the same mistake. Will remember this for long time now…

    Reply  |  Quote
  66. Itay ISRAEL Mozilla Firefox Windows says:

    GENIOUS!
    Thank you so much! :)
    You are awesome…

    And happy new year! ;)

    Reply  |  Quote
  67. Yaxara ITALY Mozilla Firefox Mac OS says:

    Thanks a lot! I was going mad about wrong figure numbers. Your post is very helpful!

    Reply  |  Quote
  68. Rob GERMANY Mozilla Firefox Windows says:

    Fantastic. Just saved my life.

    Reply  |  Quote
  69. Pierre CANADA Mozilla Firefox Debian GNU/Linux says:

    Ah! Exactly what I was looking for. Thank you so much!!

    Reply  |  Quote
  70. Qichang UNITED STATES Mozilla Firefox Windows says:

    Thank you for saving me another 3 hours or more!!

    Reply  |  Quote
  71. shakes UNITED KINGDOM Internet Explorer Windows says:

    You star!!!Was going bang my head on a wall but you saved it..latex is moody…

    Reply  |  Quote
  72. Daniel NEW ZEALAND Mozilla Firefox Windows says:

    Had the same problem, saved me time, thanks!

    Reply  |  Quote
  73. Pragyan UNITED STATES Internet Explorer Windows says:

    Thank you..You just saved me hours!!

    Reply  |  Quote
  74. Pavel CZECH REPUBLIC Opera Windows says:

    Thank you very much

    Reply  |  Quote
  75. Thomas NORWAY Opera Windows says:

    You made me fix my problem in ten seconds instead of hours. I owe you time!

    Reply  |  Quote
  76. kamikaze DENMARK Mozilla Firefox Ubuntu Linux says:

    You are a good man! Finding lifesaving posts like this one makes me think there is hope for the world after all ;) Thank you!

    Reply  |  Quote
  77. McGyver ITALY Mozilla Firefox SuSE Linux says:

    well… you just have saved a lot of man-hour effort to all of us! thanks!

    Reply  |  Quote
  78. Nobel AUSTRIA Mozilla Firefox Windows says:

    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.

    Reply  |  Quote
  79. Marci HUNGARY Internet Explorer Windows says:

    It was also my problem for a very long time.

    Thank U!

    Reply  |  Quote
  80. GARY UNITED KINGDOM Mozilla Firefox Windows says:

    THANKS! Solve my problem too!!!

    Reply  |  Quote
  81. Erick NETHERLANDS Mozilla Firefox Ubuntu Linux says:

    I’ll join this group of extremely happy people

    THANKS MAN!!

    Reply  |  Quote
  82. Sumit UNITED STATES Mozilla Firefox Ubuntu Linux says:

    Thanks, I was trying to figure out a solution for last three days :-)

    Reply  |  Quote
  83. Eugen GERMANY Mozilla Firefox Windows says:

    Thank you so much for this advice!

    Reply  |  Quote
  84. Jon UNITED STATES Mozilla Firefox Mac OS says:

    Thanks the gods for you and google!

    Reply  |  Quote
  85. GP ITALY Mozilla Firefox Mac OS says:

    Thank you so much!

    Reply  |  Quote
  86. Sofia PORTUGAL Mozilla Firefox Mac OS says:

    Ahah! You really save me some debugging hours!
    Thank u :D

    Reply  |  Quote
  87. Pete AUSTRALIA Opera Linux says:

    Thought I’d chime in with my thanks too…
    Cheers.

    Reply  |  Quote
  88. Emery Spencer JAMAICA Mozilla Firefox Windows says:

    Many thanks. That’s my dissertation saved right there!

    Reply  |  Quote
  89. Gravel POLAND Mozilla Firefox Windows says:

    Hi,
    You saved my life :) This is the exact problem I had with referencing. Solved in 2 min thanks to that post.

    Cheers!

    Reply  |  Quote
  90. JustinMH UNITED STATES Mozilla Firefox Linux says:

    Thank you! It saved me too. Why hasn’t this bug been squashed?

    Reply  |  Quote
  91. Rick ITALY Mozilla Firefox Ubuntu Linux says:

    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” :-)

    Reply  |  Quote
  92. Dimitris GREECE Mozilla Firefox Debian GNU/Linux says:

    A big THANK YOU from a PhD student in Greece :-)

    Reply  |  Quote
  93. Y UNITED STATES Mozilla Firefox Debian GNU/Linux says:

    Thanks a lot!
    Have a nice day!

    Reply  |  Quote
  94. phil_s_stein UNITED STATES Mozilla Firefox Linux says:

    You just saved me 3 hours. Holy crap I love the internet! Thanks!

    Reply  |  Quote
  95. Daniel, Sweden SWEDEN Mozilla Firefox Ubuntu Linux says:

    Here is another soul that you saved!

    Reply  |  Quote
  96. Davis Holmes CANADA Mozilla Firefox Fedora Linux says:

    Holy crap. You are a saint! Thank you so much!

    Reply  |  Quote
  97. Juan Ma MEXICO Mozilla Firefox Ubuntu Linux says:

    Thank you very, very much! I was so desperate that I’ve changed manually the index of almost all figures. thanx a lot!.

    Reply  |  Quote
  98. markusN ITALY Mozilla Firefox Linux says:

    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…

    Reply  |  Quote
  99. Amazing UNITED STATES Safari Mac OS says:

    Man – this is the gift that keeps on giving. Thanks!!!!

    Reply  |  Quote
  100. Thank you UNITED STATES Mozilla Firefox RedHat Linux says:

    Just another thank you ;). Keep \label and \caption within the same block… don’t split them.

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>