# Take 2: How do automatic overlay specifications work in Beamer (in a nested situation)

I had previous asked a question about this, but I feel that it wasn't focused enough and the answers got a little confused...so instead of editing that question, I've opted to start a new one....I hope people do not object.

I'm having trouble understanding what Beamer does when one uses nested automatic overlay specification. Consider the following example:

\documentclass[10pt,a4paper]{beamer}

\begin{document}
\begin{frame}
\Huge
\only<+>{a}\only<+>{b}\only<+>{c}\only<+>{d}\only<+>{e}
\only<1>{1}\only<2>{2}\only<3>{3}\only<4>{4}\only<5>{5}\only<6>{6}
\end{frame}
\end{document}


Which gives:

I would say that the foo and bar appear as expected, but outside of the \only between slide 4 and 5 something strange happens. In particular where are b and c? I would expect that there would be a nice correspondance between the letters and the numbers...wouldn't you? Sure, there would be an offset, but I would not expect that this offset would change at slide 5.

Also, is there a variable/counter that holds the slide number?

To give some weight to my suggestion that \beamerpauses should only be calculated once I would say the there are two completely non-intuitive lines in the following frame:

\begin{frame}
\alt<1-2>{
\begin{itemize}[<+->]
\item a
\item b
\end{itemize}}{
\addtocounter{beamerpauses}{2} %why should I need this?
\begin{itemize}[<+->]
\item A
\item B
\item C
\item D
\end{itemize}
}
\action<6>{} %this lines is really annoying because of the 6
%\action<+>{} %this gives an extra slide at the end.
%\action<.>{} %this doesn't work.

\end{frame}


I would write the whole thing without those extra lines and expect it to Just Work (TM)....

-

It's logic, captain, but not as we know it.

The crucial fact to remember is that every time beamer sees a + then beamerpauses is incremented. Here's what I see, with the value of beamerpauses (in italics) evaluated at lots of occasions. First, the actual code I ran (beamerpauses coloured to make it stand out).

{\color{green}\thebeamerpauses}
{\color{green}\thebeamerpauses}
\only<+>{a}\only<+>{b}\only<+>{c}\only<+>{d}\only<+>{e}
{\color{green}\thebeamerpauses}
\only<1>{1}\only<2>{2}\only<3>{3}\only<4>{4}\only<5>{5}\only<6>{6}
{\color{green}\thebeamerpauses}


Now, what I saw (asciified), plus my interpretation of what's going on, both as to what the code expands to (in terms of what all the overlay specifications become) and an "English translation". (Disclaimer: this is all from observed behaviour rather than looking under the bonnet or hood.)

1. 1 foo 3 bar 4 9 1 9

\only<1-4>{\alert<-2>{foo} \alert<-3>{bar}}
\only<4>{a}\only<5>{b}\only<6>{c}\only<7>{d}\only<8>{e}
\only<1>{1}\only<2>{2}\only<3>{3}\only<4>{4}\only<5>{5}\only<6>{6}


The +s in the \only and \alert mean that after "foo", we've incremented twice. The \alert for "bar" increments again. Each of the \onlys for "a" &c increments again, getting up to 9 by the end of that line.

2. 1 foo 3 bar 4 9 2 9

\only<1-4>{\alert<-2>{foo} \alert<-3>{bar}}
\only<4>{a}\only<5>{b}\only<6>{c}\only<7>{d}\only<8>{e}
\only<1>{1}\only<2>{2}\only<3>{3}\only<4>{4}\only<5>{5}\only<6>{6}


Same again.

3. 1 foo 3 bar 4 9 3 9

\only<1-4>{\alert<-2>{foo} \alert<-3>{bar}}
\only<4>{a}\only<5>{b}\only<6>{c}\only<7>{d}\only<8>{e}
\only<1>{1}\only<2>{2}\only<3>{3}\only<4>{4}\only<5>{5}\only<6>{6}


The -+ in the foo-alert is expanded to -2 so we lose the alert on foo here.

4. 1 foo 3 bar 4 a 9 4 9

\only<1-4>{\alert<-2>{foo} \alert<-3>{bar}}
\only<4>{a}\only<5>{b}\only<6>{c}\only<7>{d}\only<8>{e}
\only<1>{1}\only<2>{2}\only<3>{3}\only<4>{4}\only<5>{5}\only<6>{6}


This now happens for bar. Finally, we reach the slide where the first letter appears since the + in \only<+>{a} expands to 4.

5. 1 2 d 7 5 7

