Why did I get my subfloat twice?

901 views Asked by At

I am trying to run two pictures in one beamer slide step by step. But I have a duplicate of the "caption" name.

\documentclass{beamer}


\begin{document}

\begin{frame}

       \begin{figure}[ht]
        \begin{center}
\leavevmode \subfloat[first]{%
            \includegraphics[width=4cm,height=4cm]<1>{example-1}}
          \hspace{2cm}


\subfloat[second]{% 
\includegraphics[width=4cm,height=4cm]<2>{example-2}}
  \end{center}
\end{figure}


\end{frame}

\end{document}

Do you know why?

1

There are 1 answers

0
Andrew Swann On

Your posted code does not compile without extra packages. I presume you are using subfig, which is the only standard package that will compile without errors. However as you notice it produces two captions; this is because there is no compatibility code to work with beamers overlay mechanism. However, for the purposes you describe, the \subfloats are not really necessary.

Here are two different approaches. The first places to the two figures at about the same position on each slide, the second places the first to the left on the first slide and the second to right on the following slide.

Sample output

\documentclass{beamer}

\begin{document}

\begin{frame}

  \begin{figure}[ht]
    \begin{center}
      \leavevmode \only<1>{\caption{first}%
      \includegraphics[width=4cm,height=4cm]{example-image-a}}
      \only<2>{\caption{second}% 
      \includegraphics[width=4cm,height=4cm]{example-image-b}}
    \end{center}
  \end{figure}

\end{frame}

\begin{frame}

  \begin{columns}
    \begin{column}<1>{0.45\textwidth}
      \begin{figure}
        \centering
        \includegraphics[width=4cm,height=4cm]{example-image-a}
        \caption{first}
      \end{figure}
    \end{column}
    \begin{column}<2>{0.45\textwidth}
      \begin{figure}
        \centering
        \includegraphics[width=4cm,height=4cm]{example-image-b}
        \caption{second}
      \end{figure}
    \end{column}
  \end{columns}

\end{frame}

\end{document}