# Using \only<#> to change text color causes spacing issues

I'm comparing the following two commands:

1. This is a \only<1>{test}\only<2>{\textcolor{red}{test}}.

2. $\only<1>{\max_{(x,y)}}\only<2>{\textcolor{red}{\max_{(x,y)}}}$

The first one works fine, and makes the text turn red.

The second one also makes the text turn red. However, it causes a small shift in the spacing. I can't figure out what is causing this shift, has anyone else run into this problem before?

Is it problems with the math environment?

• I suspect that $\only<1>{...}$ leaves some empty box at the start of the math formula, so \max is slightly displaced because it's a math operator following an (empty) ordinary symbol. The same doesn't happen with \max inside \textcolor, because this starts another level of grouping and \max is the first item in the subformula. – egreg May 30 '13 at 23:07
• What about \alert<2>{$\max_{(x,y)}$}? – Corentin May 30 '13 at 23:08

Please always post complete documents not just fragments.

The whatsit nodes needed for \only cause math atoms in math mode that can affect the spacing. Easiest is to put an empty math atom {} before the \max in both cases.

\documentclass{beamer}
\begin{document}

\begin{frame}{}

This is a \only<1>{test}\only<2>{\textcolor{red}{test}}.

$\only<1>{{}\max_{(x,y)}}\only<2>{\textcolor{red}{{}\max_{(x,y)}}}$

\end{frame}

\end{document}


Another option is to use math mode for each expression separately (this seems (at least to me) more syntactically correct):

\documentclass{beamer}
\begin{document}

\begin{frame}

This is a \only<1>{test}\only<2>{\textcolor{red}{test}}.

\only<1>{$\max_{(x,y)}$}\only<2>{\textcolor{red}{$\max_{(x,y)}$}}

\end{frame}

\end{document}