The rules for the incremental overlay specification are given as:
\beamerpauses
is initially set to 1
- If
+
is encountered in an overlay specification, all occurrences of +
are replaced by \beamerpauses
, then \beamerpauses
is incremented
- If
.
is encountered in an overlay specification, all occurrences of .
are replaced by \beamerpauses
-1; \beamerpauses
is not changed
It might be easier to understand if you pretend this, which as far as I can see is equivalent:
\beamerpauses
is initially set to 0
- If
+
is encountered in an overlay specification, \beamerpauses
is incremented and all occurrences of +
are replaced by \beamerpauses
- If
.
is encountered in an overlay specification, all occurrences of .
are replaced by \beamerpauses
; \beamerpauses
is not changed
If you assume as well that \alt
is smart and gobbles the inactive argument rather than expands it, you would guess that
\alt<-+>{\alert<+>{foo}\alert<+>{bar}}{\alert<.>{bar}\alert<.>{foo}}
is the same as
\alt<-1>{\alert<+>{foo}\alert<+>{bar}}{\alert<.>{bar}\alert<.>{foo}}
On slide 1, the first argument \alert<+>{foo}\alert<+>{bar}
is expanded, resulting in
\alert<2>{foo}\alert<2>{bar}
After slide 1, the second argument \alert<.>{bar}\alert<.>{foo}
is instead expanded, resulting in
\alert<1>{bar}\alert<1>{foo}
So in total the combination is equivalent to
\alt<-1>{\alert<2>{foo}\alert<2>{bar}}{\alert<1>{bar}\alert<1>{foo}}
Put these in the same frame and you'll see they do the same thing.
The -
indicates an interval, so <2-4>
means "on slides 2 through 4." Without explicit endpoints the first and last slide are substituted. So the first <-+>
specification is equivalent to <1-1>
, which in turn is equivalent to <1>
.
Slide by slide we get:
the \alt
expands \alert<2>{foo}\alert<2>{bar}
, which since \beamerpauses
is 1 is the same as {foo}{bar}
the \alt
expands \alert<1>{bar}\alert<1>{foo}
, which since \beamerpauses
is 2 is the same as {bar}{foo}
.
Advanced incremental-overlay-specification-fu involves offsetting \beamerpauses
with numbers in parentheses such as \alert<.(2)->{foo}\alert<+->{bar}
, which will alert first bar
then both foo
and bar
. This is how you can have parts of a frame (for instance, parts of tikzpictures) change dynamically without having them all in the same order as they are typeset on the frame or hard-coding their slide numbers.
Edit Yossi asked if \alt
really is "smart". If you look in the source code for beamerbaseoverlay.sty
you find:
%
% \alt and \altenv
%
\def\alt{\@ifnextchar<{\beamer@alt}{\beamer@alttwo}}
\long\def\beamer@alttwo#1#2{\beamer@ifnextcharospec{\beamer@altget{#1}{#2}}{#1}}
\long\def\beamer@altget#1#2<#3>{%
\def\beamer@doifnotinframe{#2}\def\beamer@doifinframe{#1}%
{\beamer@masterdecode{#3}}\beamer@donow}
\long\def\beamer@alt<#1>#2#3{%
\def\beamer@doifnotinframe{#3}\def\beamer@doifinframe{#2}%
{\beamer@masterdecode{#1}}\beamer@donow}
To me it looks the like the effect of \alt<#1>{#2}{#3}
is that #2
and #3
are saved in macros which are expanded depending on whether #1
applies to the current frame. I don't understand expansion completely but I believe that when \def
is scanning for parameter text it doesn't expand that text until the point of replacement (as opposed to \edef
, which expands the parameter text before assigning it to #n
). So yes, \alt
is "smart" in the sense that conditionally included text is not expanded until it's included. I guess you could do a \tracingall
to know for sure.
Late Edit
Couldn't help but paste in this frame I'm working on right now.
\begin{frame}[label=integral-of-x]{Example: Integral of $x$}
\begin{example}<+->
Find $\int_0^3 x\,dx$
\end{example}
\begin{solution}<+->
\action<.->{For any $n$ we have $\alert<.(5)>{\Delta x = \frac{3}{n}}$ and for each $i$ between $0$ and $n$, $\alert<.(4)>{x_i = \frac{3i}{n}}$.}
\action<+->{For each $i$, take $x_i$ to represent the function on the $i$th interval.}
\action<+->{So}
\begin{align*}
\action<.->{\int_0^3 x\,dx = \lim_{n\to\infty} R_n }
\action<+->{&= \lim_{n\to\infty} \sum_{i=1}^n \alert<.(1)>{f(x_i)}\,\alert<.(2)>{\Delta x}}
\action<+->{ = \lim_{n\to\infty}\sum_{i=1}^n
\alert<.>{\left(\frac{\alert<.(2)>{3}i}{\alert<.(2)>{n}}\right)}
\alert<+>{\left(\frac{\alert<.(1)>{3}}{\alert<.(1)>{n}}\right) }\\}
\action<+->{&= \lim_{n\to\infty}\alert<.>{\frac{9}{n^2}} \alert<.(1)>{\sum_{i=1}^n i}}
\action<+->{ = \alert<.(1)>{\lim_{n\to\infty}}\frac{9}{\alert<.(1)>{n^2}}
\cdot \alert<.>{\frac{\alert<.(1)>{n(n+1)}}{2}}}
\action<+->{= \frac{9}{2}\alert<.>{\cdot 1}}
\end{align*}
\end{solution}
\end{frame}
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 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 \only
s for "a" &c increments again, getting up to 9 by the end of that line.
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.
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.
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
.
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.
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}
Best Answer
In short,
\only<>{}
does not render the braced material at all except on the named slides, while\onslide<>
renders the material until the next\onslide
, sort of like an\item
, but, on the off slides, covers it (according to the preference given in\setbeamercovered
; i.e. transparent or invisible). There are other alternatives, for example,\uncover<>{}
, which functions like\only
but acts like\onslide
; i.e. takes a specific chunk of text and covers or uncovers it as appropriate.There are some circumstances where they work differently. For example, using
\onslide
in one of theamsmath
equation environments (I have found) produces horrible bugs:Presumably this is because
align
does what the manual calls "wicked things" in processing its contents.You also ask about the
overlayarea
andoverprint
environments; these are fancy versions of\uncover
and\onslide
respectively, that reserve the space for an entire sequence of uncoverings. Theoverprint
environment does so automatically but only allows you to uncover in an "itemized" fashion by using\onslide
successively, and theoverlayarea
environment requires you to say exactly the size of the overlay area but allows you to use any Beamer constructs inside; it will then produce the requisite number of slides with various material appearing in that area (including necessary empty space) on each one.