如何在方程标签中使用自定义计数器?

如何在方程标签中使用自定义计数器?

我使用自定义计数器对示例和问题进行编号。计数器使用章节编号,后跟问题编号。例如,第 3 章中的第一个问题将是 3.1,依此类推。在某些问题中,我想使用计数器自动生成的数字“3.1”对方程式进行编号。例如,3.1 的第一个方程式应为“P.3.1.1”P,以表示方程式属于问题,因此此编号与方程式的主要编号不同。我可以使用选项手动tag编号,从thechaptertheprob计数器中提取标签的部分。我无法自动完成标签要求的最后一部分,即最后一个数字。接下来的另一个相关问题是,如果这些方程式标签是使用自动计数器的输入创建的,如何唯一地引用它们?

MWE 我如何实现计数器以及我想要什么。

\documentclass{book}


\usepackage{amsmath}
\newcounter{prob}
\newcommand{\prob}{
\stepcounter{prob}
{\textbf{Problem~\thechapter.\arabic{prob}.~ }}} 


% resets the problem counter after every chapter
\makeatletter
\@addtoreset{prob}{chapter}
\makeatother




\begin{document}


\chapter{Intro}

\prob This is a sample problem and it should be numbered 1.1.

I want this equation to be numbered according to the number of the problem. This equation below should be numbered P.1.1.
\begin{equation}
A = B + C
\tag{P.\thechapter.\theprob.1}
\end{equation}

Another equation in this problem should be labelled P.1.1.2.
\begin{equation}
D = E + F
\tag{P.\thechapter.\theprob.2}
\end{equation}
Right now I am doing this manually, but how can one automate the last counter within the problem.


Is there a simpler way to achieve this. And more importantly how to use \texttt{ref} with this \texttt{tag}. As if we use another problem, the \texttt{tag} will remain same. For example, let us consider another problem

\prob This is another sample problem and it should be numbered 1.2.
The equation below will be labelled as P.1.2.1, though the syntax for the tag is same.
\begin{equation}
P = Q + R
\tag{P.\thechapter.\theprob.1}
\end{equation}

How can one create unique syntax for these tags which can be used in \texttt{ref}.

\end{document}  

编辑:我已设法创建一个可产生所需结果的新计数器。

\newcounter{ptag}[prob]
\newcommand{\ptag}{
\stepcounter{ptag}
{\textbf{P.\thechapter.\theprob.\arabic{ptag}}}} 

在问题环境中使用它\tag{\ptag}可得到所需的结果。但如何使用唯一地引用此方程标签的问题\ref对我来说并不清楚。

答案1

我建议使用amsthm-environment 来解决问题并\counterwithin进行正确的编号。

方程编号现在已经符合您的要求,但是您必须pequation在问题中使用 -environments 而不是equation-environments。

此外,我建议使用cleveref-package 作为参考。

注意:这里我使用了articlesection,以便在一个页面上显示所有内容。这应该很容易适应bookchapter

在此处输入图片描述

\documentclass{article}

\usepackage{amsthm}
\newtheorem{prob}{Problem}[section] % New environment "prob" (will be displayed as "Problem") and its counter is reset at every new chapter.

\usepackage{amsmath}
\newcounter{pequation} % New counter for problem-equations
\counterwithin{pequation}{prob} % Adapt counter such that it also contains Section and Problem.
\newenvironment{pequation}{%
   \equation
   \refstepcounter{pequation}
   \tag{P.~\thepequation}
}{%
   \endequation
}


\begin{document}
   
   \section{First Chapter}
   Some text before/in between/after the problems.
   \begin{prob} \label{prob:a}
      Content of Problem 1.1.
      \begin{pequation} \label{eq:a.a}
         a^2+b^2=c^2
      \end{pequation}
      Intertext.
      \begin{pequation} \label{eq:a.b}
         a^2+b^2=c^2
      \end{pequation}
      End of Problem.
   \end{prob}
   Some text before/in between/after the problems.
   \begin{prob} % Counter increment works.
      Content of Problem 1.2.
      \begin{pequation}
         a^2+b^2=c^2
      \end{pequation}
      Intertext.
      \begin{pequation}
         a^2+b^2=c^2
      \end{pequation}
      End of Problem.
   \end{prob}
   Some text before/in between/after the problems.
   
   \section{Second Chapter}
   Some text before/in between/after the problems.
   \begin{prob} % Resetting the "prob"-counter works.
      Content of Problem 2.1.
      \begin{pequation}
         a^2+b^2=c^2
      \end{pequation}
      Intertext.
      \begin{pequation}
         a^2+b^2=c^2
      \end{pequation}
      End of Problem.
   \end{prob}
   Some text before/in between/after the problems.
   
   Normal equations still work:
   \begin{equation}
      c^2=a^2+b^2
   \end{equation}
   We can refer to Problems and equations, see e.\,g. Problem~\ref{prob:a} and Equations~\eqref{eq:a.a} and~\eqref{eq:a.b}. Nevertheless, I recommend using the \texttt{cleveref}-package.
   
\end{document}

相关内容