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 Uncategorized. Bookmark the permalink.



256 Responses to LaTex: Fixing Wrong Figure Numbers

  1. drozzy CANADA Google Chrome Linux says:

    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), while Figure \ref{fig:blah} will actually refer to the thing we want (something like Figure 1).

    Reply  |  Quote
  2. aalibash GERMANY Mozilla Firefox Windows says:

    Thanks for saving my three hours!

    Reply  |  Quote
  3. Marry me? CANADA Mozilla Firefox Windows says:

    Well it’s been almost 2 months since your last ‘thank you,’ so let me just say:
    YOU.SAVED.MY.LIFE.

    Thanks man.

    Reply  |  Quote
  4. Tugcem Oral TURKEY Google Chrome Linux says:

    Dude! The one and the only exact solution of all times! Thanks :brofist

    Reply  |  Quote
  5. Luke from Switzerland SWITZERLAND Mozilla Firefox Windows says:

    A big thanks to you sir – this post saved my day.

    Reply  |  Quote
  6. Antonio Rezende Neto BRAZIL Google Chrome Windows says:

    Thank you very much! You have saved my life… lol!

    Reply  |  Quote
  7. Hamed CANADA Mozilla Firefox Windows says:

    Thank you dude! You saved me!! :D

    Reply  |  Quote
  8. Ivan BRAZIL Mozilla Firefox Windows says:

    Just to let you know: it still helps people like me a loooot! thank you very very much!

    Reply  |  Quote
  9. Srikanth INDIA Mozilla Firefox Windows says:

    Thanks mate, saved me some time :)

    Reply  |  Quote
  10. Richard Mozilla Firefox Linux says:

    Thank you! Think you’ve just saved me from hours of frustration!

    Reply  |  Quote
  11. Katherine UNITED STATES Google Chrome Windows says:

    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.

    Reply  |  Quote
  12. Katherine UNITED STATES Google Chrome Windows says:

    Actually, I just figured it out. Sorry about that. Thanks, though.

    Reply  |  Quote
  13. Celes UNITED STATES Mozilla Firefox Mac OS says:

    You’re a life saver and I think I love you! I was going crazy trying to figure out what was wrong.

    Reply  |  Quote
  14. Angel THE FORMER YUGOSLAV REPUBLIC OF MACEDONIA Google Chrome Windows says:

    +1
    Thanks a bunch!
    Cheers

    Reply  |  Quote
  15. Eric UNITED STATES Google Chrome Mac OS says:

    70 minutes before a manuscript submission deadline, you are my savior.

    Reply  |  Quote
  16. Thank You AUSTRIA Google Chrome Mac OS says:

    :) You are a lifesaver!

    Reply  |  Quote
  17. Doug Google Chrome Mac OS says:

    Thanks bro! You help me a lot with your post. Ty Ty Ty

    Reply  |  Quote
  18. Tue Chasin DENMARK Internet Explorer Windows says:

    I’ll join the long list:
    Thank you SO MUCH for this post!
    You can add 1 to your list of lives saved… :)

    Reply  |  Quote
  19. ejd UNITED STATES Mozilla Firefox Mac OS says:

    WOW!! Super awesome. Many, many thanks! ^_^

    Reply  |  Quote
  20. Sam UNITED KINGDOM Safari Mac OS says:

    Looks like it’s been a while since this page saved someone… but it just saved me!

    Thanks very much!

    Reply  |  Quote
  21. Tom UNITED STATES Google Chrome Linux says:

    Wow, great post. This makes me wonder what people did in the days before huge quantities of advice was available and easily searchable.

    Reply  |  Quote
  22. Alex AUSTRALIA Safari Mac OS says:

    Thanks mate! that was an awesome post… Spent the last couple of day trying to fix it but finally got it done! thanks!!

    Reply  |  Quote
  23. Roi ISRAEL Mozilla Firefox Windows says:

    Thanks from Israel! Very helpful!

    Reply  |  Quote
  24. katja GERMANY Google Chrome Mac OS says:

    GREAT! Many, many thanks!
    Had the same problem just now and thought I would have to spent the 3 or more hours, too.

    Reply  |  Quote
  25. sh ISRAEL Mozilla Firefox Ubuntu Linux says:

    THANK YOU.
    life is a bit better now.

    Reply  |  Quote
  26. R. Shankar UNITED STATES Google Chrome Windows says:

    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.

    Reply  |  Quote
  27. Vinay Ramasesh UNITED STATES Mozilla Firefox Windows says:

    Thanks, helped save time for my Master’s thesis!

    Reply  |  Quote
  28. Freeo Google Chrome Windows says:

    Thank you! Saved me 3 hours obviously :) Your hint changed my 4.1.1 labeled figure into a correct 4.1 in the text.

    Reply  |  Quote
  29. mbnz NEW ZEALAND Google Chrome Mac OS says:

    THANK YOU

    Reply  |  Quote
  30. Wouter NETHERLANDS Google Chrome Windows says:

    Thanks a lot! Saved at least three hours from my life

    Reply  |  Quote
  31. luca ITALY Mozilla Firefox Ubuntu Linux says:

    YOU SAVED MY TIME!!! THANK YOU!!!

    Reply  |  Quote
  32. Lucas Google Chrome Windows says:

    I know it has been 6 years since this post but you still helped me!
    Thank you!

    Reply  |  Quote
  33. Joao PORTUGAL Google Chrome Windows says:

    Helping people since 2007… lol Thanks!

    Reply  |  Quote
  34. Brenno UNITED STATES Google Chrome Windows says:

    \caption{you saved my life}
    \label{fig:this latex glitch almost made me crap my pants}
    \comment{thank you}

    Reply  |  Quote
  35. Brenno UNITED STATES Google Chrome Windows says:


    \begin{figure}[!htbp]
    \centering
    \caption{You saved my life as well}
    \label{fig:thank you so much}
    \end{figure}

    Reply  |  Quote
  36. Nick Mozilla Firefox Windows says:

    Dude, 5 years on and you just saved a South African undergrad from doing what you described. Big up.

    Reply  |  Quote
  37. Edwin UNITED STATES Mozilla Firefox Windows says:

    Needless to say, thank you! You saved me so many hours of debugging!

    Reply  |  Quote
  38. Girma AUSTRALIA Google Chrome Windows says:

    THANK YOU
    THANK YOU
    THANK YOU

    I found your tip after wasting an hour. Thanks for not wasting more hours.

    Reply  |  Quote
  39. Still relevant UNITED STATES Google Chrome Mac OS says:

    I was fighting this problem for a half hour until just finding this post. Thanks.

    Reply  |  Quote
  40. Manolito SPAIN Google Chrome Windows says:

    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.

    Reply  |  Quote
  41. Manish Sharma UNITED STATES Google Chrome Windows says:

    Thanks man! You saved my time :-)

    Reply  |  Quote
  42. Matt UNITED STATES Safari Mac OS says:

    Thank you! You’re 3 hour struggle has turned into a 3 minute google search. You’re a lifesaver.

    Reply  |  Quote
  43. Ajith Google Chrome Windows says:

    7 years on, you continue to make our life easy!!! Thanks again.

    Reply  |  Quote
  44. kay GERMANY Mozilla Firefox Windows says:

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

    Reply  |  Quote
  45. meg UNITED STATES Safari Mac OS says:

    Thanks a million!!!

    Reply  |  Quote
  46. Henry C. UNITED STATES Safari Mac OS says:

    Thanks very much, 3 hours of my life has indeed been saved!

    Reply  |  Quote
  47. brian UNITED STATES Google Chrome Mac OS says:

    Thank you, saved me a lot of time editing my thesis.

    Reply  |  Quote
  48. Marek NORWAY Google Chrome Windows says:

    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

    Reply  |  Quote
  49. Ben UNITED STATES Mozilla Firefox Windows says:

    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.

    Reply  |  Quote
  50. anon SPAIN Mozilla Firefox Ubuntu Linux says:

    @ Luke:
    It’s 2015 now and you saved me :D

    Reply  |  Quote
  51. EB UNITED KINGDOM Internet Explorer Windows says:

    Thanks for posting this and for highlighting the solution. Saved me quite some time….

    Reply  |  Quote
  52. Tim CANADA Google Chrome Linux says:

    Thanks! You just saved me hours and hours of frustration.

    Reply  |  Quote
  53. Bruno CHILE Mozilla Firefox Fedora Linux says:

    You have just saved me those three hours of suffering and reduced them to fifteen minutes. Thanks!

    Reply  |  Quote
  54. Neil CANADA Mozilla Firefox Mac OS says:

    A lot of debugging saved with this post.

    Reply  |  Quote
  55. Martin CZECH REPUBLIC Google Chrome Windows says:

    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!

    Reply  |  Quote
  56. jmatsubara UNITED STATES Google Chrome Mac OS says:

    9 years later, this is exactly the problem i was having, and you have spared me the 3+ hours. thank you!

    Reply  |  Quote

Leave a Reply

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