\only<1-4>{\alert<-2>{foo} \alert<-3>{bar}}
\only<2>{a}\only<3>{b}\only<4>{c}\only<5>{d}\only<6>{e}
\only<1>{1}\only<2>{2}\only<3>{3}\only<4>{4}\only<5>{5}\only<6>{6}


This is where the interesting stuff happens. The initial \only<1-+(3)>{...} is expanded to \only<1-4>{...}. This means that everything inside the braces now completely disappears. This includes the two +s. So all the \only<+>{a}s and so forth now think that they appear on slides two earlier than they used to.

6. 1 2 e 7 6 7

\only<1-4>{\alert<-2>{foo} \alert<-3>{bar}}
\only<2>{a}\only<3>{b}\only<4>{c}\only<5>{d}\only<6>{e}
\only<1>{1}\only<2>{2}\only<3>{3}\only<4>{4}\only<5>{5}\only<6>{6}


Same again.

So, in summary, the key to understanding this example is to remember that \only throws away its argument on slides where it isn't meant to be. This is extremely useful in making quite complicated stuff happen on different slides.

As for your last question: not that I know of. I once needed one, couldn't find one, and so defined one. It was a crude definition, involving a wrapper around the frametitle command so that I could add some code on every slide to increment the slide counter.

Regarding the update, I think that there are two issues here with regard to the two strange lines. The first is the + issue. I have to say that I find your assertion that it should "just work" slightly disingenuous. You have already specified an exact overlay specification (in the \alt<1-2>) so there's already something that you've had to compute by hand. There are many other ways to achieve what you want with that particular example (but I suspect that it is over simplified and that what you really want will probably be much more complicated!). Possibly the most relevant is to use the onlyenv environment because that does typeset its contents (and then throw them away). So this works:

\begin{frame}
\begin{onlyenv}<1-2>
\begin{itemize}[<+->]
\item a
\item b
\end{itemize}
\end{onlyenv}
\begin{itemize}[<+->]
\item A
\item B
\item C
\item D
\end{itemize}
\end{frame}


The second issue is actually something different, I think, and it is something that I've asked about before. Somehow, getting the beamerpauses right after an automatically unveiled list is a bit complicated. I have a command \itpause for after a list which subtracts one from beamerpauses and then does \pause - that's the simplest way I've found of getting the following to Just Work:

\begin{frame}
\begin{itemize}[<+->]
\item First item
\item Second item
\end{itemize}
\itpause
\end{frame}


With \pause there, the last line appears on the fourth frame, with nothing then it appears on the second frame. I would class this as a bug; and it seems to be something to do with a difference in beamerpauses inside and outside such a list environment. To see this, compare the different behaviour of \alert<.>{} inside and outside:

\begin{frame}
\begin{itemize}[<+->]
\item B
\item C
\item D
\end{itemize}
\end{frame}

-
Nice work! This question gave me some "pause" (groan). On slides 5 and 6 is \only<7>{5} supposed to be \only<5>{d}? Also, the notation at the beginning of each slide explanation with italicized numbers is very helpful but it took me a while to understand which points in the code each italicized number referred to. Maybe that could be indicated in the code with * s or something? –  Matthew Leingang Dec 3 '10 at 12:01
@Matthew: Yes and good suggestion. I've modified it to do that. –  Loop Space Dec 3 '10 at 12:12
@Andrew: Nice. I wish I could upvote it again. –  Matthew Leingang Dec 3 '10 at 12:24
@Andrew: Very nice explanation. However, I think that this is not how it should work. It doesn't make any sense that in different slides the numbers in the overlay specs are different...The other option is that Beamer would expand all the +'s in the frame once, replace them with the appropriate numbers and then not do all these expansions, ever. I don't see the advantages of the current algorithm, and I see lots of confusion... –  Yossi Farjoun Dec 3 '10 at 12:59
@Yossi: To do what you say, the \only command would have to examine its contents for any + s inside. At the moment, it just chucks away its argument and that is extremely useful. With the current implementation, you could do what you said manually by something like \alt<+-(3)>{\alert<+>{foo}}{\only<+>{}} but to get the current behaviour with your proposal would be much trickier, I think. –  Loop Space Dec 3 '10 at 13:15

Regarding the last question, the number of the current slide within the frame is stored in the TeX count register \beamer@slideinframe. You could turn it into a LaTeX counter with

\makeatletter \setcounter{currentslide}{\the\beamer@slideinframe} \makeatother


if you like.

-

In answer to your last question, I do not think there is a public counter that holds the slide number. Otherwise I would expect a command like \insertslidenumber and there isn't one. See this question where we hacked up a workaround subtracting \insertframestartpage from \insertpagenumber.

-