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

2. Luke 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?

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

4. Luke says:

Thanks! I will check it out.

5. Jack says:

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

-Jack

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

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

8. Luke says:

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

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

10. Steven says:

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

11. Ben says:

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

12. gehho 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!

13. marve 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!

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

15. Gabrief says:

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

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

17. Marcel 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… 18. Mike 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. 19. Leo 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. 20. Luca says: Thanks man, you saved me a lot of troubles and time.. Two days before my Master’s dissertation deadline! A big thanks, Luca 21. Rodolphe Courtier says: Like everyone else, THANKS SO MUCH FOR FIGURING THAT OUT AND POSTING IT. I was on the brink of insanity. 22. Natalie Schluter says: Thank you, thank you, thank you. I was hard-coding the references out of desperation. You are awesome! 23. iontic says: thanks! great post, and great explaination 24. Robert 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! 25. JohnFlux says: You saved me hours! 26. Leela says: Thank you! That is all…. 27. Jim says: CRAP. Thanks a lot!! 28. Tomas 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 =) 29. 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. :) 30. LD says: WOO! Thanks Luke! That took 10 minutes instead of 3 hours! 31. Luite 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” ! 32. Shannon says: Dude you are awsome. I have 45 minutes to turn in my thesis and you just saved my ass 33. Zeeshan 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 :)

34. Emil says:

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

35. Bayes Bloomington 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.

36. grace says:

omg thank you YOU!

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

38. Eirik says:

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

39. Kim says:

Best Help EVER!!!!!!

40. Pankaj says:

You saved my life dude…thanks a ton

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

42. says:

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

43. Sebastian says:

you saved me !!!
thanks a lot!

44. Christian says:

Thanks :-)

Saved me a lot frustration :-)

45. Galiya says:

Thank you very much from saving me!

46. Michael says:

Thanks for this Post!

47. izzet says:

god bless you :))

48. Speed says:

Yeah, cheers dude!

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

49. Rob Egginton 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.

50. Saved! 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….

51. Manuela says:

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

52. Knut says:

Thanks! That helped me a lot :-)

53. Kristian says:

I love you man, you saved my day!

54. javier says:

thanks from a desperate student!!

55. Shida 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}
56. firas says:

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

THANK YOU

57. Hossein says:

Thanks a lot dude! It was wonderful.

This bug is ridiculous. Why would latex do that?

Thanks again :)

58. Pedro Ribeiro says:

Thanks from Brazil! God bless you.

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

60. Carol says:

You the man!

61. says:

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

62. Joe says:

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

63. centguy 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 !!

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

65. Ashesh says:

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

66. Itay says:

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

And happy new year! ;)

67. Yaxara says:

68. Rob says:

Fantastic. Just saved my life.

69. Pierre says:

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

70. Qichang says:

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

71. shakes says:

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

72. Daniel says:

Had the same problem, saved me time, thanks!

73. Pragyan says:

Thank you..You just saved me hours!!

74. Pavel says:

Thank you very much

75. Thomas says:

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

76. kamikaze 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!

77. McGyver says:

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

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

79. Marci says:

It was also my problem for a very long time.

Thank U!

80. GARY says:

THANKS! Solve my problem too!!!

81. Erick says:

I’ll join this group of extremely happy people

THANKS MAN!!

82. Sumit says:

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

83. Eugen says:

Thank you so much for this advice!

84. Jon says:

Thanks the gods for you and google!

85. GP says:

Thank you so much!

86. Sofia says:

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

87. Pete says:

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

88. Emery Spencer says:

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

89. Gravel says:

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

Cheers!

90. JustinMH says:

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

91. Rick 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” :-)

92. Dimitris says:

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

93. Y says:

Thanks a lot!
Have a nice day!

94. phil_s_stein says:

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

95. Daniel, Sweden says:

Here is another soul that you saved!

96. Davis Holmes says:

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

97. Juan Ma says:

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

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

99. Amazing says:

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

100. Thank you says:

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