[Tex/LaTex] Captions at right/left side in beamer

beamerfloatrow

I want to place captions at right/left side of pictures in a beamer presentation. However, the following code does not work due to incompatibilities between beamer and floatrow.

\documentclass{beamer}
\setbeamertemplate{caption}{\insertcaption}

\usepackage{floatrow}

\begin{document}

\begin{frame}
    \begin{figure}
        \floatbox[{\capbeside\thisfloatsetup{capbesideposition={right,bottom},capbesidewidth=0.4\textwidth}}]{figure}[\FBwidth]
        {\caption{Example image}\label{fig:example}}
        {\includegraphics[width=0.5\textwidth]{example-image}}
        %\includegraphics[width=0.5\textwidth]{example-image}
        %\caption{Example image}
        %\label{fig:example}
    \end{figure}
\end{frame}

\end{document}

Best Answer

Here is a solution using a columns environment:

\documentclass{beamer}
\setbeamertemplate{caption}{\insertcaption}

\begin{document}
\begin{frame}
    \begin{figure}
      \begin{columns}
        \column{.6\linewidth}
        \includegraphics[width=\textwidth]{example-image}
        \column{.3\linewidth}
        \caption{Example image}
        \label{fig:example right}
      \end{columns}
    \end{figure}
\end{frame}

\begin{frame}
    \begin{figure}
      \begin{columns}
        \column{.3\linewidth}
        \caption{Example image}
        \label{fig:example left}
        \column{.6\linewidth}
        \includegraphics[width=\textwidth]{example-image}
      \end{columns}
    \end{figure}
\end{frame}
\end{document}

enter image description here

enter image description here

Edit

Here, I define a new command (\figurewithcaptionatside) with 3 parameters (first parameter is optional):

  1. the options : caption position (vertical position: c, t orb), caption on the left, caption on the right, caption width, image width,

  2. the material to make the caption,

  3. the material to make the figure.

I use pgfkeys to manage the options.

Usage:

\begin{figure}
  \figurewithcaptionatside
  [caption position=t,caption on the left]
  {\caption{Example image with left caption}\label{fig:example left}}
  {\includegraphics[width=\textwidth]{example-image}}
\end{figure}

The complete code:

\documentclass{beamer}

\makeatletter
\newif\ifcaptionsideisleft
\pgfset{
  fwcas@/.is family,
  fwcas@,
  caption position/.is choice,
  caption position/c/.code={
    \def\fwcas@captionposition{c}\def\fwcas@imageposition{c}
  },
  caption position/b/.code={
    \def\fwcas@captionposition{b}\def\fwcas@imageposition{b}
  },
  caption position/t/.code={
    \def\fwcas@captionposition{T}\def\fwcas@imageposition{T}
  },
  caption left side@/.is if=captionsideisleft,
  caption on the left/.style={caption left side@=true},
  caption on the right/.style={caption left side@=false},
  caption width/.code={
    \pgfmathsetmacro\fwcas@captionwidth{#1}
  },
  image width/.code={
    \pgfmathsetmacro\fwcas@imagewidth{#1}
  },
  every figurewithcaptionatside/.style={
    caption position=c,
    caption on the left,
    caption width=.3\linewidth,
    image width=.7\linewidth,
  },
}
\newcommand\figurewithcaptionatside[3][]{
  \pgfset{fwcas@,every figurewithcaptionatside,#1}
  \ifcaptionsideisleft
  \begin{columns}
    \column[\fwcas@captionposition]{\fwcas@captionwidth pt}
    #2
    \column[\fwcas@imageposition]{\fwcas@imagewidth pt}
    #3
  \end{columns}
  \else
  \begin{columns}
    \column[\fwcas@imageposition]{\fwcas@imagewidth pt}
    #3
    \column[\fwcas@captionposition]{\fwcas@captionwidth pt}
    #2
  \end{columns}
  \fi
}
\makeatother

\setbeamertemplate{caption}{\insertcaption}
\begin{document}
\begin{frame}
    \begin{figure}
      \figurewithcaptionatside
      {\caption{Example image with default value}\label{fig:example}}
      {\includegraphics[width=\textwidth]{example-image}}
    \end{figure}
\end{frame}

\begin{frame}
    \begin{figure}
      \figurewithcaptionatside
      [caption position=t,caption on the left,
      caption width=.5\linewidth,
      image width=.5\linewidth,
      ]
      {\caption{Example image with left caption}\label{fig:example left}}
      {\includegraphics[width=\textwidth]{example-image}}
    \end{figure}
\end{frame}

\begin{frame}
    \begin{figure}
      \figurewithcaptionatside
      [caption on the right,caption position=b]
      {\caption{Example image with right caption}\label{fig:example right}}
      {\includegraphics[width=\textwidth]{example-image}}
    \end{figure}
\end{frame}
\end{document}