499

I want to place 2 images side by side in LaTeX. I have 2 .png files and I don't understand how to do it in LaTeX. I have tried many ways but could not get a good result.

2

4 Answers 4

631

For two independent side-by-side figures, you can use two minipages inside a figure enviroment; for two subfigures, I would recommend the subcaption package with its subfigure environment; here's an example showing both approaches:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\centering
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=.4\linewidth]{image1}
  \caption{A subfigure}
  \label{fig:sub1}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=.4\linewidth]{image1}
  \caption{A subfigure}
  \label{fig:sub2}
\end{subfigure}
\caption{A figure with two subfigures}
\label{fig:test}
\end{figure}

\begin{figure}
\centering
\begin{minipage}{.5\textwidth}
  \centering
  \includegraphics[width=.4\linewidth]{image1}
  \captionof{figure}{A figure}
  \label{fig:test1}
\end{minipage}%
\begin{minipage}{.5\textwidth}
  \centering
  \includegraphics[width=.4\linewidth]{image1}
  \captionof{figure}{Another figure}
  \label{fig:test2}
\end{minipage}
\end{figure}

\end{document}

enter image description here

The demo option for graphicx was used only to make my example compilable for everyone; you shouldn't use that option in your actual code.

The % (between \end{subfigure} and \begin{subfigure} or minipage) is really important; not suppressing it will cause a spurious blank space to be added, the total length will surpass \textwidth and the figures will end up not side-by-side.

23
  • 29
    If one of the images have a multi-line caption then the images are not aligned side-by-side. In this case, use [t] option after \begin{subfigure}.
    – bkarpuz
    Jun 4, 2013 at 7:04
  • 11
    The width of the subfigure (.5\textwidth) and the width of the includegraphics (.4\linewidth) seem to get multiplied, is that correct? If you want them both side-by-side so that they fill the entire screen width, setting the graphic's width to full \textwidth works better for me.
    – caw
    Nov 16, 2013 at 1:17
  • 5
    @GonzaloMedina I cannot get the images to be positioned side by side they are always on top of each other. % character is included, how can this be remedied?
    – Vesnog
    May 18, 2014 at 13:35
  • 8
    @GonzaloMedina Sorry for disturbing, there was a space between the end command and the "%" which was the culprit.
    – Vesnog
    May 18, 2014 at 13:48
  • 6
    @Anonj Well % causes TeX to ignore everything to its right so, in particular, the space is killed. There are some questions/answers on this site dealing with uses/dangers of %; see, for example, tex.stackexchange.com/q/34844/3954. Dec 14, 2014 at 16:59
188
\usepackage{subfig}

The PDF documentation with lots of examples can be found here: subfig.pdf

Note that you'll see a lot of references to "subfigure" on the net, but that's outdated now.

Here is a small example taken from the documentation

\documentclass[10pt,a4paper]{article}
\usepackage[demo]{graphicx}
\usepackage{subfig}
\begin{document}
\begin{figure}%
    \centering
    \subfloat[\centering label 1]{{\includegraphics[width=5cm]{img1} }}%
    \qquad
    \subfloat[\centering label 2]{{\includegraphics[width=5cm]{img2} }}%
    \caption{2 Figures side by side}%
    \label{fig:example}%
\end{figure}
\end{document}

Output:

enter image description here

9
  • 3
    you should run texdoc subfig, then you are sure that you'll get the latest version of the documentation.
    – user2478
    Dec 8, 2011 at 15:06
  • 4
    What's the % after figure for?
    – Clément
    Aug 9, 2016 at 2:34
  • 3
    It ensures that Latex does not insert white space at the end of the line, which can have an effect on positioning and line breaks.
    – lanoxx
    Dec 11, 2016 at 12:45
  • 3
    The top voted answer uses subfigure. Should I believe that or your answer? Jan 10, 2018 at 12:47
  • 6
    @tommy.carstensen : The Subfigure pack­age is now con­sid­ered ob­so­lete: it was su­per­seded by sub­fig. Find more documentation here ctan.org/pkg/subfig Jun 3, 2018 at 18:18
31

You could use the subfigure package:

\begin{figure}
\hfill
\subfigure[Title A]{\includegraphics[width=5cm]{img1}}
\hfill
\subfigure[Title B]{\includegraphics[width=5cm]{img2}}
\hfill
\caption{Title for both}
\end{figure}

Or try this use the subfig package with \usepackage{subfig}:

\begin{figure}
\centering
\parbox{5cm}{
\includegraphics[width=5cm]{img1}
\caption{First.}
\label{fig:2figsA}}
\qquad
\begin{minipage}{5cm}
\includegraphics[width=5cm]{img2}
\caption{Second.}
\label{fig:2figsB}
\end{minipage}
\end{figure}
8
  • 1
    Thanks rekire. But the images are not being displayed.
    – nikhil
    Dec 8, 2011 at 11:40
  • 1
    You have to change the img1 and img2
    – rekire
    Dec 8, 2011 at 11:42
  • 1
    Ya rekire. It does not work for me
    – nikhil
    Dec 8, 2011 at 11:43
  • 2
    The second example does note make use of the subfig package at all!?
    – user2574
    Dec 8, 2011 at 16:23
  • 13
    I think it would be better not to recommend using the obsolete package subfigure. Dec 8, 2011 at 16:27
15

Since you didn't tell anything about whether these figures are going to be sub-figures or figures with continued numbering, here is an example that uses the floatrow package.

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{floatrow}

\begin{document}
  \begin{figure}[!ht]
    \centering
    \begin{floatrow}
      \ffigbox[\FBwidth]{\caption{Dummy figure}\label{fig:dummy-1}}{%
        \rule{1.6in}{0.9in}   % Just a dummy. Replace with your figure.
      }
      \ffigbox[\FBwidth]{\caption{Dummy figure}\label{fig:dummy-2}}{%
        \rule{1.6in}{0.9in}   % Just a dummy. Replace with your figure.
      }
    \end{floatrow}
  \end{figure}
\end{document}

Note that you have to replace the \rule commands with the images you want to include. For details please see the package manual.

1
  • 1
    I just make an anchor for this solution. thanks :-)
    – 象嘉道
    Mar 10, 2012 at 0:13