## LaTeX forum ⇒ Graphics, Figures & Tables ⇒ Numbering Figures by section.subsection.etc Topic is solved

Information and discussion about graphics, figures & tables in LaTeX documents.
west.logan
Posts: 85
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?

west.logan
Posts: 85
Joined: Tue Sep 14, 2010 6:58 pm
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"

\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}
Last edited by west.logan on Tue Sep 28, 2010 6:57 pm, edited 1 time in total.

meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm
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.:
Last edited by meho_r on Wed Sep 29, 2010 3:21 pm, edited 3 times in total.

west.logan
Posts: 85
Joined: Tue Sep 14, 2010 6:58 pm
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.

localghost
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Time has run out for editing. So I did the job.

Best regards and welcome to the board
Thorsten
LaTeX Community Moderator

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

Pete Sleeman
Posts: 2
Joined: Thu May 31, 2018 10:55 pm
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.


Stefan Kottwitz
Posts: 9019
Joined: Mon Mar 10, 2008 9:44 pm
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

Pete Sleeman
Posts: 2
Joined: Thu May 31, 2018 10:55 pm
No problem !
The solution to the list-of-tables "number over-running the caption" was to do this:

\renewcommand{\numberline}[1]{#1 \,}

It simply outputs the table's number, a small gap and then the table's caption.......pagenum
All sorted!