3

I am trying to display two figures next to each other with captions using subfigures but it keeps placing one under the other.

Here is my code:

\documentclass{article}

\usepackage{graphicx}
\usepackage{siunitx}
\usepackage{hyperref}
\usepackage{xcolor}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\centering

\begin{subfigure}{0.5\textwidth}
\centering
\includegraphics[width=0.7\linewidth]{apparatus.jpg}
\caption{A photograph of the apparatus used}
\label{fig:capparatus}
\end{subfigure}

\begin{subfigure}{0.5\textwidth}
\centering
\def\svgwidth{0.7\linewidth}
\input{diagram.pdf_tex}
%\includegraphics[width=0.7\linewidth]{diagram.pdf}
\caption{A diagram of the apparatus sited inside the evacuated chamber}
\label{fig:cdiagram}
\end{subfigure}

\caption{A graphical depiction of the setup}
\label{fig:csetup}
\end{figure}

\end{document}

I have tried:

  • Changing from pdf_tex to just including the pdf file (generated from inkscape)
  • Adding [h] and [b] to the subfigures
  • Adding a % after the first and second subfigures
  • Changing the sizes of the subfigures and images

but nothing seems to make them display on the same line. Does anyone know how to do this?

Here is my output with pdf_tex:

With pdf_tex

And with just the pdf:

Without pdf_tex

4
  • 2
    Trailing spaces: Remove them: \end{subfigure}\begin{subfigure}
    – Sigur
    Apr 20, 2020 at 15:52
  • In addition to "Adding a % after the first and second subfigures": did you also remove the empty line between them?
    – leandriis
    Apr 20, 2020 at 15:53
  • Yes, the trailing whitespace seemed to be the issue. Thank you both.
    – Jack
    Apr 20, 2020 at 16:00
  • 1
    you have a blank line which is a paragraph break so forced vertical stacking it is the same layout as you get with letters xx side by side x<newline>x a space between them x<newline><newline>x two paragraphs and vertically stacked x Apr 20, 2020 at 16:18

2 Answers 2

3

To display images side by side it is preferable to use the subfig package instead of the subcaption package. You must not put a space between the two calls to subfloat

\documentclass{article}

\usepackage{graphicx}
\usepackage{siunitx}
\usepackage{hyperref}
\usepackage{xcolor}
\usepackage{subfig}

\begin{document}

\begin{figure}

\centering
\subfloat[A photograph of the apparatus used]{\label{fig:capparatus}
\centering
\includegraphics[width=0.45\linewidth]{apparatus.jpg}
}
%no space
\hfill
\subfloat[A diagram of the apparatus sited inside the evacuated chamber]{\label{fig:cdiagram}
\centering

\includegraphics[width=0.45\linewidth]{diagram.pdf}
}

\caption{A graphical depiction of the setup}
\label{fig:csetup}
\end{figure}

\end{document}

enter image description here

4
  • The subfigpackage is not preferable for placing figures side-by-side. This can be dan also with the subcaption package (with all advantages over subfig) and use subfigure environments. You only need to take a care, that between code for subfigures is not empty line and space.
    – Zarko
    Jan 16, 2021 at 4:55
  • subfigure package is Deprecated? ctan.org/pkg/subfigure
    – rpapa
    Jan 16, 2021 at 13:26
  • sibfigure package is deprecated, but not subfigure environment defined in the subcaption package which has some advantages over subfig package as better control of environment, less problems with hyper referencing. I just like to say, that use subfloat environment is not preferable over sunfigure. Interestingly, subcaption can be used for defined both of them.
    – Zarko
    Jan 16, 2021 at 15:00
  • Thank you, I will try, but at the moment I have no notable concerns
    – rpapa
    Jan 16, 2021 at 15:51
1

For exercise:

  • As was already mentioned, the cause of your problems is trailing space code between both subfigures. You only need to remove it as suggested in @Sigur comment: ****
  • Off-topic:
    • hyperref had to be with rare exception (as is cleveref package) had to be loaded lat in preamble (because it must correct the settings of other packages that will work properly).
    • since both images have equal width, you can use keys Gin for define it
    • you can have shorter code if prescribe narrower width for subfigure and equal image width to it:
\documentclass{article}
\usepackage{graphicx}
\usepackage{siunitx}
\usepackage{xcolor}
\usepackage{subcaption}
\usepackage{hyperref}   % had to loaded last

\begin{document}
    \begin{figure}[htb]
    \centering
    \setkeys{Gin}{width=\linewidth}
\begin{subfigure}{0.4\textwidth}
\includegraphics{example-image-duck}%{apparatus.jpg}
\caption{A photograph of the apparatus used}
\label{fig:capparatus}
\end{subfigure}% trailing space between `subfigure` environments  had to be removed
\hfil
\begin{subfigure}{0.4\textwidth}
\includegraphics{example-image-duck}%{diagram.pdf}
\caption{A diagram of the apparatus sited inside the evacuated chamber}
\label{fig:cdiagram}
\end{subfigure}
\caption{A graphical depiction of the setup}
\label{fig:csetup}
    \end{figure}
\end{document}

enter image description here

You must log in to answer this question.

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