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}[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:
\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}
The standard LaTeX command for boxing something is \fbox
. Its argument is in text mode; to switch to display format mathematics you need to issue
\fbox{$\displaystyle ... $}
Thus your example becomes
\documentclass{article}
\begin{document}
\[ \frac{2}{4} =\fbox{$\displaystyle \frac{1}{2}$} \]
\end{document}
producing
If you are using amsmath
then its \boxed
command (thanks daleif for the link) already takes care of switching to maths and display style, so you just write the code for the math object directly between the braces {...}
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[ \frac{2}{4} = \boxed{\frac{1}{2}} \]
\end{document}
In other words in the argument to \boxed
you do not need math mode switching commands such as \[..\]
.
Best Answer
If you want to add text to just one slide, try
\only<>{}
With a bit of help from https://tex.stackexchange.com/a/205623/36296 you could also explain the terms like this