4

Is there an easy way to get an accurate count of the number of equations that have identifiers? If they are all auto generated than \arabic{equation} does the job, but equations can also have manually specified identifiers using \tag.

One solution would be to simply redefine \tag to increment some other counter and then sum that counter and \arabic{equation} at the end. However, it turns out that this is not quite as simple as my failed attempt below shows.

The MWE below yields an incorrect count of one when it should be two. Also, not sure if there any other common way to add identifiers to equations.

enter image description here

Code

\documentclass{article}
\usepackage{amsmath}

\newcounter{taggedEquations}
\let\OldTag\tag
\renewcommand*{\tag}[1]{\stepcounter{taggedEquations}\OldTag{#1}}


\begin{document}
Auto numbered
\begin{align}
    y &= 2x 
\end{align}
Manually labelled
\begin{align}
    y &= x \tag{x}\label{eq:foo}
\end{align}
\addtocounter{taggedEquations}{\arabic{equation}}
Numbered Equations = \arabic{taggedEquations}
\end{document}
  • 1
    Just for clarification, by "labels" you don't necessarily mean \labels. Or do you mean \labels?. – Steven B. Segletes Jan 3 '17 at 13:54
  • 2
    @StevenB.Segletes: Yes, I do not mean \label as you can "number" an equation but never refer to it (so don't need a \label). Does that make sense? Have edited the question to not use "label". – Peter Grill Jan 3 '17 at 13:56
  • To me it seems that the incrementation of the tagged equation counter is local, hence is forgotten outside the align. Try \arabic{taggedEquations} after your last equation. It will print zero. – Ruben Jan 3 '17 at 14:12
  • ... even though it really should not be like that :) – Ruben Jan 3 '17 at 14:14
5

Rather than hook into the user-level tag command it's probably better to hook into the lower level tag formatting command used by all tags whether automatic or from \tag. Also you need to account for the fact that AMS alignments are executed twice, to measure alignment widths.

Since \eqref also uses \maketag@@@ (via \tagform@), setting \measuring@true ensures that the counter is not incremented when equation are referenced.

enter image description here

\documentclass{article}
\usepackage{amsmath}

\newcounter{taggedEquations}
\makeatletter
\def\eqref#1{{\textup{\measuring@true\tagform@{\ref{#1}}}}}%
\def\maketag@@@#1{\hbox{%
\ifmeasuring@\else
  \stepcounter{taggedEquations}%
\fi
\m@th\normalfont#1}}
\makeatother


\begin{document}
Auto numbered
\begin{align}
    y &= 2x 
\end{align}
Manually labelled
\begin{align}
    y &= x \tag{x}\label{eq:foo}
\end{align}
Equation Reference: \eqref{eq:foo}

Numbered Equations = \arabic{taggedEquations}
\end{document} 
  • It would be shorter if you say: \let\ams@maketag@@@\maketag@@@\renewcommand*\maketag@@@{\stepcounter{taggedEquations}\ams@maketag@@@} :) – Ruben Jan 3 '17 at 14:27
  • ... and like the pedant I am I'd rather name the counter like \newcounter{totalEquations} at this point. +1 anyways! – Ruben Jan 3 '17 at 14:29
  • @Ruben at which point? – David Carlisle Jan 3 '17 at 14:30
  • Now that there is only one counter to count them all. – Ruben Jan 3 '17 at 14:31
  • @Ruben your redefinition misses the \if@measuring test – David Carlisle Jan 3 '17 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.