重置方程计数器

重置方程计数器
\documentclass{article}
\usepackage{amsmath, amsfonts, chngcntr}
\newcounter{problem}
\newcounter{solution}

\newcommand\Problem{%
  \stepcounter{problem}%
  \textbf{\theproblem.}~%
  \setcounter{solution}{0}%
}

\newcommand\TheSolution{%
  \textbf{Solution:}\\%
}

\newcommand\ASolution{%
  \stepcounter{solution}%
  \textbf{Solution \thesolution:}\\%
}
\parindent 0in
\parskip 1em
\begin{document}

\section{Kinematics}

\Problem A motorboat going going downstream overcame a raft at point $\emph{A}$; $\tau$ = 60 min later it turned back and after some time passed the raft at a distance $l$ = 6.0 km from the point $\emph{A}$. Find flow velocity assuming the duty of the engine to be constant.

\TheSolution Let u be the flow velocity and v be velocity of boat in still water, 
    \begin{equation} \frac{l}{u}=\tau + \frac{(u+v)\tau-l}{v-u}  \end{equation}
    \begin{equation} u=\frac{l}{2\tau}=\frac{6}{2\cdot1}=3 \ km/hr \end{equation} 

\Problem  A point traversed half the distance with a velocity $v_0$. The remaining part of the distance was covered with velocity $v_1$ for half the time, and with velocity $v_2$ for the other half of the time. Find the mean velocity of the point averaged over the whole time of motion. 

\TheSolution \begin{equation} v_{av} = \frac{2\cdot v_0\cdot\frac{v_1+v_2}{2}}{v_0 + \frac{v_1+v_2}{2}} \end{equation}
\end{document}

这是我编写问题及其解决方案的基本模板,现在如果我编译它,它将提供以下内容:

abv 代码的输出

现在我想要的是,等式计数器在每个问题之后重置,就像在问题 2 中它不会变成 (3) 而是变成 (1)。

答案1

根据您的格式要求,equation每次problem启动新环境时,您有两种主要选择来自动重置计数器。

  • 如果你只是想在equation每次新的problem开始时重置计数器,但不是希望改变方程计数器的显示方式,您可以在序言中运行以下指令:

    \counterwithin*{equation}{problem}
    

    *请注意末尾的“星号”( )符号\counterwithin*

  • 如果除了重置equation计数器之外,您还想改变计数器的外观,以便它显示与问题 1 相关的方程的 (1.1)、(1.2) 等;与问题 2 相关的方程的 {2.1) 等,您可以在序言中运行以下代码:

    \counterwithin{equation}{problem}
    

以下代码假设您想要追求第二种选择。

在此处输入图片描述

\documentclass{article}
\usepackage{amsmath, amsfonts}
\newcounter{problem}
\newcounter{solution}

%% option 1
%\counterwithin*{equation}{problem
%% option 2
\counterwithin{equation}{problem}

\newcommand\Problem{%
  \refstepcounter{problem}%
  \textbf{\theproblem.}~%
  \setcounter{solution}{0}%
}

\newcommand\TheSolution{%
  \textbf{Solution:}\\%
}

\newcommand\ASolution{%
  \stepcounter{solution}%
  \textbf{Solution \thesolution:}\\%
}
\parindent 0in
\parskip 1em

\usepackage{siunitx} % for \qty and \unit macros

\begin{document}

\section{Kinematics}

\Problem 
A motorboat going downstream passed a raft at point $\emph{A}$; $\tau = \qty{60}{min}$ later it turned back and after some time passed the raft at a distance $l = \qty{6.0}{km}$ from the point $\emph{A}$. Find flow velocity assuming the duty of the engine to be constant.

\TheSolution Let $u$ be the flow velocity and $v$ be velocity of boat in still water,
    \begin{equation} \frac{l}{u}=\tau + \frac{(u+v)\tau-l}{v-u}  
    \end{equation}
    \begin{equation} u=\frac{l}{2\tau}=\frac{6}{2\cdot1}=\qty{3}{km/hr} \end{equation}

\Problem  
A point traversed half the distance with a velocity $v_0$. The remaining part of the distance was covered with velocity $v_1$ for half the time, and with velocity $v_2$ for the other half of the time. Find the mean velocity of the point averaged over the whole time of motion.

\TheSolution 
\begin{equation} v_{av} = \frac{2\cdot v_0\cdot\frac{v_1+v_2}{2}}{v_0 + \frac{v_1+v_2}{2}} \end{equation}
\end{document} 

答案2

该命令\setcounter{equation}{0}将把方程计数器重置为 0。下次您有一个编号方程时,计数器将递增并显示,以便该方程的数字为 1。

相关内容