Graphics, Figures & TablesNumbering Figures by section.subsection.etc

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Numbering Figures by section.subsection.etc

Post by west.logan »

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?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

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

west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Numbering Figures by section.subsection.etc

Post by west.logan »

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"

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}  
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

Numbering Figures by section.subsection.etc

Post by meho_r »

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

\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}  
Last edited by meho_r on Wed Sep 29, 2010 3:21 pm, edited 3 times in total.
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Re: Numbering Figures by section.subsection.etc

Post by west.logan »

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.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Re: Numbering Figures by section.subsection.etc

Post by localghost »

Time has run out for editing. So I did the job.


Best regards and welcome to the board
Thorsten
Pete Sleeman
Posts: 2
Joined: Thu May 31, 2018 10:55 pm

Numbering Figures by section.subsection.etc

Post by Pete Sleeman »

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.

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}}
User avatar
Stefan Kottwitz
Site Admin
Posts: 10330
Joined: Mon Mar 10, 2008 9:44 pm

Numbering Figures by section.subsection.etc

Post by Stefan Kottwitz »

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

Numbering Figures by section.subsection.etc

Post by Pete Sleeman »

No problem !
The solution to the list-of-tables "number over-running the caption" was to do this:

Code: Select all

\renewcommand{\numberline}[1]{#1 \,}
It simply outputs the table's number, a small gap and then the table's caption.......pagenum
All sorted!
Post Reply