[Tex/LaTex] Trouble placing images side by side in beamer

graphicsminipage

I need to place a couple of images side by side (horizontally) in a presentation I'm doing with beamer, so I looked around and inserted the following code (sugested by this post):

\documentclass{beamer}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{frame}

\begin{figure}
\centering

\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=.4\linewidth]{image1}
\caption{A subfigure}
\end{minipage}
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=.4\linewidth]{image2}
\caption{A subfigure}
\end{minipage}

\caption{A figure with two subfigures}
\end{figure}

\end{frame}

\end{document}

But the images appear one over the other (vertically). I tried both approaches suggested in the aforementioned post but I get the same result. What can I be doing wrong and how can I fix it?

Best Answer

The minipage environment adds a small piece of horizontal space after it, so if you use

    ...
    \begin{minipage}{.5\textwidth}
    ...
    \end{minipage}%
    \begin{minipage}{.5\textwidth}
    ...

then you get the desired result- here's a complete MWE.

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{beamer}
\begin{document}
\begin{frame}
    \begin{figure}
        \centering
        \begin{minipage}{.5\textwidth}
            \centering
            \includegraphics[width=.4\linewidth]{example-image-a}
            \caption{A subfigure}
        \end{minipage}%
        \begin{minipage}{.5\textwidth}
            \centering
            \includegraphics[width=.4\linewidth]{example-image-b}
            \caption{A subfigure}
        \end{minipage}
        \caption{A figure with two subfigures}
    \end{figure}
\end{frame}
\end{document}