无论节深度如何,重置任何节变化的“方程”计数器

无论节深度如何,重置任何节变化的“方程”计数器

我希望equation计数器在调用命令时自动重置\section,与部分深度无关:

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

MWE描述:

最强力的方法是\setcounter{equation}{0}每当调用节命令时执行此操作。此方法已被chngcntr包取代。

目前,chngcntr包用于重置方程计数器。如果下一节处于相同深度,这将自动重置计数器;但是,\counterwithin*每次更改节深度时都必须使用该命令。

当前的 MWE 代码最初是这里按用户Christian Hupfer

梅威瑟:

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

答案1

只需使用一组\counterwithin*指令:

\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

加星标的版本不会更新\theequation。这相当于

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

最新版本的 LaTeX (2015) 处理计数器重置的方式略有不同。它不是将计数器重置为 0,而是将其设置为 -1,然后是\stepcounter。这执行了计数器重置的分层级联效果。因此,使用

\counterwithin*{equation}{subparagraph}

或者

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

相关内容