2

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}
4
  • Add \hfill after the first subfigure of your code using subcaption : \end{subfigure}\hfill\begin{subfigure}[b]{0.5\textwidth} Apr 25, 2022 at 22:36
  • It didn't work unfortunately :( Apr 25, 2022 at 22:40
  • 1
    @SimonDispa - You were close: what's needed after the first instance of \end{subfigure} is %, not \hfill.
    – Mico
    Apr 26, 2022 at 0:34
  • Both % and \hfill will work. The important thing is to not end a line with }, which adds an extra space (for which there is not enough room). BTW, inside subfigure you can use \caption. Theoretically, using \subcaption would produce a subsubcaption. Apr 26, 2022 at 12:52

1 Answer 1

3

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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .