Graphics, Figures & TablesFigure numbers by section.subsection-figure

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

Figure numbers by section.subsection-figure

Post by west.logan »

Thanks to meho_r, I'm learning a bit more about what is possible with renewcommand.

However, this still isn't an optimal solution because now the table of contents and list of figures will not display properly. I can correct the spacing on the list of figures easily enough but how can I display the text in each section and subsection? Is there a better way to change the figure number based on what section or subsection it is in?

My idea was to redefine the \section command so that it called \section and also renewed the figure number each time it was called. I have not been able to implement this though.

As a reiteration, I'm trying to create a figure number that shows what section it is in, e.g. the second figure in section 1.1.1 would be 1.1.1-2.

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}
\tableofcontents
\listoffigures
\newpage

\mysection{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 west.logan on Thu Sep 30, 2010 4:47 am, edited 1 time in total.

Recommended reading 2024:

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

NEW: TikZ book now 40% off at Amazon.com for a short time.

meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

Figure numbers by section.subsection-figure

Post by meho_r »

My bad, just reverse #1 and #2 in the code:

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}
\tableofcontents
\listoffigures
\newpage

\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}  
EDIT: Arguments reverted to previous state.
Last edited by meho_r on Thu Sep 30, 2010 8:27 am, edited 2 times in total.
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Re: Figure numbers by section.subsection-figure

Post by west.logan »

Hmm, that does it for the table of contents but now the headings don't work within the document.

I appreciate the help but I'm not sure I understand what these parameters mean. I looked up the "newcommand" documentation and such but would you mind just describing what your newcommand is actually doing?

Is there a way to redefine the "section" command (and so forth) to do what your "mysection" is doing? I tried \let\Oldsection\section and then doing a "renewcommand" but LaTeX didn't like that.
meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

Figure numbers by section.subsection-figure

Post by meho_r »

\newcommand{\mysection}% name of a new command
[2]% numbers of arguments in the new command
[]% default argument (this is problematic one, it "empties" TOC entry); the value specified here is used if argument #2 is omitted
{\section{[#2]% this is argument for optional title
{#1}% this is argument for title of a section

The rest you know.

If you aren't using optional titles for TOC and headers, you may use only one argument (i.e. remove default argument and optional title) which solves the problem, e.g.

Code: Select all

\newcommand{\mysection}[1]{\section{#1}\setcounter{figure}{0}\renewcommand{\thefigure}{\thesection-\arabic{figure}}}
Other solution would be to use both arguments, but to always specify optional title, i.e.: \section[Section 1]{Section 1}.
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Re: Figure numbers by section.subsection-figure

Post by west.logan »

Thank you for your explanation. I really do want to learn all of these little details.

I think I can live without the optional heading for now. I'll make a few tweaks but this is pretty much what I wanted, so thank you for your help!
meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

Figure numbers by section.subsection-figure

Post by meho_r »

You're welcome. In case you do need optional titles, one way may be to define another command, e.g., \myosection which will be similar to \mysection, but with 2 arguments, and use them on those places where needed. As for redefinition of \section command, I expect some low-level changes are needed and I personally am not ready to go down that path yet ;)
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Figure numbers by section.subsection-figure

Post by west.logan »

Actually I was able to do that redefinition myself with:

Code: Select all

\let\Oldsection\section
\renewcommand{\section}[1]{\Oldsection{#1}\setcounter{figure}{0}\renewcommand{\thefigure}{\thesection-\arabic{figure}}}
Post Reply