# beamer's \only command, equation counter increment does not get retained outside of \only

I've got a problem where I am using beamer's \only command, but within \only, the equation number incrementation doesn't survive. I.e., I'd like to do

\only<1>{
\begin{align}
equation 1
\end{align}
}

\only<2>{
\begin{align}
equation 2
\end{align}
}


The problem is that both equations show up as having equation number 1, rather than the first being equation 1 and the 2nd being equation 2. One way to deal with this seems to be to add a \refstepcounter{equation} after the first only, but that is not ideal, and it depends on how many equations are in the first \only environment (the same problem would occur if defining a macro, say, that called the \refstepcounter{equation} at the end of the only, i.e,. the user would need to keep track of how many equations are in each \only).

Is there a better way to do this? thanks very much!

-
I think this is by design. Beamer assumes if you have two equations on a frame and only show one at the time, then they are really the same equation (maybe with slight modification) and should get the same number. – Caramdir Apr 5 '11 at 22:39
Thanks for the answer, but I was hoping there was some way to do this. The basic reason why is that I want a series of slides where the text on the top half stays the same, but the tex on the bottom changes in each slide, and some of the bottom text contains equations (and I don't want to have to copy the top-half material on separate slides, but I do want the bottom half equation numbers to increment since they are separate equations and should be referable separately). Thanks again! – Jim Smith Apr 5 '11 at 23:24

The following code adds a command to remove the equation counter from the list of counters that are reset after each overlay. (Warning: I don't know a lot about Beamer's internals, so it is possible that it breaks something that I am not aware of (though I did some testing).) Just add the definitions to your preamble (don't forget the \makeatletter) and use the commands as in the example.

\documentclass{beamer}

\makeatletter
% The following two commands should only be given between frames

% Remove the equation counter from the list of counters that are reset after
% each overlay.
\def\donotresetequations{{%
\let\@@elt\relax
\def\@elt##1{%
\expandafter\ifx\csname ##1\endcsname\c@equation%
\else%
\@@elt {##1}%
\fi%
}%
\edef\beamer@overlaycounterresets{\beamer@overlaycounterresets}%
\let\@elt\relax%
\def\@@elt{\@elt}%
\xdef\beamer@overlaycounterresets{\beamer@overlaycounterresets}%
}}

% Add the equation counter from the list of counters that are reset after
% each overlay.
\def\resetequations{\resetcounteronoverlays{equation}}
\makeatother

% Example document
\begin{document}

\begin{frame}
\only<1>{
\begin{align}equation\ 1.1\end{align}
}
\only<2>{
\begin{align}equation\ 1.2 \end{align}
}
\end{frame}

\donotresetequations

\begin{frame}
\only<1>{
\begin{align}equation\ 2.1\end{align}
}
\only<2>{
\begin{align}equation\ 2.2\end{align}
}
\end{frame}

\resetequations

\begin{frame}
\only<1>{
\begin{align}equation\ 3.1\end{align}
}
\only<2>{
\begin{align}equation\ 3.2 \end{align}
}
\end{frame}
\end{document}

-
That counter storage mechanism is quite clever by the way. It is basically a string of \@elt and the counter names. Whenever a macro has to be executed on each counter, \@elt is let to that macro and the list is expanded. – Caramdir Apr 6 '11 at 0:30
Hi Caramdir, your solution is one that I just tested and it seems to work. I'll use it for my entire presentation series and will get back to you if I run into any problems. Thanks for the clever and precise solution (seems something like this should be made part of beamer)!! – Jim Smith Apr 6 '11 at 0:47

You can use the \alt environment to include the equation when you want and increment the counter when you don't:

\documentclass{beamer}

\begin{document}
\begin{frame}
\alt<1>{% do this in frame 1
\begin{align}
equation 1
\end{align}
}{% do this not in frame 1
\stepcounter{equation}
}

\only<2>{
\begin{align}
equation 2
\end{align}
}

\end{frame}

\end{document}

-
Thanks Matthew. This, however, still suffers from the same drawback which is that one has to keep track of how many equations are in frame 1, and increment the counter a corresponding number of times. Ideally, there'd be a solution that uses latex's automatic equation counter increment mechanism that we all know and love. thanks though!! – Jim Smith Apr 6 '11 at 0:18

I think I might have stumbled upon an answer to my own question that does not require any beamer modification. It uses the beamer overprint feature and the \onslide feature. Critical is that you use the \onslide feature without any braces (i.e., if you were to change the below to use \onslide<1>{ stuff goes in here }, then the example fails to work and the contents continues to occupy space. Not using the braces, however, will (like the \only command) not occupy any space, but will increment the equation numbers for some reason. I am not sure if this is intentional beamer behavior or not, but it appears to work.

\begin{frame}{test slide}
This is some front text to exist on all slides.
\begin{overprint}
\onslide<1>
this goes on slide 1
\begin{align}
eq 1
\end{align}
\onslide<2>
this goes on slide 2
\begin{align}
eq 2
\end{align}
\end{overprint}
\end{frame}

-
Oh, just to comment on the solution above. This solution has a problem whereby if you create any labels (e.g., \label{equation:eqn1}) within the portion of the text that is to appear only on one slide, it seems like the labels are thrown away (very similar to the original problem I mentioned at the top). I haven't yet verified this, but I'm guessing that the above solution by Caramdir would also not retain any labels that are defined within such a block, again pointing to a potential problem with the beamer feature set. – Jim Smith Apr 8 '11 at 3:57
One more comment on the problem I mention immediately above: I think the solution to the label problem is that in beamer the label command requires also an overlay specification (see page 83 of the beamer manual, version 3.1). So if you give the label above as \label<2>{equation:eqn2} this then gives an appropriate label to the 2nd equation created on page 2, which can be ref'd accordingly (if no <2> is given to the label command, then the label creation seems only to activate on slide one, but label appears on slide two so its internals seems never to run, or something). – Jim Smith Apr 17 '11 at 9:04