[Tex/LaTex] beamer with equation or align : replace part of equation and keep alignment

alignbeamerequations

I'm creating a presentation with the beamer class. Assume I want to write this :

\begin{equation}
    f(x) 
    \only<1>{= \cos(x)}
    \only<2>{= 2\cos(x)}
\end{equation}

I have a problem because on the slide 2, the equation is longer and therefore
the equation is moving. If I replace only by uncover, there is a white
space on the slide 2.

I have the same problem for the align environment

\begin{align}
    f(x) &= \exp(x)\\
    \only<2>{&= \cos(x)}
    \only<3>{&= 2\cos(x)}
\end{align}

How can I replace part of an equation by something else without changing the placement of the other part of the equation ?

Edit

I will be more precise. I would like to find an efficient way to replace the
right hand side of an equality (for equation or align) without adding
extra white space and such that it keeps the equal sign at the same place.

For example :

\begin{equation}
    \cos(x) = 
    \somecommand<1>{\dfrac{\exp{ix}+\exp{-ix}}{2}}
    \somecommand<2>{\sum_{n=0}^{\infty}\dfrac{(-1)^{n}}{2n+1}x^{2n+1}}
    ...
    \somecommand<n>{some other equality with a given lenght}
\end{equation}
  • If \somecommand is \only then the whole equality will move from slide 1 to 2.
  • If \somecommand is \uncover then there will be a whitespace between the equal sign and the Taylor expansion on slide 2.
  • Using \hphantom with \alt or \temporal might be a solution but it seems very complicated to use it for more then two slides

Best Answer

There are a number of ways of doing this. Here's one, using \phantom{<stuff>} to pad the missing <stuff> in the shorter equation:

enter image description here

\documentclass{beamer}
\usepackage{amsmath}
\begin{document}

\begin{frame}
  \frametitle{A frame}

  \begin{equation}
    f(x) =
      \only<1>{\cos(x)\phantom{2}}
      \only<2>{2\cos(x)}
  \end{equation}

\end{frame}

\end{document}

Placement of the \phantom{<stuff>} depends on how you want to examine the contents. For example, you could also try

f(x) = \alt<2>{2}{\phantom{2}}\cos(x)

enter image description here

Also see \temporal in the beamer documentation.


For larger constructions, the best offer I can make is to identify the biggest (horizontally and vertically) element in the set of equations and store these in a macro and use another macro as a space-adjustment:

enter image description here

\documentclass{beamer}
\usepackage{amsmath}
\newcommand{\inserteqstrut}[1]{%
  \rlap{$\displaystyle#1$}%
  \phantom{\biggesteq}}
\begin{document}

% Store biggest equation in set
\newcommand{\biggesteq}{\sum_{n=0}^{\infty}\dfrac{(-1)^{n}}{2n+1}x^{2n+1}}

\begin{frame}
  \frametitle{A frame}

  \begin{equation}
    \cos(x) = 
    \only<1>{\inserteqstrut{\dfrac{\exp{ix}+\exp{-ix}}{2}}}
    \only<2>{\inserteqstrut{\biggesteq}}
    \only<3>{\inserteqstrut{\text{some equality}}}
  \end{equation}

\end{frame}

\end{document}

If you have two separate equations that make up the "biggest", use a combination of \vphantom (for the tallest/deepest) and \hphantom (for the widest/longest). Here's such an example:

enter image description here

\documentclass{beamer}
\usepackage{amsmath}
\newcommand{\inserteqstrut}[1]{%
  \rlap{$\displaystyle#1$}%
  \phantom{\biggesteq}}
\begin{document}

% Store biggest equation in set
\newcommand{\biggesteq}{%
  \vphantom{\sum_{n=0}^{\infty}n}% tallest/deepest
  \hphantom{\text{some other equality}}}% longest/widest

\begin{frame}
  \frametitle{A frame}

  \begin{equation}
    \cos(x) = 
    \only<1>{\inserteqstrut{\tfrac{\exp{ix}+\exp{-ix}}{2}}}
    \only<2>{\inserteqstrut{\sum_{n=0}^{\infty}n}}
    \only<3>{\inserteqstrut{\text{some other equality}}}
  \end{equation}

\end{frame}

\end{document}