Figures in LaTeX: \includegraphics, Captions and References
Quick answer
- Insert an image:
\usepackage{graphicx}, then\includegraphics[width=0.8\textwidth]{plot}(PDF, PNG or JPG; extension optional). - A numbered figure: wrap it in
\begin{figure}[htbp]with\centering,\caption{...}and\label{fig:plot}, label after caption. - Reference it:
Figure~\ref{fig:plot}, or\autoref{fig:plot}with hyperref. - Keep it in place:
\usepackage{float}and\begin{figure}[H]. - Text around it:
wrapfigurefrom thewrapfigpackage. List of figures:\listoffigures.
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\begin{figure}[htbp]
\centering
\includegraphics[width=0.6\textwidth]{example-image-a}
\caption{A sample figure.}
\label{fig:sample}
\end{figure}
Figure~\ref{fig:sample} shows a sample image.
\end{document}A figure in LaTeX has two parts: the image itself, inserted with \includegraphics from the graphicx package, and the figure environment around it, which adds a numbered caption and lets LaTeX place it where it fits on the page. This guide covers both, how to size and position images, how to label and reference figures, wrapping text around a figure, the list of figures, and the errors that come up most: missing files, too many unprocessed floats and figures that drift away from their text.
The examples use example-image-a, a placeholder that ships with every TeX distribution (the mwe package), so they compile as they are. Replace it with your own file name, such as figures/plot.
LaTeX \includegraphics: Inserting an Image
\includegraphics{file} places an image at its natural size, like a large letter in the line. The optional argument sizes and transforms it:
| Option | Effect |
|---|---|
width=0.8\textwidth | Width as a fraction of the text width (height scales to match) |
width=\linewidth | Full width of the current column or minipage |
height=5cm | A fixed height |
width=8cm, height=5cm, keepaspectratio | Fit inside the box without distortion |
scale=0.5 | Half the natural size |
angle=90 | Rotate counter-clockwise by 90 degrees |
trim=1cm 0 1cm 0, clip | Cut 1cm from the left and right (left bottom right top) |
page=3 | Page 3 of a multi-page PDF |
\documentclass{article}
\usepackage{graphicx}
\graphicspath{{figures/}{images/}} % folders to search, each in braces
\begin{document}
\includegraphics[width=0.45\textwidth]{example-image-a}
\hfill
\includegraphics[scale=0.2, angle=90]{example-image-b}
\includegraphics[width=\linewidth, height=3cm, keepaspectratio]{example-image}
\includegraphics[trim=2cm 1cm 2cm 1cm, clip, width=0.5\textwidth]{example-image-c}
\end{document}Leave out the file extension and LaTeX picks the best format available. pdfLaTeX, XeLaTeX and LuaLaTeX read PDF, PNG and JPG; EPS files are converted to PDF automatically by current distributions. SVG is not supported directly: export it to PDF, or use the svg package with Inkscape installed. Prefer PDF for plots and diagrams so they stay sharp at any zoom, and PNG or JPG for photos.
The figure Environment and Captions
Wrap the image in figure to get a caption and a number. Inside it:
\centeringcenters the content. Prefer it to thecenterenvironment, which adds extra vertical space inside the float.\caption{...}adds “Figure 1: ...”. Put it below the image for figures (above is the convention for tables).\caption[Short]{Long caption...}puts the short version in the list of figures.\label{fig:name}goes after\caption.
\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}
\captionsetup{font=small, labelfont=bf} % "Figure 1:" in bold, small text
\begin{document}
\listoffigures
\section{Results}
\begin{figure}[htbp]
\centering
\includegraphics[width=0.7\textwidth]{example-image-a}
\caption[Measured response]{Measured response of the system over ten trials.
The shaded band is one standard deviation.}
\label{fig:response}
\end{figure}
The response in Figure~\ref{fig:response} settles after a few trials.
\end{document}Figure Placement: [htbp], [H] and Floats
figure is a float: LaTeX moves it to a place where it does not leave a half-empty page. The optional argument says which places are allowed, and LaTeX tries them in the order h, t, b, p regardless of how you write them:
| Specifier | Meaning |
|---|---|
h | Here, at this point in the text, if it fits |
t | Top of a page |
b | Bottom of a page |
p | On a separate page of floats |
! | Relax the limits on how much of a page floats may take |
H | Exactly here, not a float at all (needs \usepackage{float}) |
The default is tbp. [htbp] is the usual safe choice; a lone [h] is risky, because if the figure does not fit right there it waits, and every later float waits behind it.
\documentclass{article}
\usepackage{graphicx}
\usepackage{float} % provides [H]
\usepackage{placeins} % provides \FloatBarrier
\begin{document}
\section{Setup}
Text before the figure.
\begin{figure}[H] % exactly here, never moved
\centering
\includegraphics[width=0.5\textwidth]{example-image-b}
\caption{The setup, placed exactly where it appears in the source.}
\label{fig:setup}
\end{figure}
Text after the figure.
\FloatBarrier % no float from above moves past this point
\section{Next}
Figure~\ref{fig:setup} stays in the Setup section.
\end{document}[H] can leave a large gap at the bottom of a page when the figure does not fit and moves to the next one. If that happens, let the figure float with [htbp] and refer to it by number instead of “below”.
Figure References: \label, \ref and \autoref
A label gives the figure a name; references print its number and update when figures are added or moved. Use a prefix like fig: to keep labels organized, and a ~ so “Figure” and the number never split across lines.
| Command | Output | Package |
|---|---|---|
Figure~\ref{fig:plot} | Figure 2 | None |
\pageref{fig:plot} | The page number, e.g. 5 | None |
\autoref{fig:plot} | Figure 2 (as a link) | hyperref |
\cref{fig:plot} | fig. 2 (\Cref: Figure 2) | cleveref, after hyperref |
\documentclass{article}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{cleveref} % load after hyperref
\begin{document}
\begin{figure}[htbp]
\centering
\includegraphics[width=0.5\textwidth]{example-image-c}
\caption{Error rate per epoch.}
\label{fig:error} % after \caption
\end{figure}
As Figure~\ref{fig:error} on page~\pageref{fig:error} shows, the error drops.
\autoref{fig:error} is a clickable reference, and so is \Cref{fig:error}.
\end{document}References need two compilations: the first writes the numbers to the .aux file, the second reads them. Until then you see ??. Hyperlink styling is covered in hyperlinks, and the same \label/\ref scheme numbers equations in equation numbering.
Side-by-Side Figures and wrapfig
For two images in one figure, put each in a minipage with its own caption. For panels labeled (a), (b) under a shared caption, use subcaption; the subfigures guide covers it in detail.
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{minipage}{0.48\linewidth}
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{Before training.}
\label{fig:before}
\end{minipage}\hfill
\begin{minipage}{0.48\linewidth}
\centering
\includegraphics[width=\linewidth]{example-image-b}
\caption{After training.}
\label{fig:after}
\end{minipage}
\end{figure}
\end{document}To let text flow around a smaller figure, use wrapfigure from wrapfig. It takes the side (r or l) and the width, and must start at the beginning of a paragraph, not right before a list or a heading:
\documentclass{article}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{lipsum}
\begin{document}
\begin{wrapfigure}{r}{0.4\textwidth}
\centering
\includegraphics[width=0.38\textwidth]{example-image-a}
\caption{A figure with text wrapped around it.}
\label{fig:wrapped}
\end{wrapfigure}
\lipsum[1-2]
\end{document}Common Errors and Fixes
| Message or symptom | Cause | Fix |
|---|---|---|
| LaTeX Error: File `plot' not found | Wrong path or name, a different case (Plot.PNG vs plot.png), or the file was not uploaded | Check the path relative to the main .tex file, match the case, or set \graphicspath |
| LaTeX Error: Unknown graphics extension: .svg | The engine cannot read that format | Convert to PDF or PNG, or use the svg package |
| LaTeX Error: Too many unprocessed floats | One float cannot be placed (strict [h], too large) and 18 more queue up behind it | Use [htbp], shrink the figure, add \clearpage, or use [H] |
| Figure ends up pages away or at the end of the chapter | The figure is too tall for the space, or only [h] or [p] is allowed | [!htbp], a smaller width, \FloatBarrier, or [H] |
| The reference shows ?? | Only one compile, or a misspelled label | Compile again; check the label name in the log warning Reference ... undefined |
| The reference shows the section number | \label is before \caption | Move \label after \caption |
| LaTeX Error: Not in outer par mode | A figure inside a minipage, table cell or another float | Use plain \includegraphics with \captionof{figure} (caption package) there |
Undefined control sequence on \includegraphics | graphicx is not loaded | \usepackage{graphicx} |
| Overfull \hbox, image sticks into the margin | Natural size or a width larger than \textwidth | width=\linewidth |
Paths with spaces or extra dots in the file name (my plot.v2.png) also cause trouble on some setups; rename files to letters, digits, hyphens and underscores. For a full walkthrough of image formats and folder layouts, see the guide to inserting images in LaTeX.
Frequently Asked Questions
How do I insert an image in LaTeX?
Load \usepackage{graphicx} in the preamble and write \includegraphics[width=0.8\textwidth]{plot} where the image should go. The file extension can be left out; pdfLaTeX accepts PDF, PNG and JPG. For a numbered figure with a caption, put it inside \begin{figure} … \end{figure} with \centering, \caption{...} and \label{fig:plot}.
How do I reference a figure in LaTeX?
Give the figure a label after its caption, \caption{Results}\label{fig:results}, and write Figure~\ref{fig:results} in the text. With hyperref, \autoref{fig:results} adds the word “Figure” and makes it a link. Compile twice so the numbers resolve; ?? means the label is not known yet or is misspelled.
How do I keep a figure where I put it in LaTeX?
Figures are floats, so LaTeX moves them to where they fit best. Give a placement such as [htbp] or [!htbp] to allow more positions, or load the float package and use [H] to force the figure exactly where it is in the source. \FloatBarrier from the placeins package stops figures from moving past a point, for example the end of a section.
Why does my figure caption show the wrong number?
The \label is before the \caption. \label picks up the most recent number, which before the caption is the section number. Always put \label after \caption (or inside it). If the number is ?? instead, compile again, since references take two runs.
How do I put two figures side by side in LaTeX?
Place two minipages next to each other inside one figure environment, each with its own \includegraphics and \caption, and widths that add up to less than \linewidth, such as 0.48\linewidth each. For sub-labels (a) and (b) under one main caption, use the subcaption package and its subfigure environment.
What does “Too many unprocessed floats” mean?
LaTeX keeps up to 18 figures and tables waiting to be placed. If one cannot be placed, often because its placement is too strict like [h] or it is too large, everything after it queues up until the limit is hit. Allow more positions with [htbp], make the figure smaller, add \clearpage to flush the queue, or use [H] from the float package.
Next Steps
- Subfigures – Panels (a), (b) with subcaption
- Insert Images in LaTeX (blog) – Formats, folders and troubleshooting
- Tables – The other common float, with captions above
- TikZ – Draw diagrams directly in LaTeX
- PGFPlots – Plots from data, without exporting images
- Table of Contents – Contents, list of figures and list of tables
Free LaTeX Tools
- LaTeX Preview – Try figure layouts with a live PDF preview
- AI LaTeX Generator – Generate LaTeX from text descriptions
- LaTeX Symbols Reference – Browse math operators and special characters
Place figures faster with AI
Octree's AI editor sizes and captions your images, fixes “File not found” and float placement problems, and compiles your document in seconds.
Try Octree Free →