[Tex/LaTex] Equation numbering problems in AMSMath environments with \pause

amsmathbeamerequationspause

I'm writing a beamer presentation, and trying (unsuccessfully) to use \pause to step through a numbered array of equations, with the equations appearing in turn, and the corresponding numbers appearing at the correct time (i.e. when the equation first appears).

edit: I have partially solved the problem, using a variant on the \alt trick described here, but with some vertical wiggling.

The solution ought to be a little more elegant than the one I've proposed below, given that the end user of the document (not me) is likely to need to make changes, and is not an advanced (La)TeX-user.

\documentclass{beamer}
\begin{document}
  \begin{frame}
    \begin{gather}
       1\alt<2->{\\2\alt<3>{\\3}{}}{}
    \end{gather}
  \end{frame}
\end{document}

Best Answer

In the beamer user guide, in the explanation of \pause it says:

This command does not work inside amsmath environments like align, since these do really wicked things.

The wicked thing that align, gather and friends do is that they evaluate the contents of the environment twice. In particular, overlay commands such as \pause are executed twice, which can cause mayhem. In the first run, the contents of the environment are just measured; the actual typesetting is done in the second run.

Since the measuring in the first run starts with \begingroup \measuring@true, the problem can be fixed quite easily (although according to the beamer user guide "there is no fix for this"!): just let \measuring@true deactivate the appropriate overlay commands. (Of course one has to find out what exactly to deactivate ...) The code below does this, giving the desired output:

animated gif of the output

\documentclass{beamer}
\makeatletter
\let\save@measuring@true\measuring@true
\def\measuring@true{%
  \save@measuring@true
  \def\beamer@sortzero##1{\beamer@ifnextcharospec{\beamer@sortzeroread{##1}}{}}%
  \def\beamer@sortzeroread##1<##2>{}%
  \def\beamer@finalnospec{}%
}
\makeatother
\begin{document}
\begin{frame}
  \begin{gather}
    1 \\ \pause
    2 \\ \pause
    3
  \end{gather}
\end{frame}
\end{document}