[Tex/LaTex] Take 2: How do automatic overlay specifications work in Beamer (in a nested situation)

beamernestingoverlays

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<1-+(3)>{\alert<-+>{foo} \alert<-+>{bar}}
\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:
alt text

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)….

Best Answer

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}
\only<1-+(3)>{\alert<-+>{foo} {\color{green}\thebeamerpauses} \alert<-+>{bar}}
{\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
On the \alert{third} frame
\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}
\alert<.>{hello}
\begin{itemize}[<+->]
\item A \alert<.>{hello}
\item B
\item C
\item D
\end{itemize}
\alert<.>{hello}
\end{frame}