# Animated equation in Beamer

I would like to animate an equation in Beamer with the \alt command. Unfortunately, there is some "movement" between the two equations. Consider the following example:

\documentclass{beamer}

\begin{document}
\begin{frame}<-2>{Example}
$\alt<1>{\max}{\min}(1,2)=\alt<1>{2}{1}$
\end{frame}
\end{document}


Then, my \max and \min commands have not the exact same length. Therefore, the sign = for instance (but also the (1,2)) moves between slides 1 and 2.

Is there a way to avoid this?

-

1. \minx, a \min that is right-aligned in a box of the width of \max.
2. \minxx, a \min that is \llaped at the end of a \hphantom{\max}.

With the mathtools package one can replace \llap with \mathllap and \makebox with \mathmakebox.

Addition care is needed if \max/\min is preceeded by any content as \hphantom and \widthof would hide this content from \max and \min.

## Code

\documentclass{beamer}
\usepackage{mathtools}
\newcommand*{\minx}{%
\mathmakebox[\widthof{$\max$}][r]{\min}%
}
\newcommand*{\maxx}{%
{\max}%
}
\newcommand*{\minxx}{%
\hphantom{\max}\mathllap{\min}%
}
\begin{document}
\begin{frame}<-2>{Example}
$\alt<1>{{\max}}{\minx}(1,2) = \alt<1>{2}{1}$
$\alt<1>{{\max}}{\minxx}(1,2) = \alt<1>{2}{1}$
$\alt<1>{\maxx}{\minxx}(1,2) = \alt<1>{2}{1}$
$\max(1,2) = 2$
$\minx(1,2) = 1$
\end{frame}
\end{document}

-
Very nice, thanks! –  Bruno Nov 28 '12 at 18:16

This does the trick :

\documentclass{beamer}
\newlength{\widthofamin}

\begin{document}
\begin{frame}<-2>{Example}

\settowidth{\widthofamin}{$\min$}

$\hspace{-\widthofamin}\alt<1>{\phantom{\min}\max}{\phantom{\max}\min}(1,2)=\alt<1>{2}{1}$

% Even inline, there's no extra space : $\hspace{-\widthofamin}\alt<1>{\phantom{\min}\max}{\phantom{\max}\min}(1,2)=\alt<1>{2}{1}$\\
% if you compare with $\alt<1>{\phantom{\min}\max}{\phantom{\max}\min}(1,2)=\alt<1>{2}{1}$.
\end{frame}
\end{document}


This method adds a virtual \max in front of the \min, and vice-versa, to ensure that both are right-aligned to the equal. But the side-effect is that it adds white space in front of the equation, which we fix with that \hspace.

I'm sure there is a less dirty solution using \savebox and friends, but I'm not familiar enough with these commands to build you an example.

-
This is an already quite convincing solution. Of course, it is kind of dirty, but at least it gives a better result than before! –  Bruno Nov 27 '12 at 22:44

I've tried to write a macro using @Qrrbrbirlbel's answer below. I had to use the \mathmakebox version to specify a length and throw in the vphantom to prevent the slide from jumping vertically. I hope someone finds it useful!

\documentclass{beamer}
\usepackage{mathtools}
\usepackage{calc}

\begin{document}

\newcommand*{\maxwidthof}[2]{\maxof{\widthof{#1}}{\widthof{#2}}}
\newcommand<>*{\robustalt}[2]{
\alt#3
{\makebox[\maxwidthof{#1}{#2}]{#1}}
{\makebox[\maxwidthof{#1}{#2}]{#2}}
}
\newcommand*{\maxwidthofm}[2]{\maxof{\widthof{$#1$}}{\widthof{$#2$}}}
\newcommand<>*{\robustaltm}[2]{
\alt#3
{\mathmakebox[\maxwidthofm{#1}{#2}]{#1}\vphantom{#1#2}}
{\mathmakebox[\maxwidthofm{#1}{#2}]{#2}\vphantom{#1#2}}
}

\begin{frame}%
$\robustaltm<1>{\max}{\min}(1,2) = \robustaltm<1>{2}{1}.$
x $\robustaltm<1>{\alpha}{\underbrace{\alpha}_{\rm apples}}$ \uncover<1-3>{done.}\\
x \robustalt<1>{apples}{oranges} \uncover<1-2>{done.}
\end{frame}%

\end{document}

-