# How to preserve space when using an Overlay command in Beamer?

Into Beamer, I want to preserve the space when I replace an expression by another in a mathematics formula with the help of an Overlay command. For example, if we have in slide number 1 this formula:

$x+\mathrm{Trace}M=y.$


I want to replace the "\mathrm{Trace}M" by "z" for exmample such that in slide 2 the previous spacing still the same and we have :

$x+ z =y.$


One option:

\documentclass{beamer}
\usepackage{amssymb}

\newlength\mylen
\newlength\mylena
\newlength\mylenb

\newcommand\R{\mathbb{R}}
\newcommand\bL{bL}
\newcommand\Div{Div}

\newcommand\getmaxlength[2]{%
\settowidth\mylen{#1}%
\settowidth\mylena{#1}%
\settowidth\mylenb{#2}%
\ifdim\mylenb>\mylena\relax
\setlength\mylen{\mylenb}
\fi%
}
\newcommand\Replace[2]{%
\getmaxlength{#1}{#2}%
\makebox[\mylen][c]{#2}%
}

\begin{document}

\begin{frame}
\onslide<1,2>{
$x+ \only<1>{\mathrm{Trace}M} \only<2>{\Replace{\mathrm{Trace}M}{y}} =z$
}

\onslide<3,4>{
$x+ \only<3>{\Replace{\mathrm{Trace}M}{y}} \only<4>{\Replace{y}{\mathrm{Trace}M}} =z$
}

\onslide<5->{
Comme l'application d\'eterminant est diff\'erentiable et que sa diff\'erentielle en l'identit\'e est l'application trace, alors $p.p.\, y\in\R^3,\,$:
$\only<5>{\mathrm{det}(D_y\Phi(t,y))} \only<6->{\Replace{\mathrm{det}(D_y\Phi(t,y))}{|J(t,y)|}}=1+t\,\only<5>{\mathrm{Trace}(D_yV(y))} \only<6->{\Replace{\mathrm{Trace}(D_yV(y))}{\Div_y V(y)}} +t\,\epsilon_1(t,y),\,\epsilon_1(t)\xrightarrow{\bL^{\infty}}0.$
}
\end{frame}

\end{document}


The main command is

\Replace{<text1>}{<text2>},


which typesets but reserving the space used by . For expressions in math mode, the argument(s) should be enclosed in $...$.

Not related to the question, but please don't use $$...$$ in LaTeX documents; use $...$ instead; see Why is $...$ preferable to $$...$$?.

• Thank you. But, I encounter an error if I type "\Replace{some expression}{\mathbb{I}}". LaTeX Error: \mathbb allowed only in math mode. If I use the \$ delimiter, all works fine... – SAKLY Mar 14 '14 at 14:44
• There is another problem: if I use your command into \onslide<>, the spacing still not the same. Example:"\onslide<5->{ Comme l'application d\'eterminant est diff\'erentiable et que sa diff\'erentielle en l'identit\'e est l'application trace, alors : $$p.p.\, y\in\R^3,\,\only<5>{\mathrm{det}(D_y\Phi(t,y))}\only<6->{\Replace{\mathrm{det}(D_y\Phi(t,y))}{|J(t,y)|}}=1+t\,\only<5>{\mathrm{Trace}(D_yV(y))}\only<6->{\Div_y V(y)}+t\,\epsilon_1(t,y),\,\epsilon_1(t)\xrightarrow{\bL^{\infty}}0.$$ } " – SAKLY Mar 14 '14 at 15:23
• @SAKLY And what are \bL and \Div? Please provide complete, auto-contained examples. – Gonzalo Medina Mar 14 '14 at 16:52
• @SAKLY please see my updated answer. – Gonzalo Medina Mar 14 '14 at 17:13