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.
-
2Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question.– Torbjørn T.Dec 8, 2011 at 14:25
-
2This answer helped me: tex.stackexchange.com/a/83665/61609– ricardoramosAug 28, 2017 at 16:01
4 Answers
For two independent side-by-side figures, you can use two minipage
s 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}
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.
-
29If 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}
.– bkarpuzJun 4, 2013 at 7:04 -
11The width of the
subfigure
(.5\textwidth
) and the width of theincludegraphics
(.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.– cawNov 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?– VesnogMay 18, 2014 at 13:35
-
8@GonzaloMedina Sorry for disturbing, there was a space between the end command and the "%" which was the culprit.– VesnogMay 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
\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:
-
3you should run
texdoc subfig
, then you are sure that you'll get the latest version of the documentation.– user2478Dec 8, 2011 at 15:06 -
4
-
3It ensures that Latex does not insert white space at the end of the line, which can have an effect on positioning and line breaks.– lanoxxDec 11, 2016 at 12:45
-
3The top voted answer uses
subfigure
. Should I believe that or your answer? Jan 10, 2018 at 12:47 -
6@tommy.carstensen : The
Subfigure
package is now considered obsolete: it was superseded bysubfig
. Find more documentation here ctan.org/pkg/subfig Jun 3, 2018 at 18:18
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}
-
1Thanks rekire. But the images are not being displayed.– nikhilDec 8, 2011 at 11:40
-
1
-
1Ya rekire. It does not work for me– nikhilDec 8, 2011 at 11:43
-
2The second example does note make use of the
subfig
package at all!?– user2574Dec 8, 2011 at 16:23 -
13I think it would be better not to recommend using the obsolete package
subfigure
. Dec 8, 2011 at 16:27
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