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 regardless of whether it's a good practice or not, my company labels figure numbers by the current section, subsection, or whatever else you happen to be in.
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?
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.

-
- Posts: 87
- Joined: Tue Sep 14, 2010 6:58 pm
Numbering Figures by section.subsection.etc
I didn't include a MWE before because I just wanted to know if there was a simple command. Seeing that there doesn't appear to be an easy answer, I'll give something to play around with.
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"
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
Code, edit and compile here:
\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)}
Last edited by west.logan on Tue Sep 28, 2010 6:57 pm, edited 1 time in total.
Numbering Figures by section.subsection.etc
Until gurus give you a better solution, here's an idea: a custom commands for sectioning units which redefine figure numbering "on-the-fly". E.g.:
Code: Select all
Code, edit and compile here:
\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
Last edited by meho_r on Wed Sep 29, 2010 3:21 pm, edited 3 times in total.
-
- Posts: 87
- Joined: Tue Sep 14, 2010 6:58 pm
Re: Numbering Figures by section.subsection.etc
Thank you very much. I was trying to do something like this but was unable to figure out how.
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.
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
Time has run out for editing. So I did the job.
Best regards and welcome to the board
Thorsten
Best regards and welcome to the board
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
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
I know this thread is 8 years old but i've been struggling with this exact same issue.
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.
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
Code, edit and compile here:
%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: 10344
- Joined: Mon Mar 10, 2008 9:44 pm
Numbering Figures by section.subsection.etc
Hi Pete,
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
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
LaTeX.org admin
-
- Posts: 2
- Joined: Thu May 31, 2018 10:55 pm
Numbering Figures by section.subsection.etc
No problem !
The solution to the list-of-tables "number over-running the caption" was to do this:
It simply outputs the table's number, a small gap and then the table's caption.......pagenum
All sorted!
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!