5

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}
4
  • Do you want all equations to be eq (1)? :o)
    – Bernard
    Sep 2, 2015 at 19:40
  • @Bernard : Only if it's the first in the section ; j
    – kando
    Sep 2, 2015 at 19:43
  • @Bernard : Ultimately, I will be prepending the equation with the section number followed by a hyphen.
    – kando
    Sep 2, 2015 at 19:44
  • equations (3.2.5.1.2.1), (3.2.5.1.3.1), (3.2.5.2.1.1) and so on? Nanonumbers?
    – Bernard
    Sep 2, 2015 at 19:50

1 Answer 1

6

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
5
  • These all stack with each other?
    – kando
    Sep 2, 2015 at 19:45
  • There's the reset code I've been looking for! And they do stack! And Werner, you're wonderful! [Thank you Werner and Hupfer!]
    – kando
    Sep 2, 2015 at 19:48
  • @kando: For reference see Master and slave counters.
    – Werner
    Sep 2, 2015 at 19:50
  • Why \@addtoreset{equation}{subparagraph} is not sufficient?
    – touhami
    Sep 2, 2015 at 20:40
  • @touhami: Because the counters are only linked to the level above them. That is, subparagraph is only linked to the stepping of paragraph, and paragraph to the stepping of subsubsection.
    – Werner
    Sep 2, 2015 at 20:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .