如何定义一个每当进入显示方程环境时重置的计数器?

如何定义一个每当进入显示方程环境时重置的计数器?

我想要一个计数器,每当进入显示数学环境时都会重置,是否编号。我怎样才能实现这一点?

下面的代码仅在实际方程环境中产生零。

\documentclass{article}


\usepackage{amsmath}
\newcounter{mycounter}[equation]

\begin{document}

\setcounter{mycounter}{23}  
\begin{equation}
x\themycounter
\end{equation}

\setcounter{mycounter}{23}
\[
y\themycounter
\]

\setcounter{mycounter}{23}
\begin{multline}
a\\=b\themycounter
\end{multline}


\begin{equation}
z\themycounter
\end{equation}
Counter mycounter should reset in any equation--like environment, numbered or not.
\end{document}

答案1

使用\AtBeginEnvironmentetoolbox

\documentclass{article}
\usepackage{amsmath}
\usepackage{etoolbox}

\newcounter{mycounter}
\AtBeginEnvironment{equation}{\setcounter{mycounter}{0}}
\AtBeginEnvironment{equation*}{\setcounter{mycounter}{0}}
\AtBeginEnvironment{align}{\setcounter{mycounter}{0}}
\AtBeginEnvironment{align*}{\setcounter{mycounter}{0}}
\AtBeginEnvironment{gather}{\setcounter{mycounter}{0}}
\AtBeginEnvironment{gather*}{\setcounter{mycounter}{0}}
\AtBeginEnvironment{multline}{\setcounter{mycounter}{0}}
\AtBeginEnvironment{multline*}{\setcounter{mycounter}{0}}

\begin{document}

\setcounter{mycounter}{23}  
\begin{equation}
x\themycounter
\end{equation}

\setcounter{mycounter}{23}
\[
y\themycounter
\]

\setcounter{mycounter}{23}
\begin{multline}
a\\=b\themycounter
\end{multline}

\begin{align}
z\themycounter&=y\\
t\themycounter&=u
\end{align}

\begin{align*}
z\themycounter&=y\\
t\themycounter&=u
\end{align*}

\end{document}

相关内容