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.



256 Responses to LaTex: Fixing Wrong Figure Numbers

  1. Thx AUSTRIA Opera Linux says:

    Thank you very much, saved me a lot of time :D

    Reply  |  Quote
  2. Sören GERMANY Opera Linux says:

    Dude,

    I’ll have to add my blessings to the other comments, too.
    You’re it, man. Thanks a lot!

    Reply  |  Quote
  3. Steve Pike UNITED KINGDOM Safari Mac OS says:

    Thanks for this! Saved me a lot of stress!
    -S

    Reply  |  Quote
  4. Steve Pike UNITED KINGDOM Safari Mac OS says:

    PS- it is also problematic if you put anyhting between the caption and label:

    \caption{Thing I Want To Appear In LOF}
    More of what I want under this image
    \label{fig:thatone}

    Don’t do that either! :)

    Reply  |  Quote
  5. qq SWITZERLAND Google Chrome Linux says:

    Many thanks for this life saver. I’ll go and get the beer for the next 3 hours to celebrate it ;-)

    Reply  |  Quote
  6. doc POLAND Mozilla Firefox Windows says:

    Thanks mate. I love you (no offence).

    Reply  |  Quote
  7. Nina UNITED STATES Mozilla Firefox Mac OS says:

    GOD BLESS YOU.

    That is all.

    Reply  |  Quote
  8. Chris NETHERLANDS Mozilla Firefox Fedora Linux says:

    Thanks! This thing ***really*** bugged me

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

    Saved my life man. Good onya!

    Reply  |  Quote
  10. bad box IRELAND Mozilla Firefox Windows says:

    aaaargh

    thank you. Only 1 hour of my life wasted before I found you.

    Reply  |  Quote
  11. Kommie P. UNITED KINGDOM Mozilla Firefox Windows says:

    Many thanks for this, I had same problem and just couldn’t figure out what’s going on until I found this article. Thanks!

    Reply  |  Quote
  12. Pan GREECE Safari Mac OS says:

    Good God, yes, that’s it! Thank you, thank you, thank you, you saved me from hours of frustration…

    Out of curiosity, why does this happen with the figure environment? Has anyone figured this out?

    Reply  |  Quote
  13. Dave UNITED KINGDOM Mozilla Firefox Windows says:

    Legend! I can finally go to bed now

    Reply  |  Quote
  14. Guy Gur-Ari ISRAEL Mozilla Firefox Windows says:

    It’s 5:30 AM, and you’re my favorite person in the whole world.

    Cheers

    Reply  |  Quote
  15. Chris CHINA Mozilla Firefox Ubuntu Linux says:

    2010 and I’m another person who has just been saved by this post. Thanks :)

    Reply  |  Quote
  16. Anish Muttreja UNITED STATES Mozilla Firefox Mac OS says:

    Thank you! Wish I had searched Google before trying to debug this.

    Reply  |  Quote
  17. Me UNITED STATES Google Chrome Windows says:

    You are just … Awesome!!!

    Reply  |  Quote
  18. Lucas UNITED STATES Mozilla Firefox Windows says:

    I agree with everyone else on this list: thanks for saving me alot of time!

    Reply  |  Quote
  19. Pingback: Let’s Learn LaTex: Part 1 « Terminally Incoherent WordPress

  20. Yves Mozilla Firefox Mac OS says:

    Who ever you are, you made my day (or night)!!! BIG UP!!!

    Reply  |  Quote
  21. Nicolas SWITZERLAND Mozilla Firefox Mac OS says:

    Thank you!
    You saved my day!

    Reply  |  Quote
  22. Simarillion GERMANY Mozilla Firefox Ubuntu Linux says:

    Thx, you made my day

    Reply  |  Quote
  23. joost NETHERLANDS Google Chrome Windows says:

    Great tip!
    Saved me at least the 3 hours it would have cost me :-)

    Reply  |  Quote
  24. Ryan UNITED STATES Safari Mac OS says:

    Thank you so much! May the Lord bless you greatly as you have greatly blessed me with this solution. I am glad that this is such a simple fix, given the understanding that you shared. Thanks again.

    Reply  |  Quote
  25. eve11 UNITED STATES Mozilla Firefox Windows says:

    Another (late-coming) hint: Don’t include the \caption statement or the \label statement within the \centering tag. I had eg:

    \centering{
    \includegraphics{graph.pdf}
    \caption{here is my caption}
    }
    \label{here is my label}

    And it gave me the same labeling problems. So make sure the caption is outside of the centering bracket.

    Reply  |  Quote
  26. James UNITED KINGDOM Mozilla Firefox RedHat Linux says:

    Thanks so much!!

    Reply  |  Quote
  27. Michael UNITED KINGDOM Mozilla Firefox Mac OS says:

    Thank you! In fact, I think I love you.

    Reply  |  Quote
  28. Johanna UNITED STATES Mozilla Firefox Windows says:

    Thank you!!! Sorry about the 3 hours of your life, but look at how many people you have helped!!!!

    Reply  |  Quote
  29. Lou UNITED KINGDOM Mozilla Firefox Ubuntu Linux says:

    Cheers mate!

    Saved me a lot of hassle. Keep up the good work.

    Reply  |  Quote
  30. Squalo AUSTRALIA Google Chrome Mac OS says:

    THANKS SO MUCH! I had this problem for last few weeks and this post saved me! THANKS

    Reply  |  Quote
  31. Eli UNITED STATES Google Chrome Windows says:

    Thanks! This was driving me nuts.

    Reply  |  Quote
  32. THANKS UNITED STATES Google Chrome Windows says:

    thanks man. saved me some serious headache

    Reply  |  Quote
  33. LJ UNITED STATES Mozilla Firefox Mac OS says:

    This bug just cropped up for me (I don’t use LaTeX that much), and this was the first hit on google when I searched “LaTeX Wrong Figure Number”, even though the original post is 3 yrs old. Thanks for the quick fix, it definitely saved me a few hours and plenty of cursing!

    Reply  |  Quote
  34. BA IRELAND Google Chrome Windows says:

    Just to echo previous sentiments…You saved me a chunk of time and hair so thanks very much.

    Keno.

    Reply  |  Quote
  35. David UNITED KINGDOM Mozilla Firefox Ubuntu Linux says:

    Thanks! It sorted my numbering issues, caused by my “well, I do normally put \label and \caption the order way around, but what harm can it do…”!

    Reply  |  Quote
  36. Sanfeliz UNITED STATES Google Chrome Windows says:

    Dude, thank you. period.

    Reply  |  Quote
  37. Mike UNITED KINGDOM Google Chrome Mac OS says:

    Thank you! So glad I read this before wasting any more time. Like you I have several chapters where labels are always after captions, but one chapter where I inexplicably started labelling at the top.

    Reply  |  Quote
  38. mg UNITED STATES Mozilla Firefox Fedora Linux says:

    Thanks man! You definitely saved me some time!

    Reply  |  Quote
  39. David Canada CANADA Google Chrome Linux says:

    THX > 9000 times, this same “bug” goes for the table reference as well.

    Reply  |  Quote
  40. Thank you GERMANY Mozilla Firefox Ubuntu Linux says:

    Thanks again, for saving us the time! So your 3 hours haven’t been totally useless!

    Reply  |  Quote
  41. Confident again ITALY Google Chrome Windows says:

    Stumbled upon your post while googling for the same issue. You really saved me hours of pain. Thanks

    Reply  |  Quote
  42. z0nam REPUBLIC OF KOREA Google Chrome Mac OS says:

    You saved my life!!

    Reply  |  Quote
  43. Marco BRAZIL Google Chrome Windows says:

    Thank you so much!
    Saved my day!

    Reply  |  Quote
  44. kagan UNITED STATES Google Chrome Windows says:

    awesome… u saved my life:)

    Reply  |  Quote
  45. student POLAND Mozilla Firefox Linux says:

    also interesting is where the problem came from. you know something?

    Reply  |  Quote
  46. Steph GERMANY Mozilla Firefox Ubuntu Linux says:

    Thanks mate! You saved me hours of debugging :)

    Reply  |  Quote
  47. Grzegorz POLAND Mozilla Firefox Fedora Linux says:

    Looks like LaTeX is not as awesome, as some insist it is. But is had wonderful users! Many thanks!!

    Reply  |  Quote
  48. Alex UNITED STATES Internet Explorer Windows says:

    Dude, this didn’t work for me. I’m at the wit’s end here. I’ve made all my figures exactly like you say. My figures in sections are numbered Chapter.#, but if figures are below sections (subsections, subsubsections), they are just #, and they start over each chapter, so I’ve got like, 4 “figure 1’s.” Why? I made sure to put \updateCounters in; can’t think of anything else to try.

    Reply  |  Quote
  49. Mahdi Y.D. ISLAMIC REPUBLIC OF IRAN Mozilla Firefox Windows says:

    I found that another reason may cause wrong numbering in tables and figures.
    The caption of figures or tables must not be put between {}, e.g. {\caption{…}}!
    :)

    Reply  |  Quote
  50. Joachim GERMANY Mozilla Firefox Ubuntu Linux says:

    Thanks to your entry being the fourth result on google, this just cost me 5 minutes.
    Thanks!

    Reply  |  Quote
  51. grateful grant UNITED STATES Mozilla Firefox Mac OS says:

    saved me too. thanks a mill!

    Reply  |  Quote
  52. Sandeep UNITED STATES Mozilla Firefox Ubuntu Linux says:

    Thanks buddy, you saved a lot of time for me :)

    Reply  |  Quote
  53. Steve FRANCE Mozilla Firefox Linux says:

    Merci beaucoup for this post from France!

    Reply  |  Quote
  54. Angelos GREECE Mozilla Firefox Windows says:

    Thanks a lot for sharing with us :)

    Reply  |  Quote
  55. Soleng NORWAY Mozilla Firefox Windows says:

    Luke, I love you!

    Reply  |  Quote
  56. Jenn UNITED STATES Mozilla Firefox Windows says:

    Thanks so much =)

    Reply  |  Quote
  57. Amy Mozilla Firefox Windows says:

    Thank you sooo much – this was driving me insane :)

    Reply  |  Quote
  58. Duncan McBryde UNITED KINGDOM Google Chrome Windows says:

    Thanks so much, like so many others here I was about to go crazy and do references manually. Thanks for sharing and making this post easy to find on Google :)

    Reply  |  Quote
  59. william UNITED STATES Google Chrome Mac OS says:

    Thank you very much. I had this problem for all the tables in my thesis (figures were fine) and it was driving me crazy.

    Reply  |  Quote
  60. Domschaap NETHERLANDS Mozilla Firefox Windows says:

    Even in the year 2011, you are a hero: saved my first publication. Google and incoherent blogs are a lethal combination!

    Reply  |  Quote
  61. Matt UNITED STATES Google Chrome Windows says:

    LOL This is an old post, but it just saved me a headache! Thanks!

    Reply  |  Quote
  62. ains BELGIUM Mozilla Firefox Mac OS says:

    you’re my hero!!

    Reply  |  Quote
  63. R Garcia UNITED STATES Google Chrome Windows says:

    Thanks a lot! I spent way too long on this! Fortunately, I fixed it quickly once I found your post.

    Reply  |  Quote
  64. Leonel Google Chrome Windows says:

    Muchas gracias!! a mi también me salvaste!!.

    Reply  |  Quote
  65. Yomna UNITED ARAB EMIRATES Internet Explorer Windows says:

    Seriously I don’t know how to thank you!!! My thesis submission is today, and I’ve never faced this problem b4!!!thought I was going crazy or sth.

    Reply  |  Quote
  66. Andreas GERMANY Mozilla Firefox Windows says:

    Thanks man, you saved my day – and my keyboard!!! :-)

    Reply  |  Quote
  67. Kathy POLAND Mozilla Firefox Linux says:

    This is F***N’ ENORMOUS ERROR IN LATEX!!!!!!!!!!

    Reply  |  Quote
  68. Ah, had exactly the same problem. Thanks for helping out! (and why isn’t this fixed yet in latex, I suppose this is not an intentional ‘feature’ ;))

    Reply  |  Quote
  69. KyleBroflosky GERMANY Mozilla Firefox Windows says:

    found this page after hours of bad keyword searching on google.. thank you!

    Reply  |  Quote
  70. Lucas ITALY Google Chrome Linux says:

    I had been changing all kinds of numbering options for 1+ hours without effect.
    Thanks google and THANK YOU

    Reply  |  Quote
  71. Johannes GERMANY Mozilla Firefox Windows says:

    You just saved my day with this post, although a couple of years old! Thank you very much!

    Reply  |  Quote
  72. Saha FRANCE Mozilla Firefox Linux says:

    Thanks a lot. Now my figure is number 2 instead of 5, they way it should be. :)

    Reply  |  Quote
  73. Magnesium UNITED STATES Google Chrome Mac OS says:

    Thank you! I had label, then centering, then caption….and I swear I have spent the last 20 minutes trying to figure this out.

    Reply  |  Quote
  74. meena Mozilla Firefox Windows says:

    Very useful , thank you

    Reply  |  Quote
  75. Jose UNITED KINGDOM Mozilla Firefox Windows says:

    You are the Mac Daddy bro!

    Reply  |  Quote
  76. Whitney Mozilla Firefox Windows says:

    Thanks a million!

    Reply  |  Quote
  77. CS GERMANY Mozilla Firefox Windows says:

    You saved my life..I’m relieved it works now

    Reply  |  Quote
  78. Piotr FINLAND Google Chrome Linux says:

    Thanks!! It is a mystery for me how I managed to write one thesis without stumbling upon that only to get right into it while working on the second one!

    Reply  |  Quote
  79. L T X NETHERLANDS Mozilla Firefox Windows says:

    Thank you! I don’t know why -after a while working with LaTeX- I decided to put my label first… if not by you, I would have spent hours trying to figure out why my captions were wrong!

    Reply  |  Quote
  80. yakx POLAND Google Chrome Windows says:

    saved my day too ;)

    Reply  |  Quote
  81. Splinter NETHERLANDS Google Chrome Windows says:

    Thank you, you’re my hero!

    Reply  |  Quote
  82. Amir Mozilla Firefox Linux says:

    Thank you, thank you, and thank you…
    I wrote thesis and paper and both of them had the same problem…
    But there are ok now ..

    regards,

    Reply  |  Quote
  83. raul UNITED STATES Mozilla Firefox Windows says:

    Luke,

    Thank you. You fixed my problem!

    Reply  |  Quote
  84. DAMN! QATAR Mozilla Firefox Windows says:

    THANK YOU!

    VERY MUCH!

    Reply  |  Quote
  85. Marcin POLAND Google Chrome Windows says:

    God bless internet and LaTex users who share their knowledge.

    Reply  |  Quote
  86. Jassel GERMANY Safari Mac OS says:

    This was absolutely the best find ever – thank you, you genius.

    Reply  |  Quote
  87. Klame FRANCE Google Chrome Mac OS says:

    Four years after and your post is still helping =)

    Thanks for sharing !

    Reply  |  Quote
  88. Rodrigo CANADA Mozilla Firefox Windows says:

    We’re in 2012 and you’re still saving people’s soul. Thanks! :D

    Reply  |  Quote
  89. Cornelius GERMANY Safari Mac OS says:

    thanks for sharing man I had the exact same problem.

    Reply  |  Quote
  90. amir CANADA Mozilla Firefox Windows says:

    Thanks man, your certainly saved me on my upcoming paper.

    Reply  |  Quote
  91. Kinh Kha JAPAN Mozilla Firefox Windows says:

    I am compiling a *.tex file with some figures labeled by \label{f3}, \label{f3}, ….
    After compiled, Figs are numbered as Fig. 1, Fig. 2, Fig. 3, and Fig. 5, no Fig. 4. :( what is this problem? Please tell me more. Thank you so much.
    (In Fig. 3, I have 4 subfigures labeled by f3a, f3b…, so whenever I call Fig. \ref{f3a} I will get Fig. 3a. And If I call Fig. \ref{f3} I will have Fig. 3).

    Reply  |  Quote
  92. sp vanilla SWITZERLAND Mozilla Firefox Fedora Linux says:

    Saved us from hours of madness and going berserk

    Reply  |  Quote
  93. Ishrat Mozilla Firefox Windows says:

    Thanks a lot… indeed a great help :)

    Reply  |  Quote
  94. Jacek SWITZERLAND Mozilla Firefox Windows says:

    You know what is wrong with LateX? You have remember so much detail and little issues to get it working. This is my story:

    I have encountered this error about half a year ago, resolved it, but stopped writing my thesis since I had to make some programming stuff. Now I wanted to return to writing and thought: Hey why these labels are BELOW objects. How idiotic it is. And moved all the labels above. Of course, as you can guess, everything messed up…

    I started to look for a solution and found this blog post AGAIN. One of the many LateX ewww’s it is…

    Reply  |  Quote
  95. adirangan UNITED STATES Mozilla Firefox Windows says:

    THANKS! saved me untold hours of frustration :)

    Reply  |  Quote
  96. Martin UNITED STATES Google Chrome Mac OS says:

    Thanks for posting this – Just saved me a lot of frustration!

    Reply  |  Quote
  97. Jacques GERMANY Mozilla Firefox Windows says:

    Haha, thanks! Guess you made me save a lot of (angry) time…

    Reply  |  Quote
  98. Rach AUSTRALIA Mozilla Firefox Ubuntu Linux says:

    Wow, you seriously just solved a problem that was causing me a lot of pain. Best tip ever!

    Reply  |  Quote
  99. Frank Stein GERMANY Mozilla Firefox Linux says:

    Thank you. You’ve literally saved me a lot of debugging. My apologies go now to Knuth’s mother.

    Reply  |  Quote
  100. Ehsan Mozilla Firefox Windows says:

    Thanks a LOT! u saved me too! like others. ;)

    Reply  |  Quote

Leave a Reply

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