Graphics, Figures & TablesWeird figure numbering

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Xorlium
Posts: 2
Joined: Wed Jan 09, 2013 10:16 pm

Weird figure numbering

Post by Xorlium »

Hello,

I have no idea what is going on here. I've reduced it to a minimal example:

Code: Select all

\pdfoptionpdfminorversion=5
\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage[demo]{graphicx}

\usepackage{float}

\usepackage{hyperref}

\begin{document}

\section{Intro}
Intro section to show whatever...

\section{Whatever}\label{definitelyNOTouch}

The following should read Figure 1: Figure~\ref{ouch}

But it reads Figure 2!! What is going on???

\begin{figure}[H] \label{ouch}
\begin{center}
\includegraphics{anypic.pdf}
\caption{Some nice caption}
\end{center}

\end{figure}

\end{document}
Just see what happens. I compile it and it calls the figure "figure 1", but it refers to it as "Figure 2"!! And the link points to the section 2, not figure 1.

I've tried cleaning the help files and recompiling thousands of times, so that's not it. I've also tried this in a different computer with a different OS.

Thanks!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Weird figure numbering

Post by Stefan Kottwitz »

Hi,

welcome to the board!

Just use \label after \caption, this order is important. You will see, it will work fine then. \caption prepares an internal macro which can be used by \label. Without it, \label refers to the anchor before, which is the section here, that's why you got the number 2.

By the way, by simply adding the demo option to graphicx you can ensure that people are able to compile it even without the image. I edited the code line above for you. Now readers can just click the "Open" link above the code and see the output. That's why it's great that you posted a minimal example.

Stefan
LaTeX.org admin
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Weird figure numbering

Post by sommerfee »

The \label needs to be placed after \caption, otherwise it will reference to the last command which had produced a reference which is \section{Whatever} here.

See also: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=crossref
Last edited by cgnieder on Wed Jan 09, 2013 11:09 pm, edited 1 time in total.
Xorlium
Posts: 2
Joined: Wed Jan 09, 2013 10:16 pm

Re: Weird figure numbering

Post by Xorlium »

Ah, thanks! It works now :)
sabbaumt
Posts: 1
Joined: Wed Mar 26, 2014 5:10 am

Re: Weird figure numbering

Post by sabbaumt »

\newpage
\section{Electrospun nanofibers as host scaffold for the growth of hierarchical nanostructures}
\begin{figure}[ht]
\centering
\includegraphics[width=0.9\textwidth]{D:/Dharani/Theisis/Thesis/Figures/14.png}
\caption[blah.]{blah.}
\label{fig:14}
\end{figure}

I used the above format and for section 1 it worked but for section 2 it didnt work, it always recalls the image as Fig. 2.2. I dont understand whats going wrong.
Can you please help me?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Weird figure numbering

Post by Johannes_B »

Impossible to tell the reason for the behaviour described. The safest way to find the problem cause, is to prepare a minimal working example and come back here.

Another thing, do you know the difference between \newpage and \clearpage? Maybe the latter is of more use for you.

Best regards
Johannes

BTW: Please insert code into the code-environment, this gives nice markup.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
JB_777
Posts: 1
Joined: Thu Oct 24, 2019 6:46 pm

Weird figure numbering

Post by JB_777 »

I had the same problem you were mentioning before.

I could find a solution by just disabling the \centering option, like this (with the "%" symbol):

\newpage
\section{Electrospun nanofibers as host scaffold for the growth of hierarchical nanostructures}
\begin{figure}[ht]
%\centering
\includegraphics[width=0.9\textwidth]{D:/Dharani/Theisis/Thesis/Figures/14.png}
\caption[blah.]{blah.}
\label{fig:14}
\end{figure}

It worked perfectly to me. All the references and ordinal numbers for my figures were right!
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Weird figure numbering

Post by Ijon Tichy »

:?: \centering does not influence labels or references. Please show a Infominimal working example.

BTW: You should not use the center environment inside a float like figure or table, because it does add extra vertical space. An indeed if you would use center, the \label has to be in the same group level as the \caption.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Post Reply