How to Place Two Images Side by Side in LaTeX
At a glance
- 1. Load the packages
Use graphicx to include image files and subcaption if you want separate captions or labels.
- 2. Set each width
Give each image a fraction of the available line width, leaving room for the gap between them.
- 3. Place them together
Put both image blocks inside one figure environment and separate them with horizontal space.
- 4. Add captions and labels
Put a caption and label in each subfigure, then add a caption and label for the full figure.
To place two images side by side, put them in the same horizontal row and make sure their combined widths leave room for the space between them. If you need an individual caption or reference for each image, the subcaption package provides a convenient subfigure environment.
The example below creates one numbered figure with two labeled subfigures. It uses sample images from the mwe package; replace those filenames with your own image files.
A complete example with subcaptions
\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{mwe} % supplies example-image-a and example-image-b
\begin{document}
\begin{figure}[htbp]
\centering
\begin{subfigure}[t]{0.48\textwidth}
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{First image}
\label{fig:first-image}
\end{subfigure}\hfill
\begin{subfigure}[t]{0.48\textwidth}
\centering
\includegraphics[width=\linewidth]{example-image-b}
\caption{Second image}
\label{fig:second-image}
\end{subfigure}
\caption{Two images in one figure.}
\label{fig:two-images}
\end{figure}
See Figures~\ref{fig:first-image} and~\ref{fig:second-image},
within Figure~\ref{fig:two-images}.
\end{document}
The graphicx package provides \includegraphics. The subcaption package provides the subfigure environment and its individual captions. The mwe package is included only to provide sample image files; for your own figures, use the correct filenames and remove \usepackage{mwe}.
Each subfigure is set to 0.48\textwidth. Together, those widths use 96% of the line, leaving some room for \hfill, which stretches the gap between the blocks. Inside a subfigure, \linewidth means that subfigure’s available width, so width=\linewidth scales the image to fit its block.
The optional [t] aligns the subfigure blocks at the top. The [htbp] option asks LaTeX to place the floating figure here if possible, or at the top or bottom of a page, or on a float page. It is a placement suggestion, not a guarantee that the figure will appear exactly at that point in the source.
Two images without separate captions
If the images do not need separate captions or labels, you can put two \includegraphics commands in one figure environment:
\begin{figure}[htbp]
\centering
\includegraphics[width=0.46\textwidth]{first-image}\hfill
\includegraphics[width=0.46\textwidth]{second-image}
\caption{A shared caption for both images.}
\label{fig:comparison}
\end{figure}
This simpler arrangement gives the pair one caption and one figure number. Adjust 0.46\textwidth if the images are too large or the gap is too narrow. If you want the images side by side but do not want them to float, omit the figure environment and use the two \includegraphics commands in the document text; the trade-off is that the pair will not receive a numbered figure caption through that environment.
How to choose image widths
The two image blocks and the space between them must fit on one line. For equal-width blocks, a width slightly below half the available line width usually leaves enough room for a modest gap. With subfigures, the relevant available width is commonly \textwidth; inside each subfigure, use \linewidth to size its image.
If the images have different aspect ratios, matching their widths does not make their heights equal. To make their displayed heights match instead, set a common height, for example \includegraphics[height=4cm]{first-image}. LaTeX preserves each image’s aspect ratio by default. Specifying both width and height without keepaspectratio changes the image’s aspect ratio; use keepaspectratio if you want it to fit within both dimensions while preserving its aspect ratio.
Common mistakes
- The second image moves to the next line. The blocks plus intervening spaces are too wide. Reduce their widths, or reduce the gap. Two blocks each at
0.5\textwidthcan overflow once the separating space is included. - The image exceeds its subfigure. Use
width=\linewidthinside the subfigure, notwidth=\textwidth; the latter can make the image wider than its block. - A caption or reference is missing. Use
\captioninside eachsubfigurefor subcaptions, and put each\labelimmediately after its corresponding caption. Put the main figure label after the main caption. - The figure is not exactly where the code appears. Figures are floats, so LaTeX may move them to improve page layout. Placement options influence where they may go but do not normally force exact placement.
- A graphics file cannot be found. Check that the file is in the project directory or provide its relative path, and use a format supported by your LaTeX engine. For example, pdfLaTeX commonly accepts PDF, PNG, and JPEG images; compilation workflows differ.
For two images that need their own captions and cross-references, the subcaption example is generally the most flexible starting point. For a single shared caption, two \includegraphics commands are enough.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.