Graphics, Figures & Tables ⇒ Numbering Figures by section.subsection.etc
-
- Posts: 87
- Joined: Tue Sep 14, 2010 6:58 pm
Numbering Figures by section.subsection.etc
So if you're working in
3.1.3, the figure would be 3.1.3-1
And if you go down a level to
3.1.3.1, the figure number would be 3.1.3.1-1
Up until now this has all been entirely manual. I would like to be able to have the writer simply place a figure and it automatically see what section, subsection, subsubsection, etc., that they are in and number the figure accordingly.
I've messed around with
\renewcommand{\thefigure}{\thesection-\arabic{figure}}
and other variants, but the obvious problem is that it only does the section number, not subsection. Then if I try to incorporate the subsection number, any figure landing in a section number will show up as 3.0-1, which is not what I want either. So it needs to somehow look at the correct counter.
I also tried re-defining the \section, \subsection, etc., commands by having them redefine the figure number whenever they are called, i.e. if you call a \subsubsection then there would be a
\renewcommand{\thefigure}{\thesection.\thesubsection.\thesubsubsection-\arabic{figure}}
but I'm not adept at that and it didn't seem to work. Are there any suggestions or fixes for this?
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
-
- Posts: 87
- Joined: Tue Sep 14, 2010 6:58 pm
Numbering Figures by section.subsection.etc
I have one command at the top that makes the first figure how it should be, but not any subsequent ones. Of course any testing will require an arbitrary figure named "test.png"
Code: Select all
\documentclass{article}
\usepackage[pdftex]{graphicx} %to use images and figures
%Make section number before figure number.
\renewcommand{\thefigure}{\thesection-\arabic{figure}}
\begin{document}
\section{Section 1}
\begin{figure}[h] %Should be Figure 1-1
\begin{center}
\includegraphics[width=120mm]{test.png}
\end{center}
\caption{(Should say Figure 1-1)}
\end{figure}
\newpage
\subsection{Subsection}
\begin{figure}[h] %Should be Figure 1.1-1
\begin{center}
\includegraphics[width=120mm]{test.png}
\end{center}
\caption{(Should say Figure 1.1-1)}
\end{figure}
\newpage
\subsubsection{Subsubsection}
\begin{figure}[h] %Should be Figure 1.1.1-1
\begin{center}
\includegraphics[width=120mm]{test.png}
\end{center}
\caption{(Should say Figure 1.1.1-1)}
\end{figure}
\newpage
\section{Section 2}
\begin{figure}[h] %Should be Figure 2-1
\begin{center}
\includegraphics[width=120mm]{test.png}
\end{center}
\caption{(Should say Figure 2-1)}
\end{figure}
\end{document}
Numbering Figures by section.subsection.etc
Code: Select all
\documentclass{article}
\usepackage[pdftex,demo]{graphicx} %to use images and figures
%Make section number before figure number.
\newcommand{\mysection}[2][]{\section[#1]{#2}\setcounter{figure}{0}\renewcommand{\thefigure}{\thesection-\arabic{figure}}}
\newcommand{\mysubsection}[2][]{\subsection[#1]{#2}\setcounter{figure}{0}\renewcommand{\thefigure}{\thesubsection-\arabic{figure}}}
\newcommand{\mysubsubsection}[2][]{\subsubsection[#1]{#2}\setcounter{figure}{0}\renewcommand{\thefigure}{\thesubsubsection-\arabic{figure}}}
\begin{document}
\mysection{Optional Title}{Section 1}
\begin{figure}[h] %Should be Figure 1-1
\begin{center}
\includegraphics[width=120mm]{test.png}
\end{center}
\caption{(Should say Figure 1-1)}
\end{figure}
\newpage
\mysubsection{Subsection}
\begin{figure}[h] %Should be Figure 1.1-1
\begin{center}
\includegraphics[width=120mm]{test.png}
\end{center}
\caption{(Should say Figure 1.1-1)}
\end{figure}
\newpage
\mysubsubsection{Subsubsection}
\begin{figure}[h] %Should be Figure 1.1.1-1
\begin{center}
\includegraphics[width=120mm]{test.png}
\end{center}
\caption{(Should say Figure 1.1.1-1)}
\end{figure}
\newpage
\mysection{Section 2}
\begin{figure}[h] %Should be Figure 2-1
\begin{center}
\includegraphics[width=120mm]{test.png}
\end{center}
\caption{(Should say Figure 2-1)}
\end{figure}
\end{document}
-
- Posts: 87
- Joined: Tue Sep 14, 2010 6:58 pm
Re: Numbering Figures by section.subsection.etc
This will certainly work, though if there is a more elegant solution I would be glad to hear it.
Thorsten:
Before you say something, I apparently cannot edit my first post to mark it with a green checkmark. I can edit both this and my other post but not the original.
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Re: Numbering Figures by section.subsection.etc
Best regards and welcome to the board
Thorsten
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
-
- Posts: 2
- Joined: Thu May 31, 2018 10:55 pm
Numbering Figures by section.subsection.etc
Finally found a solution.
I'm using LyX so I put this into the preamble, but whatever...
The only minor issue I see is that the list of tables gets the spacing a bit wrong for the longest sub-sub section tables...but I'm sure I can fix that. Hope this helps someone.
Code: Select all
%use these 2 lines to get tables and figures to restart at 1 in subsub sections
\counterwithin{table}{subsubsection}
\counterwithin{figure}{subsubsection}
%now get the correct section based on whether the next heirachical section number "down" is 0 (credit to yo' on stackexchange)
\newcommand{\getCurrentSectionNumber}{%
\ifnum\c@section=0 %
\thechapter
\else
\ifnum\c@subsection=0 %
\thesection
\else
\ifnum\c@subsubsection=0 %
\thesubsection
\else
\thesubsubsection
\fi
\fi
\fi
}
%finally redefine the display for tables and figures to use the correct section. 1-1, 2.1-1, 3.2.1-1 etc
\renewcommand\thetable{\getCurrentSectionNumber-\arabic{table}}
\renewcommand\thefigure{\getCurrentSectionNumber-\arabic{figure}}
- Stefan Kottwitz
- Site Admin
- Posts: 10330
- Joined: Mon Mar 10, 2008 9:44 pm
Numbering Figures by section.subsection.etc
welcome to the forum!
Thank you very much for taking the time and writing up your solution here. For sure it will help somebody who comes in searching via google. The solution you posted seems better than the other one posted earlier.
Thanks!
Stefan
-
- Posts: 2
- Joined: Thu May 31, 2018 10:55 pm
Numbering Figures by section.subsection.etc
The solution to the list-of-tables "number over-running the caption" was to do this:
Code: Select all
\renewcommand{\numberline}[1]{#1 \,}
All sorted!