[Tex/LaTex] Reset `equation` counter for any section change, regardless of section depth

countersnumbering

I would like the equation counter to reset automatically whenever a \section command is called, independent of section depth:

  • \section
  • \subsection
  • \subsubsection
  • \paragraph
  • \subparagraph

MWE Description:

The most brute force method would be to \setcounter{equation}{0} whenever a section command is called. This method has been superceded by the chngcntr package.

Currently, the chngcntr package is used to reset the equation counter. This will reset the counter automatically if the next section is at the same depth; however, the \counterwithin* command must be used each time the section depth is altered.

The current MWE code was originally developed here by user Christian Hupfer.

MWE:

\documentclass{scrartcl}

\usepackage{geometry}                   % margin/page layout settings
\usepackage{scrlayer-scrpage}           % improved header commands. [supercedes `fancyhdr' package].

\usepackage{mathtools}                  % includes amsmath, supplements it.
\usepackage{chngcntr}                   % allows changing equation section depth mid-document
\usepackage{etoolbox}


% Margin Settings:
\KOMAoptions{fontsize   = 12pt    ,
             parskip    = half-   ,
             headheight = 1.000em , 
             footheight = 2.700em , 
             DIV        = current }

\geometry{letterpaper              ,
          hmargin      = 0.750in   ,
          tmargin      = 0.750in   ,
          bmargin      = 0.750in   ,
          headsep      = 1.000em   ,
          footskip     = 3.700em   } % [ = Footheight + Footsep]


%Initialize headers and footers
\chead{\normalfont Header 1 \\ Header 2}
\cfoot{\normalfont Footer 1 \\ Footer 2}
\ofoot{\normalfont Page \thepage}


% Section numbering: Format \paragraph like \subsection
\newcommand{   \subsubsubsection} [1] {    \paragraph{#1} }
\newcommand{\subsubsubsubsection} [1] { \subparagraph{#1} }

\setcounter{secnumdepth}{5}

\makeatletter
\renewcommand{\paragraph}               %
  {\@startsection{paragraph}{4}{\z@}    %
  {-2.5ex\@plus -1ex \@minus -.25ex}    %
  {1.25ex \@plus .25ex}                 %
  {\normalfont\sffamily\normalsize\bfseries}     }

\renewcommand{\subparagraph}               %
  {\@startsection{subparagraph}{4}{\z@}    %
  {-2.5ex\@plus -1ex \@minus -.25ex}    %
  {1.25ex \@plus .25ex}                 %
  {\normalfont\sffamily\normalsize\bfseries}     }

\makeatother






% Equation Numbering
\let\origtheequation\theequation
\counterwithin*{equation}{section}

\newcommand{\xequationFormat} {\determineSection-\arabic{equation}}
\newcommand{\determineSection}{%
  \ifnum\value{subsection}    > 0
  \ifnum\value{subsubsection} > 0
  \ifnum\value{paragraph}     > 0
  \ifnum\value{subparagraph}  > 0 \thesubparagraph
  \else                           \theparagraph      \fi
  \else                           \thesubsubsection  \fi
  \else                           \thesubsection     \fi
  \else                           \thesection        \fi
}

\newcommand{\eqSectionDepth} [2] { \counterwithout*{equation}{#1}\counterwithin*{equation}{#2} }
% syntax: \equationNumbering{oldSectionDepth}{newSectionDepth}


% For demonstration purposes only:
\newtoggle{enableFormat}
\newcommand{\toggleformat}%
{ \iftoggle{enableFormat}%
  {%
    \togglefalse{enableFormat}%               % 0
    \let\theequation\origtheequation% 
  }%
  {%
    \toggletrue{enableFormat}%                % 1
    \let\theequation\xequationFormat%
  }%
}






\begin{document}

\setcounter{section}{-1}

\section{Level 1}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsection{Level 2}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsection{Level 3}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsubsection{Level 4}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subparagraph{Level 5}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}

\clearpage

\toggleformat

\section{Level 1}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}

\eqSectionDepth{section}{subsection}
\subsection{Level 2}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}

\eqSectionDepth{subsection}{subsubsection}
\subsubsection{Level 3}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}

\eqSectionDepth{subsubsection}{paragraph}
\subsubsubsection{Level 4}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}

\eqSectionDepth{paragraph}{subparagraph}
\subparagraph{Level 5}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\eqSectionDepth{subparagraph}{section}

\clearpage

\section{Level 1}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}

\eqSectionDepth{section}{subsection}
\subsection{Level 2}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}

\eqSectionDepth{subsection}{subsubsection}
\subsubsection{Level 3}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}

\eqSectionDepth{subsubsection}{paragraph}
\subsubsubsection{Level 4}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}

\eqSectionDepth{paragraph}{subparagraph}
\subparagraph{Level 5}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\eqSectionDepth{subparagraph}{section}

\end{document}

Best Answer

Just use a set of \counterwithin* instructions:

\counterwithin*{equation}{section}% Reset equation at \section
\counterwithin*{equation}{subsection}% Reset equation at \subsection
\counterwithin*{equation}{subsubsection}% Reset equation at \subsubsection
\counterwithin*{equation}{paragraph}% Reset equation at \paragraph
\counterwithin*{equation}{subparagraph}% Reset equation at \subparagraph

The starred version doesn't update \theequation. It's equivalent to

\makeatletter
\@addtoreset{equation}{section}
\@addtoreset{equation}{subsection}
\@addtoreset{equation}{subsubsection}
\@addtoreset{equation}{paragraph}
\@addtoreset{equation}{subparagraph}
\makeatother

The latest (2015) release of LaTeX handles counter resetting slightly differently. Instead of resetting the counter to 0, it sets it to -1 followed by a \stepcounter. This performs a hierarchical cascading effect of counter resetting. As such, it suffices to use

\counterwithin*{equation}{subparagraph}

or

\makeatletter
\@addtoreset{equation}{subparagraph}
\makeatother