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 [so]. 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 08 '11 at 14:25
-
2This answer helped me: https://tex.stackexchange.com/a/83665/61609 – ricardoramos Aug 28 '17 at 16:01
4 Answers
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}

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.
- 31,033
- 505,128
-
1
-
1@DNA inside the second
figureenvironment there are twominipages; one for each of the (independent) images. – Gonzalo Medina Aug 29 '12 at 20:42 -
-
@GonzaloMedina, the subfigure is giving me an error
ERROR: Missing number, treated as zero.– LWZ May 14 '13 at 04:27 -
@LWZ my example code compiles without errors; something else must be going on in your document and without seeing the actual code, it's hard to tell. Please opena a new fresh question and add to it a minimal version of the document illustrating the problem. – Gonzalo Medina May 14 '13 at 04:37
-
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}. – bkarpuz Jun 04 '13 at 07:04 -
12The 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\textwidthworks better for me. – caw Nov 16 '13 at 01: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 '14 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 '14 at 13:48
-
4What does the % mean/do though. Isn't it the comment character? Is it intended to suppress new line in this specific situation? Thanks. – xji Dec 14 '14 at 08:18
-
2@AnonJ it suppresses an otherwise spurious blank space between the two
minipages; if you don't suppress that space, you'll receive an overfull\hbox. – Gonzalo Medina Dec 14 '14 at 16:32 -
1This I know from the comments. What i wanted to ask is why it is achieved by the % character and what other possible uses does this % character potentially have (its usage here I definitely didn't know before). Probably I should have paraphrased my question a little bit differently. – xji Dec 14 '14 at 16:56
-
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, http://tex.stackexchange.com/q/34844/3954. – Gonzalo Medina Dec 14 '14 at 16:59 -
1I've got
error “Missing number, treated as zero”when I used your answer. This worked for me: http://tex.stackexchange.com/a/56164/5645 – Martin Thoma Dec 29 '15 at 08:55 -
putting
end{subfigure}\begin{subfigure}{.5\textwidth}on one line with no space also seems to work (as an alternative to inserting a%character. – dinosaur Sep 16 '16 at 17:04 -
I would refer to this answer: http://tex.stackexchange.com/a/56164/91289 – Ben Usman Dec 13 '16 at 03:29
-
1
-
There can be no line break between the
%character and the next subfigure. Otherwise the subfigures are not placed side by side. – tommy.carstensen Jan 10 '18 at 13:25 -
minipage caption overlaps: you'll probably get it if the graphics take
\linewidth. So use use minipages of0.48\textwidth, with an\hfillin between. – Paschalis Nov 11 '20 at 20:56 -
-
It's important to note that \label{} needs to be after \caption{}. – R. W. Prado Sep 30 '21 at 07:09
-
My two multi-line captions touch each other with the subfigure versions, as if they were the same piece of text. How can I avoid this? – Pertinax Dec 03 '22 at 17:03
-
Posting the version I always make when reading this answer, for my future self:
\begin{center} \centering \begin{minipage}{.5\textwidth} \centering
\end{minipage}% \begin{minipage}{.5\textwidth} \centering
\end{minipage} \end{center}
– Pulcinella Mar 23 '23 at 20:00 -
Might be one of the longest answers, but one that actually works in many usecases. – Timotej Leginus Feb 14 '24 at 18:23
\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. – Dec 08 '11 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. – lanoxx Dec 11 '16 at 12:45
-
4The top voted answer uses
subfigure. Should I believe that or your answer? – tommy.carstensen Jan 10 '18 at 12:47 -
6@tommy.carstensen : The
Subfigurepackage is now considered obsolete: it was superseded bysubfig. Find more documentation here https://ctan.org/pkg/subfig – Amarjit Dhillon Jun 03 '18 at 18:18 -
The labels are not well positionned when I copy pasted your code... They are at the left side of the images instead of below. – Raphael Royer-Rivard Mar 24 '19 at 14:17
-
i've tried this and it doesn't seem to work with overleaf, the figures just arrange one after the other – par Mar 26 '19 at 22:28
-
1
-
3Both packages, subfigure and subfig, are not being maintained anymore. There is no mention here that takes into account that caption package, used in the first answer, is being maintained nowadays. Check https://ctan.org/pkg/subfig, https://ctan.org/pkg/subfigure and https://ctan.org/pkg/caption links. Just go for the fist answer, folks. – R. W. Prado Sep 30 '21 at 06: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}
-
1
-
1
-
1
-
1I'm using this code in my master thesis together with this
\usepackage{subfigure}import. – rekire Dec 08 '11 at 11:49 -
1Thanks rekire. The above code specified is working for me. But how to put an individual caption for each figure. – Dec 08 '11 at 12:15
-
1
-
2
-
13I think it would be better not to recommend using the obsolete package
subfigure. – Gonzalo Medina Dec 08 '11 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.
- 46,014
