1

I have a large article with many sections and many figures. Generally, I use per section figure numbering,

\numberwithin{figure}{section}

In one particularly large section I'd like to use per-subsection figure numbering. Can somebody help me with that?

Thanks!

UPDATE:

I've accepted the answer by Zarko but I may have spoken too soon. I've modified the provided example a little to illustrate what I would like to achieve (see where it says 'incorrect number'):

\documentclass{article}
\usepackage[vmargin=10mm]{geometry}
\usepackage{amsmath}
\numberwithin{table}{section}
\setcounter{topnumber}{8}
\setcounter{bottomnumber}{8}
\setcounter{totalnumber}{8}

\begin{document}

\section{Long section, with subsections}
some text
\subsection{Subsection 1.1}
some text
\begin{table}[htb]
\caption{Table 1.1}
\end{table}
some text some text some text some text 
\subsection{Subsection 1.2}
some text
\begin{table}[htb]
\caption{Table 1.2}
\end{table}
some text some text some text some text  

\section{Long section, with subsections}
\numberwithin{table}{subsection}
some text
\subsection{Subsection 2.1}
some text
\begin{table}[htb]
\caption{Table 2.1.1}
\end{table}
some text some text some text some text 
\subsection{Subsection 2.2}
some text
\begin{table}[htb]
\caption{Table 2.2.1}
\end{table}
some text some text some text some text  

\section{Long section, with subsections}
\numberwithin{table}{section}
some text
\subsection{Subsection 3.1}
some text some text some text some text some text some text
\begin{table}[htb]
\caption{Table 3.1}
\end{table}
\subsection{Subsection 3.2}
some text some text some text some text some text some text 
\begin{table}[htb]
\caption{Table 3.2 - Incorrect Number}
\end{table}

\end{document}

enter image description here

1
  • This seems (to me) to be a bug in \numberwithin mechanism. See edited answer.
    – Zarko
    Aug 13, 2021 at 13:45

1 Answer 1

0

Welcome to TeX.SE!

After section title, in which you like to change floats numbering insert

\numberwithin{figure}{subsection}
\numberwithin{table}{subsection}

and after section title, where you like to return numbering style, insert

\numberwithin{figure}{section}`
\numberwithin{table}{section}` 

For example:

\documentclass{article}
\usepackage[vmargin=10mm]{geometry}
\usepackage{amsmath}
\numberwithin{table}{section}
\usepackage{lipsum}
\setcounter{topnumber}{8}
\setcounter{bottomnumber}{8}
\setcounter{totalnumber}{8}

\begin{document}
\section{Short section}
some text some text 
\begin{table}[htb]
\caption{Table 1.1}
\end{table}
some text some text 
\begin{table}[htb]
\caption{Table 1.2}
\end{table}
some text some text some text some 

\section{Long section, with subsections}
\numberwithin{table}{subsection}
some text
\subsection{Subsection 2.1}
some text
\begin{table}[htb]
\caption{Table 2.1.1}
\end{table}
some text some text some text some text 
\subsection{Subsection 2.2}
some text
\begin{table}[htb]
\caption{Table 2.2.2}
\end{table}
some text some text some text some text  

\section{Short section}
\numberwithin{table}{section}

some text some text some text some text some text some text
\begin{table}[htb]
\caption{Table 3.1}
\end{table}
some text some text some text some text some text some text 
\end{document}

enter image description here

Edit:

Unfortunately above solution works fine as long as following section, where numbering of floats is return back to preset way. In cases, those sections has sub-sections, there float counters (figure, table) are reset after each subsection (as shoed in edited question).

A way to overcome this malfunction, is better to use chngcntr package as is shown in the next MWE:

\documentclass{article}
\usepackage[vmargin=10mm]{geometry}
\usepackage{chngcntr}
\counterwithin{table}{section}  % <------
\counterwithin{figure}{section} % <------
% for enables up to 11 floats per page
\setcounter{topnumber}{11}\setcounter{bottomnumber}{11}\setcounter{totalnumber}{11}

\begin{document}
\section{Short section}
some text some text
\begin{table}[htb]
\caption{Table 1.1}
\end{table}
some text some text
\begin{table}[htb]
\caption{Table 1.2}
\end{table}
some text some text some text some

\section{Long section, with subsections}
\counterwithin{table}{subsection}  % <------
\counterwithin{figure}{subsection} % <------


\subsection{Subsection 2.1}
some text
\begin{table}[htb]
\caption{Table 2.1.1}
\end{table}
some text some text some text some text
\subsection{Subsection 2.2}
some text
\begin{table}[htb]
\caption{Table 2.2.1}
\end{table}
some text some text some text some text

\begin{table}[htb]
\caption{Table 2.2.2}
\end{table}

\section{Short section again}
\counterwithout{table}{subsection}  % <------
\counterwithout{figure}{subsection} % <------
\counterwithin{table}{section}  % <------
\counterwithin{figure}{section} % <------


\begin{table}[htb]
\caption{Table 3.1}
\end{table}
some text some text some text some text some text some text
\begin{table}[htb]
\caption{Table 3.2}

\subsection{Subsection 3.1}
\end{table}
\begin{table}[htb]
\caption{Table 3.3}
\end{table}
\begin{table}[htb]
\caption{Table 3.4}
\end{table}

\subsection{Subsection 3.2}
\begin{table}[htb]
\caption{Table 3.5}
\end{table}
\end{document}

enter image description here

3
  • worked perfectly, thank you very much! I didn't realize it was so easy :)
    – mpr
    Aug 12, 2021 at 11:21
  • thanks for the updated answer! that does exactly what I was looking for
    – mpr
    Aug 13, 2021 at 14:51
  • @mpr, I'm glad that now it works. I need some time to find better alternative ;-)
    – Zarko
    Aug 13, 2021 at 14:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .