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}

Peter Grill
  • 223,288
  • 1
    Just for clarification, by "labels" you don't necessarily mean \labels. Or do you mean \labels?. – Steven B. Segletes Jan 03 '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 03 '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 03 '17 at 14:12
  • ... even though it really should not be like that :) – Ruben Jan 03 '17 at 14:14

1 Answers1

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} 
Peter Grill
  • 223,288
David Carlisle
  • 757,742
  • It would be shorter if you say: \let\ams@maketag@@@\maketag@@@\renewcommand*\maketag@@@{\stepcounter{taggedEquations}\ams@maketag@@@} :) – Ruben Jan 03 '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 03 '17 at 14:29
  • @Ruben at which point? – David Carlisle Jan 03 '17 at 14:30
  • Now that there is only one counter to count them all. – Ruben Jan 03 '17 at 14:31
  • @Ruben your redefinition misses the \if@measuring test – David Carlisle Jan 03 '17 at 14:31
  • oh you meant the name of the counter at a point in time, I thought you meant at that (some) point in the code:-) – David Carlisle Jan 03 '17 at 14:32
  • Right :) and true that. For some silly reason I thought that the original macro would perform this test. Ok let's "agree" on: \makeatletter\newcounter{totalEquations}\let\ams@maketag@@@\maketag@@@\renewcommand*\maketag@@@{\ifmeasuring@\else\stepcounter{totalEquations}\fi\ams@maketag@@@}\makeatother. – Ruben Jan 03 '17 at 14:34
  • Just realized that this is also counting equation references. So adding \eqref{eq:foo} to the end results in the counter being reported as 3 instead of the desired 2. – Peter Grill Mar 22 '17 at 00:43
  • @PeterGrill probably \def\tagform@#1{{\measuring@true\maketag@@@{(\ignorespaces#1\unskip\@@italiccorr)}\fi} is simplest (untested:-) – David Carlisle Mar 22 '17 at 00:50
  • @DavidCarlisle: Above gives Illegal parameter number in definition of \tagform@.. Also, not sure why there is a \fi as there is not an if. And if \measuring@true is set, then isn't this going to always ignore the space taken up by the equation label when measuremesnts of size are computed? – Peter Grill Mar 22 '17 at 01:06
  • @PeterGrill sorry delete the fi actually you want to set th emeasuring flag in eqref not tagform (eqref doesn't measure anyway but setting it will stop the counting, I'll post somethin tomorrow if you've not fixed it by then (feel free to edit the answer:-) – David Carlisle Mar 22 '17 at 01:25
  • @DavidCarlisle: Have corrected it as you suggested. Please have a look to ensure that I didn't miss something. – Peter Grill Mar 22 '17 at 04:12
  • @PeterGrill - In the line immediately before \end{document}, might \thetaggedEquations be more straightforward than \arabic{taggedEquations}? – Mico Mar 22 '17 at 05:48
  • @Mico: I have been following the answers at What is the proper method of accessing a counter?. – Peter Grill Mar 22 '17 at 07:09
  • @PeterGrill - I was assuming that the definition of \thetaggedEquations wasn't going to be changed from its initial setting (set by \newcounter). However, this assumption may not be valid. – Mico Mar 22 '17 at 07:57
  • @Mico: Yep, you are correct. I just stick to using \arabic{} wheneber I need an actual value, so I don't have to think if the definition will be changed or not. – Peter Grill Mar 22 '17 at 08:40