[Tex/LaTex] Centered text moves between slides using \only

beameroverlays

I'm trying to get text (well, actually images, but my example uses text) to line up exactly between slides, such that you don't see them shift when advancing. In this example the text is not positioned in the same place on the first slide as on the second slide. This does not happen when I don't specify \centering:

\documentclass{beamer}

\begin{document}
\begin{frame}
  \centering
  \only<1>{
    test text
  }
  \only<2>{
    test text
  }
\end{frame}
\end{document}

My understanding of \only was that the included text is only typeset on slides matching the specified overlay specification, and absent altogether (as opposed to just covered or invisible) on those not matching the overlay. But clearly something else is going on here.

Best Answer

There's spurious space after \only<1>{...}

Add % there and the movement will vanish. And of course after the second one, if there's more content to follow.

\documentclass{beamer}

\begin{document}
\begin{frame}
  \centering
  \only<1>{
    test text
  }%  Add it here!!!
  \only<2>{
    test text
  }
\end{frame}
\end{document}