Place images side by side in LaTeX (minipage, subfigure)

Two \includegraphics commands in the same paragraph stand next to each other when their widths add up to less than the line width. For a separate caption under each image, put each one into a minipage.

Preamble

The snippets on this page need these lines before \begin{document}:

\usepackage{graphicx}

Two images with one caption

Write both commands without a blank line between them. \hfill pushes the images to the left and right edge; without it they are separated by one space.

\begin{figure}
\centering
\includegraphics[width=0.48\textwidth]{image-a.png}\hfill
\includegraphics[width=0.48\textwidth]{image-b.png}
\caption{The sample before (left) and after the treatment (right)}
\end{figure}
Result
The compiled example

Two figures with their own numbers

Each minipage holds an image and a \caption, and each caption gets its own figure number. The argument of minipage is its width.

\begin{figure}
\begin{minipage}{0.48\textwidth}
\centering
\includegraphics[width=\linewidth]{image-a.png}
\caption{Before the treatment}
\label{fig:before}
\end{minipage}\hfill
\begin{minipage}{0.48\textwidth}
\centering
\includegraphics[width=\linewidth]{image-b.png}
\caption{After the treatment}
\label{fig:after}
\end{minipage}
\end{figure}
Result
The compiled example

Number the parts (a) and (b)

The subfigure environment from the subcaption package numbers the images as parts of one figure. Subfigures in LaTeX shows the details.

Complete document:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\begin{subfigure}{0.48\textwidth}
\includegraphics[width=\linewidth]{image-a.png}
\caption{Before}
\end{subfigure}\hfill
\begin{subfigure}{0.48\textwidth}
\includegraphics[width=\linewidth]{image-b.png}
\caption{After}
\end{subfigure}
\caption{The sample before and after the treatment}
\end{figure}
\end{document}
Result
The compiled example

Align images of different height at the top

An image stands on the baseline of its line, so \begin{minipage}[t]{...} lines up the bottom edges of the images. With \vspace{0pt} as the first line of each minipage, [t] lines up the top edges.

\begin{figure}
\begin{minipage}[t]{0.48\textwidth}
\vspace{0pt}
\centering
\includegraphics[width=\linewidth]{image.png}
\caption{A wide image}
\end{minipage}\hfill
\begin{minipage}[t]{0.48\textwidth}
\vspace{0pt}
\centering
\includegraphics[width=0.6\linewidth]{logo.png}
\caption{A square image}
\end{minipage}
\end{figure}
Result
The compiled example

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages