# How to use custom counter in an equation tag?

I am using custom counters for numbering examples and problems. The counters use chapter numbering followed by the problem number. For example, first problem in chapter 3 would be 3.1 and so on. In some of the problems I want to number the equations using this number "3.1" which is automatically generated by the counter. For example, first equation of 3.1 should read "P.3.1.1" P to indicate equation belongs to a problem, so that this numbering is distinct from the main numbering of equations. I can use the tag option to number it manually, extracting parts of the tag from thechapter and theprob counters. I was not able to automate the last part of the tag requirement, that is the last number. Another related question that comes next is how to refer to such equation tags uniquely if they are created with input from automated counters?

MWE of how I have implemented the counter and what I want.

\documentclass{book}

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

% resets the problem counter after every chapter
\makeatletter
\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.
$$A = B + C \tag{P.\thechapter.\theprob.1}$$

Another equation in this problem should be labelled P.1.1.2.
$$D = E + F \tag{P.\thechapter.\theprob.2}$$
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.
$$P = Q + R \tag{P.\thechapter.\theprob.1}$$

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

\end{document}


Edit: I have managed to create a new counter which produces the required result.

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


Using this inside the problem environment as \tag{\ptag} gives the required result. But the question of how to uniquely refer to this equation tag using \ref is not clear to me.

• Do you use equations outside of a problem? If yes, how should they be numbered (especially should they continue the numbering, i.e. first you have (1.1), then (P. 1.2), then (1.3), ...)? Commented Feb 22, 2023 at 14:36
• Yes, the P. part is to specify the equation belongs to a problem. Similarly there will be different equations for examples, with E. as a prefix. Commented Feb 22, 2023 at 15:07
• \newcounter{prob}[chapter] will reset the prob counter whnever the chapter counter changes. \renewcommand{\theprob}{\thechapter.arabic{prob}} will handle the formatting for \ref etc. Commented Feb 22, 2023 at 15:17
• thanks for the suggestion with reset counter. I have managed to create a new counter called \ptag which will give the required output (See the edit). However question remains how to use it with \ref as all the instances will be given by \tag{\ptag}. Commented Feb 22, 2023 at 15:33
• Doesn't it work for you now? If I add \label{a} to one of your equations and put somewhere \ref{a}, this produces "P.1.1.1" (or similar). Commented Feb 22, 2023 at 15:44

I'd recommend to use an amsthm-environment for the problems together with \counterwithin for correct numbering.

The equation numbering is now as you want it, but you have to use pequation-environments inside a problem instead of equation-environments.

In addition, I'd recommend to use the cleveref-package for references.

Note: Here I used article and sections for the sake of showing everything on one page. This should be easy to adapt for book and chapter.

\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:
$$c^2=a^2+b^2$$
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}

• The prefix P. is to specify that the equation belongs to a problem. The regular equations outside of problem environment will not have any prefix and will be as 1.1, 1.2 etc. Hence I want to use a prefix to distinguish between the two and also be able to refer them uniquely. Commented Feb 22, 2023 at 15:09
• Please see the edit, I have managed to create a new counter called \ptag which will give the required output (See the edit). However question remains how to use it with \ref as all the instances will be given by \tag{\ptag}. Commented Feb 22, 2023 at 15:33
• If you don't mind using a special equation-environment inside a prob-environment, please see updated answer. Commented Feb 22, 2023 at 16:02
• yes, thanks that should do it! Commented Feb 22, 2023 at 16:22