1

I know i can use \setcounter{figure}{0} to achieve want i want but this would mean i have to add this command after every section. I was wondering if its possible to write a command such that every time i use \section{}, it automatically sets the figure numbering to 0.

Like: If \section{} used, then \setcounter{figure}{0}

I would like the figures to show up as Figure 1-1 so the chngctr package doesnt work.

  • 2
    Welcome to TeX.SE. Please clarify how the figure numbers should be displayed: (a) as 1, 2, 3, etc each time a new section begins, or as n.1, n.2, n.3, etc, where the previx number n is the number of the associated section. – Mico Sep 22 '21 at 15:32

2 Answers2

3

LaTeX has the command \counterwithin for this:

\documentclass{article}
\counterwithin{figure}{section}
\begin{document}
\section{First}
\begin{figure}[ht]
\centering
\fbox{FIRST}
\caption{First}
\end{figure}
\section{Second}
\begin{figure}[ht]
\centering
\fbox{SECOND}
\caption{Second}
\end{figure}
\end{document}
Ulrike Fischer
  • 327,261
0

I save a copy of \section and then redefine it so that resets the figure counter immediately prior to calling on the saved copy of \section.

To change the style of the typeset figure number, renew the definition of \thefigure, done here as \renewcommand\thefigure{\thesection--\arabic{figure}}, so that the first figure of each new section will have the number <section number>--1. (also works with appendices).

\documentclass{article}
\let\svsection\section
\def\section{\setcounter{figure}{0}\svsection}
\renewcommand\thefigure{\thesection--\arabic{figure}}
\begin{document}
\section{First}
\begin{figure}[ht]
\centering
\fbox{FIRST}
\caption{First}
\end{figure}
\section{Second}
\begin{figure}[ht]
\centering
\fbox{SECOND}
\caption{Second}
\end{figure}

\begin{appendix} \section{Appendix} \begin{figure}[ht] \centering \fbox{APPENDIX} \caption{Appendix} \end{figure} \end{appendix} \end{document}

enter image description here