How do automatic overlay specification work in beamer?

The rules for the incremental overlay specification are given as:

  1. \beamerpauses is initially set to 1
  2. If + is encountered in an overlay specification, all occurrences of + are replaced by \beamerpauses, then \beamerpauses is incremented
  3. 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:

  1. \beamerpauses is initially set to 0
  2. If + is encountered in an overlay specification, \beamerpauses is incremented and all occurrences of + are replaced by \beamerpauses
  3. 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:

  1. the \alt expands \alert<2>{foo}\alert<2>{bar}, which since \beamerpauses is 1 is the same as {foo}{bar}

  2. 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.

dangerous bend 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}

sample code output