[Tex/LaTex] Animated equation in Beamer

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?

Best Answer

  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}