# beamer with equation or align : replace part of equation and keep alignment

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

There are a number of ways of doing this. Here's one, using \phantom{<stuff>} to pad the missing <stuff> in the shorter equation: \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) 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: \documentclass{beamer}
\usepackage{amsmath}
\newcommand{\inserteqstrut}{%
\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: \documentclass{beamer}
\usepackage{amsmath}
\newcommand{\inserteqstrut}{%
\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}

• thx, I was almost going to use this solution but as my equations are quite long, I was not willing to write them twice. and this will only work if I add stuff to the previous equation. for instance, if I replace \cos(x) by let's say \sum_{n=0}^{\infty} \dfrac{(-1)^{n}}{(2n+1)!}x^{2n+1} – PinkFloyd Sep 30 '14 at 20:18
• @PinkFloyd: My latter suggestion is to keep the structure the same, but use \alt or \temporal to change the specific components. Not sure whether that's a feasible alternative or not. – Werner Sep 30 '14 at 20:20
• well \alt or \temporal are better because I wouldn't have to rewrite the equation but it doesn't keep the correct alignment when I replace some part on an equation by something that is completely different – PinkFloyd Sep 30 '14 at 20:44
• @PinkFloyd: Well then, please provide something more substantive in terms of what you mean. It is not always possible to have your cake and eat it, but we can try... – Werner Sep 30 '14 at 20:59
• sorry, i will edit my question – PinkFloyd Sep 30 '14 at 21:04

Although Werner's solution is clearly cleaner, I find that sometimes a quick and dirty solution is to simply insert some extra spacing in one of the alternative overlays. For example I used this code:

\frame{
\frametitle{What: the Perron method}

We split coordinates $x = (x_+, x_-)$%
\uncover<2->{, change the initial time $t_0$ in the unstable part}
\uncover<3->{and let $t_0 \to \infty$}
\begin{alignat*}{2}
x_+(t) &\mapsto
\only<1  |handout:0>{e^{t\,A_+} \cdot x_+(0)\hspace{0.86cm}}
&& \only< -3|handout:0>{+}
\only<4-           >{-\,}
\int_{\only<1  |handout:0>{0}
\only<4-           >{t}}
^{\only<1-3|handout:0>{\smash{t}}
e^{(t-\tau)\,A_+} \cdot r_+((x_+,x_-)(\tau)) \d\tau,\\
x_-(t) &\mapsto
e^{t\,A_-} \cdot x_-(0)
&&+\int_{0\,}^t e^{(t-\tau)\,A_-} \cdot r_-((x_+,x_-)(\tau)) \d\tau.
\end{alignat*}
\uncover<3->{%
We consider this rewritten map $T$ for bounded curves
$x \in B(\R;\R^n)$ only.
}

}


to create slide 7 of this beamer presentation.

• you have to set the horizontal spacing by hand... not really efficient... – PinkFloyd Sep 30 '14 at 20:57
• @PinkFloyd: True, but it saves me from inserting more complicated \smash and \hphantom commands, and presentations anyways often need a bit of this kind of manual tweaking. – Jaap Eldering Sep 30 '14 at 22:05

I finally solved my problem using this solution (I know that it's not in agreement with my question because I wanted to use equation or align)

\documentclass{beamer}
\usepackage{amsmath,amssymb}
\renewcommand*{\a}{\hat a_{#1}^{\vphantom{\dagger}}}
\renewcommand*{\c}{\hat{c}_{#1}^{\vphantom{\dagger}}}
\newcommand*{\cd}{\hat{c}_{#1}^{\dagger}}
\renewcommand*{\H}{\hat{H}}
\newcommand*{\T}{\hat{T}}
\newcommand*{\dsum}{\displaystyle\sum_{#1}}
\newcommand*{\dsumd}{\displaystyle\sum_{#1=#2}^{#3}}
\newcommand*{\dprod}{\displaystyle\prod_{#1}}
\newcommand*{\dprodd}{\displaystyle\prod_{#1=#2}^{#3}}
\newcommand*{\dbigotimesd}{\displaystyle\bigotimes_{#1=#2}^{#3}}
\newcommand*{\dbigoplusd}{\displaystyle\bigoplus_{#1=#2}^{#3}}
\newcommand*{\ket}{\left|#1\right>}
\newcommand*{\ep}{\left(#1\right)}
\renewcommand*{\vec}{\mathbf{#1}}
\renewcommand*{\det}{\mathrm{det}\ep{#1}}

\begin{document}
\begin{frame}
\begin{minipage}[h]{0.4\linewidth}
$\T \only<1>{= \dbigoplusd{\alpha}{1}{N}\hat{T}_{\alpha}} \only<2-3>{= \dbigoplusd{\alpha}{1}{N}\hat{\vec{a}}^{\dagger}_{\alpha}T\hat{\vec{a}}^{\vphantom{\dagger}}_{\alpha}} \only<4->{= \dbigoplusd{\alpha}{1}{N}\dsumd{i}{1}{n}\omega_{i\alpha}\cd{i\alpha}\c{i\alpha}}$
\end{minipage}
\hfill
\begin{minipage}[h]{0.5\linewidth}
\uncover<3->{
\begin{align*}
\omega_{i\alpha} &< \omega_{i+1\alpha}
\end{align*}
}
\end{minipage}
\uncover<5->{
$\uncover<6->{\ket{\Psi_{G}}= } \only<5->{\uncover<6->{\hat{P}_{G}^{m}}\ket{\Psi}} \uncover<5->{=\uncover<6->{\hat{P}_{G}^{m}}\dbigotimesd{\alpha}{1}{N}\dprodd{i}{1}{mn/N}\alt<5-6>{\c{i\alpha}}{\dsumd{j}{1}{n}U_{ji}^{\alpha}\ad{j\alpha}}\ket{0}} \only<10->{\equiv \dsum{\mathcal{C}}\det{U_{\mathcal{C}}}\ket{\mathcal{C}}}$
}
\begin{center}
\uncover<8->{
$\ket{\mathcal{C}} \equiv \dbigotimesd{\alpha}{1}{N}\dprodd{i}{1}{mn/N}\ad{i_{\alpha}\alpha}\ket{0}$
}
\uncover<9->{
$\dsumd{\alpha}{1}{N}\ad{i\alpha}\a{i\alpha}\ket{\mathcal{C}} = m \qquad \dsumd{i}{1}{n}\ad{i\alpha}\a{i\alpha}\ket{\mathcal{C}} = \dsumd{i}{1}{n}\ad{i\beta}\a{i\beta}\ket{\mathcal{C}}$
}
\end{center}
\end{frame}
\end{document}


I hope this can help someone else...