4

I'm trying to put 2 images in a row with this code, and runs well, but... don't adapt the size of the last 2 images in a row.

\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}
\usepackage{subcaption}
\begin{document}
\includegraphics[width=\linewidth]{example-image-a}

\includegraphics[width=7.5cm]{example-image-b}

and now 2

\begin{figure}
\centering
\subcaptionbox{A cat\label{cat}}
[.45\linewidth]{\includegraphics{example-image-a}}
\subcaptionbox{An elephant\label{aaa1}}
[.45\linewidth]{\includegraphics{example-image-b}}
\caption{Two animals}\label{aaa2}
\end{figure}
\end{document}
Werner
  • 603,163
Mika Ike
  • 3,751

2 Answers2

6

At the time of inclusion, the images themselves have no idea about the box they may be contained within. So, you would need to set the width of each image individually. It's easier to just set [width=.45\linewidth] directly:

enter image description here

\usepackage{subcaption}
%...
\begin{figure}
  \centering
  \subcaptionbox{A cat\label{fig:cat}}
    {\includegraphics[width=.45\linewidth]{example-image-a}}
  \subcaptionbox{An elephant\label{fig:elephant}}
    {\includegraphics[width=.45\linewidth]{example-image-b}}
  \caption{Two animals}\label{fig:2animals}
\end{figure}

The space between the images is given by the line break between the \subcaptionboxes. If you wish to insert something more meaningful, see Reduction of Space between two Sub-figures.

Werner
  • 603,163
  • Yes, you´re right. My problem is typing[width...] before, because I view it in the fi4 in the loweer of page 7 in http://ctan.mirrorcatalogs.com/macros/latex/contrib/caption/subcaption.pdf – Mika Ike May 03 '14 at 16:17
3
\documentclass[12pt,a4paper]{article}
\usepackage[demo]{graphicx}%% delete [demo]
\usepackage{subcaption}
\begin{document}
\noindent
\includegraphics[width=0.49\linewidth]{ES-temperaturemap.eps}%
\hfill
\includegraphics[width=0.49\linewidth]{ES-temperaturemap.eps}

and now 2

\begin{figure}[!htb]
\centering
\subcaptionbox{A cat\label{cat}}[.45\linewidth]{%
  \includegraphics[width=0.45\linewidth]{ES-temperaturemap.eps}}
\subcaptionbox{An elephant\label{aaa1}}[.45\linewidth]{%
  \includegraphics[width=0.45\linewidth]{ES-temperaturemap.eps}}
\caption{Two animals}\label{aaa2}
\end{figure}
\end{document}

enter image description here

  • Yes, thank you. the problem was that I typed [width=...] before inlcudegrph... as I viewed in lower of page 7 in http://ctan.mirrorcatalogs.com/macros/latex/contrib/caption/subcaption.pdf – Mika Ike May 03 '14 at 16:18
  • 2
    that width is only for the caption. –  May 03 '14 at 16:19