11

I have, in different overlying slides in a Beamer presentation, the letter y, in equation mode, being changed by \alert{z}. To create the overly I am using \only<1>{} and \only<2>{}, so that the only change is that letter being changed. It seems to be that the two letters take different space and then the rest of the equation moves around when one passes from one slide to the other. How to solve this?

9

When you change a letter to another, two things may change: the depth and/or height and the width. For the depth/height, you can just use \vphantom{...}. For the width, you must do measurements to know which is widest and then put each in a box of the appropriate size.

For convenience, here's a little macro which does all this automatically:

\documentclass{beamer}
\usepackage{calc}
\usepackage{mathtools}
\newlength{\letterwidth}
\newcommand{\mathmakeboxlargestof}[3]{%
% #1 = first symbol
% #2 = second symbol
% #3 = what is printed
  \setlength{\letterwidth}{\maxof{\widthof{$#1$}}{\widthof{$#2$}}}%
  \mathmakebox[\letterwidth]{\vphantom{#1}\vphantom{#2}#3}%
}
\begin{document}
\begin{frame}{Title}
$x+\only<1>{\mathmakeboxlargestof{y}{\alert{z}}{y}}\only<2>{\mathmakeboxlargestof{y}{\alert{z}}{\alert{z}}}=1$
\end{frame}
\end{document}

The macro \mathmakeboxlargestof takes as first argument the first letter (e.g. y), as second argument the second letter (e.g. \alert{z}) and as third argument what you want to be printed (e.g. y or \alert{z} or whatever else you like).

  • 1
    Very good. Question: Shouldn't the command be used as \mathmakeboxlargestof{y}{\alert{z}}{y} and \mathmakeboxlargestof{y}{\alert{z}}{\alert{z}} respectively? Just in case the alert is making some space. – Anna Taurogenireva Dec 14 '10 at 22:30
  • @Franklin: yes, it would be safer to use \alert everywhere (when it's just a color change it doesn't matter, but you never know). – Philippe Goutet Dec 14 '10 at 23:16
2

Philippe gave a great answer. For easier usage of his \mathmakeboxlargestof, you could use the following wrapper \replace:

\documentclass{beamer}
\usepackage{calc}
\usepackage{mathtools}
\newlength{\letterwidth}
\newcommand{\mathmakeboxlargestof}[3]{%
    \setlength{\letterwidth}{\maxof{\widthof{$#1$}}{\widthof{$#2$}}}%
    \mathmakebox[\letterwidth]{\vphantom{#1}\vphantom{#2}#3}%
}
\makeatletter
\newcommand\replace{}
\def\replace<#1>#2#3{%
    \only<#1>{\mathmakeboxlargestof{#2}{#3}{#2}}%
    \only<\number\numexpr#1+1>{\mathmakeboxlargestof{#2}{#3}{#3}}%
    }
\makeatother
\begin{document}
\begin{frame}{Title}
$x + \replace<1>{y}{\alert{z}} = 1$
\end{frame}
\end{document}

The macro \replace takes one number n in <> and two further arguments. The first of these two is put on slide number n, the second one on slide number n+1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.