1

I am using the following code:

\documentclass[10pt,onecolumn,twoside,draftcls]{article}
\usepackage[T1]{fontenc}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{amsbsy}
\usepackage{placeins}

\let\Oldsection\section
\renewcommand{\section}{\FloatBarrier\Oldsection}
\let\Oldsubsection\subsection
\renewcommand{\subsection}{\FloatBarrier\Oldsubsection}
\let\Oldsubsubsection\subsubsection
\renewcommand{\subsubsection}{\FloatBarrier\Oldsubsubsection}

\usepackage{datatool}
\usepackage{url}
\usepackage{graphicx}
\usepackage{graphics}
\usepackage{epstopdf}
\usepackage{algorithm}
\usepackage{bm}
\usepackage{algorithmic}
\usepackage{subfigure}
\usepackage{cite}
\usepackage{multirow}
\usepackage{pgfplotstable,filecontents}
\usepackage{ctable}
\usepackage{enumitem}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage[pagewise]{lineno}

\renewcommand*\familydefault{\sfdefault} 

\begin{document}
\listoffigures
\listoftables

See Figs.~\ref{fig:1} and~\ref{fig:2} and Tables~\ref{table:1} and~\ref{table:2}.

\section{section 1}
\section{section 2} \label{sec4}
\begin{table}[H] \label{table:1} 
    \caption{table 1 caption}
\end{table}
\begin{figure} [H]\label{fig:1}
    \caption{fig 1 caption}
\end{figure}

\subsection{subsection}
\begin{table}[H] \label{table:2} 
    \caption{table 2 caption}
\end{table}
\begin{figure} [H]\label{fig:2}
    \caption{fig 2 caption}
\end{figure}
\end{document}

When I compile, I see that the table numberings are correct in the table, but wrong when I refer to them. They get the number of their section/subsection. The same thing happens for figure numberings as well:

List of Figures
1 fig 1 caption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 fig 2 caption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
List of Tables
1 table 1 caption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 table 2 caption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
See Figs. 2 and 2.1 and Tables 2 and 2.1.
1 section 1
2 section 2
Table 1: table 1 caption
Figure 1: fig 1 caption
2.1 subsection
Table 2: table 2 caption
Figure 2: fig 2 caption 1

I need that the table numberings become independent of the section/subsection. The numbering starts at 1, and when I add a new table, it increases by 1. The same when I refer to them. I want the same thing for figure numberings as well. Can you help?


I got a comment that the label has to be placed after the caption. I did that but nothing changed. The same issue is there. So, this is not duplicate.


Edit after the second comment: I fixed it. The label has to be placed right after the caption. What I did and did not work was placing the label after caption, but it was not right after the caption. It was after the \end{figure} or \end{table}. When I put it right after the caption, it works.

Albert
  • 111
  • 4
    Welcome to tex.sx. You've got a common problem -- the \label commands are input before the \caption for each table. It's the caption that resets the numbering, so placing the label before the caption will refer to whatever the previous element was that has a referenceable number. (This is certainly a duplicate; I'll look for a suitable one.) – barbara beeton Nov 28 '19 at 19:20
  • 3
  • 1
    @barbarabeeton Thanks. But, this is not the case. Please see my edit above. – Albert Nov 28 '19 at 19:47
  • Did you rerun twice? The label information is written out to the .aux file in the first run, and read back in in the second run. Unless you delete the .aux file(s) first (which will force you to run the job twice, and which I should have mentioned earlier), you will still be getting the "old" values. So the safest approach is to delete the .aux file(s) before rerunning. If this doesn't change anything, something is seriously wrong. – barbara beeton Nov 28 '19 at 19:54
  • @barbarabeeton Yes, I rerun it twice and even deleted the .aux file twice. Nothing changed. – Albert Nov 28 '19 at 20:04
  • 2
    After moving the labels in the posted example, the references are correct. I can't reproduce the problem you report. – barbara beeton Nov 28 '19 at 20:50
  • 2
    what do you mean by "see my edit above" ? Your example clearly shows \label in teh wrong place, before \caption you have \label{fig:1} \caption{fig 1 caption} – David Carlisle Nov 28 '19 at 23:16
  • 2
    note that while it works it is usually bad practice to use a numeric label such as fig:2 as the label has no connection to the printed number and it's confusing to look at the source if Figure 4 has label fig:3 – David Carlisle Nov 28 '19 at 23:18
  • @barbarabeeton What I did and did not work was placing the label after caption, but it was not right after the caption. It was after the \end{figure} or \end{table}. Then, I put it right after the caption and now it works. Thanks a lot. – Albert Nov 29 '19 at 08:54
  • @DavidCarlisle Thanks, David. I realized what I have to do. I have edited my question again. What is your suggestion for labeling? – Albert Nov 29 '19 at 08:55
  • @Albert the label is an internal identifier for the content not the final number, so \label{fermat} then \ref{fermat} will make whatever number fermat's last theorem gets in the final presentation. – David Carlisle Nov 29 '19 at 09:10
  • 1
    @Albert -- It's sometimes recommended to put the label inside the caption, and that also works, but it's not my preference. If there is a list of tables/figures, the caption is used for that, and I'm not sure what effect a label in that position might have in the list. – barbara beeton Nov 29 '19 at 13:08

0 Answers0