0

I'm trying to put two images with the subcaption package side by side in an enumerate environement. This works fine, however the indent is not correct. Here is my code:

\documentclass[10pt,a5paper]{book}
\usepackage[demo]{graphicx}
\usepackage{epstopdf}
\usepackage{subcaption}
\usepackage{enumitem}

\begin{document}

\begin{enumerate}[label=\alph*)]
\item first item
\item second item
\item
    \begin{figure}[htb!]
    \centering
    \begin{subfigure}[b]{0.40\linewidth}
        \includegraphics[width=\linewidth]{figures/fig1.eps}
        \caption{}
        \label{fig1}
    \end{subfigure}
    \hspace{0.05\textwidth}
    \begin{subfigure}[b]{0.40\linewidth}
        \includegraphics[width=\linewidth]{figures/fig2.eps}
        \caption{}
        \label{fig2}
    \end{subfigure}
    \caption{(a) fig1 (b) fig2}
    \label{fig1and2}
    \end{figure}
\end{enumerate}

\end{document}

Similar problems were discussed here:

Subfigure with caption within an itemize list not indenting correctly (I do not want to use the subfig package as it stands in conflict with subcaption package)

Figure with caption within an itemize list not indenting correctly (only one image is subject to the procedure)

Thank you for your help!

muppethead
  • 11
  • 5

1 Answers1

0

As in the questions you've linked, the solution is to use the minipage environment (instead of the floating figure environment):

\documentclass[10pt,a5paper]{book}
\usepackage[demo]{graphicx}
\usepackage{epstopdf}
\usepackage{subcaption}
\usepackage{enumitem}

\begin{document}

\begin{enumerate}[label=\alph*)]
\item first item
\item second item
\item~\\[-1em]\begin{minipage}{\linewidth}
    \centering
    \captionsetup{type=figure}
    \begin{minipage}{0.4\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figures/fig1.eps}
        \subcaption{}
        \label{fig1}
    \end{minipage}
    \hspace{0.05\linewidth}
    \begin{minipage}{0.4\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figures/fig2.eps}
        \subcaption{}
        \label{fig2}
    \end{minipage}
    \captionof{figure}{(a) fig1 (b) fig2}
    \label{fig1and2}
\end{minipage}
\end{enumerate}

\end{document}

enter image description here

Some comments:

  • figure is a floating environment that will not respect margins set by the enumerate environment, even if you place it with [h!]. Use a minipage instead for user-defined positioning of things.
  • Inside of minipage, the \caption command doesn't work. So use \captionof instead to make a user-defined caption without the need to be inside a floating environment. The first argument here gives the style of the caption to be used.
  • To still be able to use the \subcaption command, you have to let it know what type of subcaption (i.e. table or figure) you want to have. Use the \captionsetup command for that.
  • If you just put the minipage as an item, the label will be aligned with respect to the baseline of the graphics. I assume that you want to have the label at the top end of the graphics, so I inserted an empty line (~) and then a negative vertical space of the same size (\\[-1em]). The only effect of ~\\[1em] is thus to have the item label at the top.
Tiuri
  • 7,749