# Number only referenced equations for local equation numbering

In this question I asked to achieve a local equation numbering for equations which appear only inside proofs and to have a global counter for equations outside of proofs. I prefer the style where equations inside proofs are numbered with a star, i.e. (1*),(2*) or (3*a) and so on. This can be managed in the following way:

%
% Local equation numbering inside proofs
%

\newcounter{equationstore}
\AtBeginEnvironment{proof}{\setcounter{equationstore}{\value{equation}}
\setcounter{equation}{0}\renewcommand{\theequation}{\arabic{equation}${}^\ast$}}
\AtEndEnvironment{proof}{\setcounter{equation}{\value{equationstore}}}


As you can see there exist two equation counter. Before entering the proof environment, the global counter is stored, then switched to another local counter, which starts from zero for each proof environment. After exiting the proof environment the global equation counter is loaded.

I would like to know if it is possible to show only those equations inside proofs which one refers to inside a proof. Equations inside proofs will be only referenced from the same proof. A solution should respect that I use hyperref. cleveref is NO longer necessary for me.

• I could look into that later today I think; there's mathtools' showonlyrefs (see mathtools documentation), but AFAIK this change is global. Have you got a MWE to spare please? – yo' Sep 19 '16 at 13:32
• @yo' -- The showonlyrefs option of the mathtools package is seriously incompatible with cleveref. :-( – Mico Sep 19 '16 at 13:48
• @yo'-- A MWE can be taken from Andrew Swann right here: tex.stackexchange.com/a/290019/97026 – varsop Sep 19 '16 at 14:01

The hyperref solution I gave to the other question works fine with mathtools' option showonlyrefs provided you drop cleveref, which is a standard caveat for this option of mathtools:

\documentclass{article}

\usepackage{mathtools,amsthm,etoolbox}
\usepackage{hyperref}

\mathtoolsset{showonlyrefs}

\newtheorem{theorem}{Theorem}

\newcounter{equationstore}
\newcounter{proofnum}

\AtBeginEnvironment{proof}{\setcounter{equationstore}{\value{equation}}
\refstepcounter{proofnum}
\setcounter{equation}{0}\renewcommand{\theequation}{\arabic{equation}$^*$}
\renewcommand{\theHequation}{\arabic{proofnum}.\arabic{equation}$^*$}}
\AtEndEnvironment{proof}{\setcounter{equation}{\value{equationstore}}}

\begin{document}

$$\label{eq:one} x = 1$$

\begin{theorem}
A theorem.
\end{theorem}

\begin{proof}
Proof
$$\label{eq:p} a = b$$
refers to \eqref{eq:one}, \eqref{eq:two} and \eqref{eq:p} and gives
$$\label{eq:q} c = d$$
as required.
\end{proof}

$$\label{eq:two} z = 2$$
Refer to \eqref{eq:one}, \eqref{eq:two} and \eqref{eq:p}.

\begin{theorem}
A second theorem.
\end{theorem}

\begin{proof}
Proof
$$\label{eq:p2} a = b$$
refers to \eqref{eq:one}, \eqref{eq:two} and gives
$$\label{eq:q2} c = d.$$
\eqref{eq:q2} is the required result.
\end{proof}

\end{document}


For your new request of applying the reference-use check only to equations inside proofs, you can adjust the above code to

\newcounter{equationstore}
\newcounter{proofnum}

\AtBeginEnvironment{proof}{\mathtoolsset{showonlyrefs}%
\setcounter{equationstore}{\value{equation}}
\refstepcounter{proofnum}
\setcounter{equation}{0}\renewcommand{\theequation}{\arabic{equation}$^*$}
\renewcommand{\theHequation}{\arabic{proofnum}.\arabic{equation}$^*$}}
\AtEndEnvironment{proof}{\setcounter{equation}{\value{equationstore}}%
\mathtoolsset{showonlyrefs=false}}

• I have to uncheck this answer, since finally I came a across a misbehavior of the solution. What I want is to have showonlyrefs for referenced equations inside proofs, with a new counter for every proof, but on the opposite to display all equation numbers outside proof-environments (includes the non-referenced ones). I could use \noeqref for all equations outside proof-environments, but this is merely a workaround. – varsop May 9 '17 at 14:29
• EDIT: Maybe one can switch on 'showonlyrefs' when entering a proof and switch it off at the end? – varsop May 9 '17 at 14:38
• Yes, that works. I'll mention it in my answer. – Andrew Swann May 9 '17 at 14:56