## 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"

1. \documentclass{article}
2. \usepackage[pdftex]{graphicx} %to use images and figures
3.
4. %Make section number before figure number.
5. \renewcommand{\thefigure}{\thesection-\arabic{figure}}
6.
7. \begin{document}
8.
9. \section{Section 1}
10. \begin{figure}[h] %Should be Figure 1-1
11. \begin{center}
12. \includegraphics[width=120mm]{test.png}
13. \end{center}
14. \caption{(Should say Figure 1-1)}
15. \end{figure}
16.
17. \newpage
18. \subsection{Subsection}
19. \begin{figure}[h] %Should be Figure 1.1-1
20. \begin{center}
21. \includegraphics[width=120mm]{test.png}
22. \end{center}
23. \caption{(Should say Figure 1.1-1)}
24. \end{figure}
25.
26. \newpage
27. \subsubsection{Subsubsection}
28. \begin{figure}[h] %Should be Figure 1.1.1-1
29. \begin{center}
30. \includegraphics[width=120mm]{test.png}
31. \end{center}
32. \caption{(Should say Figure 1.1.1-1)}
33. \end{figure}
34.
35. \newpage
36. \section{Section 2}
37. \begin{figure}[h] %Should be Figure 2-1
38. \begin{center}
39. \includegraphics[width=120mm]{test.png}
40. \end{center}
41. \caption{(Should say Figure 2-1)}
42. \end{figure}
43.
44. \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.:
1. \documentclass{article}
2. \usepackage[pdftex,demo]{graphicx} %to use images and figures
3.
4. %Make section number before figure number.
5.
7.
9.
11.
12.
13. \begin{document}
14.
15. \mysection{Optional Title}{Section 1}
16. \begin{figure}[h] %Should be Figure 1-1
17. \begin{center}
18. \includegraphics[width=120mm]{test.png}
19. \end{center}
20. \caption{(Should say Figure 1-1)}
21. \end{figure}
22.
23. \newpage
24. \mysubsection{Subsection}
25. \begin{figure}[h] %Should be Figure 1.1-1
26. \begin{center}
27. \includegraphics[width=120mm]{test.png}
28. \end{center}
29. \caption{(Should say Figure 1.1-1)}
30. \end{figure}
31.
32. \newpage
33. \mysubsubsection{Subsubsection}
34. \begin{figure}[h] %Should be Figure 1.1.1-1
35. \begin{center}
36. \includegraphics[width=120mm]{test.png}
37. \end{center}
38. \caption{(Should say Figure 1.1.1-1)}
39. \end{figure}
40.
41. \newpage
42. \mysection{Section 2}
43. \begin{figure}[h] %Should be Figure 2-1
44. \begin{center}
45. \includegraphics[width=120mm]{test.png}
46. \end{center}
47. \caption{(Should say Figure 2-1)}
48. \end{figure}
49.
50. \end{document}
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.

1. %use these 2 lines to get tables and figures to restart at 1 in subsub sections
2. \counterwithin{table}{subsubsection}
3. \counterwithin{figure}{subsubsection}
4. %now get the correct section based on whether the next heirachical section number "down" is 0 (credit to yo' on stackexchange)
5. \newcommand{\getCurrentSectionNumber}{%
6. \ifnum\c@section=0 %
7. \thechapter
8. \else
9. \ifnum\c@subsection=0 %
10. \thesection
11. \else
12. \ifnum\c@subsubsection=0 %
13. \thesubsection
14. \else
15. \thesubsubsection
16. \fi
17. \fi
18. \fi
19. }
20. %finally redefine the display for tables and figures to use the correct section. 1-1, 2.1-1, 3.2.1-1 etc
21. \renewcommand\thetable{\getCurrentSectionNumber-\arabic{table}}
22. \renewcommand\thefigure{\getCurrentSectionNumber-\arabic{figure}}

Stefan Kottwitz
Posts: 8953
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:

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

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

### Who is online

Users browsing this forum: No registered users and 12 guests