Alterning images in beamer is shifting to right

tucna picture tucna · Oct 3, 2014 · Viewed 7.2k times · Source

When I am trying to do alternating images in beamer with usage of the \only and overlayarea like this:

\begin{frame}
    \frametitle{Tasks}

    \begin{overlayarea}{\textwidth}{\textheight}
        \begin{figure}
            \centering
            \only<1>
                {
                    \includegraphics[width=0.3\textwidth]{img/noise_detail_2.png}
                }
            \only<2>
                {
                    \includegraphics[width=0.3\textwidth]{img/noise_detail_2.png}
                }
            \only<3>
                {
                    \includegraphics[width=0.3\textwidth]{img/noise_detail_2.png}
                }
            \only<4>
                {
                    \includegraphics[width=0.3\textwidth]{img/noise_detail_2.png}
                }
        \end{figure}
    \end{overlayarea}      
\end{frame}

the image is moving to the right more and more on the each slide. Let's say, that in the 1. slide is in the position x, in the second slide in the position x+5 and in the third slide x+10.

Why? How can I fix it?

Answer

Werner picture Werner · Oct 4, 2014

You have what is referred to as a spurious space between usages of \only. While spreading out your code for readability purposes might work well, sometimes these spaces cause unwanted output in your resulting PDF. Use % to keep the readability but avoid the spacing issues:

enter image description here

\documentclass{beamer}
\begin{document}

\begin{frame}
  \frametitle{Tasks}

  \begin{overlayarea}{\textwidth}{\textheight}
    \begin{figure}
      \centering
      \only<1>
        {%
          \includegraphics[width=.8\textwidth]{example-image-a}%
        }%
      \only<2>
        {%
          \includegraphics[width=.8\textwidth]{example-image-b}%
        }%
      \only<3>
        {%
          \includegraphics[width=.8\textwidth]{example-image-c}%
        }%
    \end{figure}
  \end{overlayarea}      
\end{frame}

\end{document}