Putting subfigures side by side using subcaption and caption package

graphicssidebysidesubcaptionsubfloats

I have the following piece of code:

\documentclass{article}
\usepackage{subcaption,caption}
\usepackag{graphicx}

\begin{document}

    \begin{figure}[hbt!]
            \centering
            \begin{subfigure}[b]{0.5\textwidth}
                \centering
                \includegraphics[width = 0.75 \textwidth]{./fig1}
                \subcaption{$\epsilon$-Greedy}
            \end{subfigure}
            \begin{subfigure}[b]{0.5\textwidth}
                \centering
                \includegraphics[width = 0.75 \textwidth]{./fig2}
                \subcaption{UCB}
            \end{subfigure}
        \end{figure}

\end{document}

This is creating the following picture:

enter image description here

But I want it to look like following (with subcaptions)

enter image description here

The second picture was created using subfigure package which is obsolete now and isn't allowing to put subcaption properly. Nevertheless, this is the code I used

\begin{figure}[hbt!]
            \centering
            \subfigure{
            \includegraphics[width = 0.45 \textwidth]{./fig1}
                }
            \subfigure{
            \includegraphics[width = 0.45 \textwidth]{./fig2}
            }
        \end{figure}

Best Answer

I can suggest two remedies:

  • Keep the widths of the subfigure environments unchanged, but insert % (comment character) immediately after the first instance of \end{subfigure}.

  • Reduce the widths of the subfigure environments from 0.5\textwidth to 0.45\textwidth, insert \hfill after the first instance of \end{subfigure}, and increase the width of the included graphics from 0.75\textwidth to 1\textwidth. This approach will also let you get rid of all three \centering instructions, greatly reducing code clutter.

enter image description here

\documentclass[demo]{article} % remove 'demo' option in real document
\usepackage{subcaption,graphicx}

\begin{document}

    \begin{figure}[hbt!]
            \centering
            \begin{subfigure}[b]{0.5\textwidth}
                \centering
                \includegraphics[width = 0.75\textwidth]{./fig1}
                \subcaption{$\epsilon$-Greedy}
            \end{subfigure}%     <-- note the % symbol
            \begin{subfigure}[b]{0.5\textwidth}
                \centering
                \includegraphics[width = 0.75\textwidth]{./fig2}
                \subcaption{UCB}
            \end{subfigure}
    \end{figure}

    \begin{figure}[hbt!]
            \begin{subfigure}[b]{0.45\textwidth}
                \includegraphics[width = 1\textwidth]{./fig1}
                \subcaption{$\epsilon$-Greedy}
            \end{subfigure}\hfill % <-- note the \hfill instruction
            \begin{subfigure}[b]{0.45\textwidth}
                \includegraphics[width = 1\textwidth]{./fig2}
                \subcaption{UCB}
            \end{subfigure}
    \end{figure}
        
\end{document}