LaTeX guide · Overleaf and desktop

LaTeX figures: insert, size, place and crop an image

To insert a LaTeX figure, load \usepackage{graphicx} in the preamble, then put \includegraphics[width=0.8\textwidth]{file} inside a figure environment with \centering, \caption{…} and \label{fig:…}. The width keeps the image inside the margins, the caption numbers it, and the placement letters [htbp] let LaTeX put it on the nearest page where it fits.

By André Antão Published 13 min read
The short answer
  1. Upload the image (PDF, PNG or JPG) to your project, ideally in a folder called images.
  2. In the preamble, add \usepackage{graphicx} and \graphicspath{{images/}}.
  3. Where the figure belongs, write \begin{figure}[htbp], \centering, \includegraphics[width=0.8\textwidth]{research-model}, \caption{…}, \label{fig:model} and \end{figure}.
  4. Refer to it as Figure~\ref{fig:model}, never as "the figure below", because LaTeX may move it to the next page.

01Insert a figure

A LaTeX figure has two layers. \includegraphics, from the graphicx package, places the image itself. The figure environment around it is a float: a box that holds the image and its caption, gets a number, appears in the list of figures, and can move to where it fits on the page. An \includegraphics on its own works too, but it sits in the line like a large letter, with no number and no caption.

This is a figure from my fictional thesis, Brand trust and customer loyalty in subscription services, as it stands in the chapter file:

main.tex (preamble and Chapter 3)
\documentclass[12pt,a4paper]{report}
\usepackage{graphicx}
\graphicspath{{images/}}

\begin{document}
...
\chapter{Research model}
The model in Figure~\ref{fig:model}
links brand trust to loyalty.

\begin{figure}[htbp]
  \centering
  \includegraphics[width=0.8\textwidth]
    {research-model}
  \caption{Research model}
  \label{fig:model}
\end{figure}
...
\end{document}
  1. Upload the file. On Overleaf, click the Upload icon at the top left of the file list, then drag the file in or click Select files. To keep images together, first create a folder with the folder icon, name it images, and upload into it. On your own computer, save the image next to main.tex or in an images folder beside it.
  2. Load graphicx in the preamble (between \documentclass and \begin{document}). Without it, \includegraphics stops the compile with "Undefined control sequence".
  3. Tell LaTeX where the images are with \graphicspath{{images/}}. Each folder goes in its own braces and ends with a slash; two folders are \graphicspath{{images/}{charts/}}. Paths are relative to the folder you compile from, which is the folder of the main file on your computer and always the project root on Overleaf.
  4. Write the figure environment as above. Give the file name without the extension: LaTeX tries the formats its compiler accepts in turn (LaTeX2e reference manual, \includegraphics).
  5. Compile. On Overleaf, click Recompile. On your own computer, run latexmk -pdf main.tex. The reference to fig:model shows "??" until the second run, which latexmk and Overleaf do for you.
Figure 1The figure environment and what it prints in the report class. The dashed outline marks the width, 0.8 of the text block, centred by \centering. The caption is numbered by chapter (3.1 is the first figure in Chapter 3) and written in the style "Figure 3.1: Research model", below the image because \caption comes after \includegraphics. The text before the figure refers to it by number.

On Overleaf: the Insert Figure button

Overleaf can write the environment for you. With \usepackage{graphicx} in the preamble, click Insert Figure on the editor toolbar, in either the Visual Editor or the Code Editor, and choose where the image comes from: Upload from computer, From project files, From another project or From URL. The dialog then asks whether to include a caption and a label and sets the width as a fraction of \linewidth; Insert figure adds the code. Pasting an image into the editor does the same, and Overleaf saves the pasted image as a .png file in the project (Overleaf docs, Using the Insert figure button; Pasting images).

Overleaf works the same on Mac and Windows; the only difference on your own computer is how you compile. Check the generated code once: it uses \begin{figure} like any other, so everything below applies to it.

02Set the size: width, height and scale

Without a size, LaTeX prints the image at its natural size, which for a PNG or JPG depends on the resolution saved in the file and is often far wider than the page. Always give a width, and give it as a fraction of the text block so the figure follows your margins if they change.

  • width=0.8\textwidth makes the image 80% of the width of the text block. \textwidth is the width between the margins.
  • width=\linewidth fills the current line. In normal text it equals \textwidth; inside a subfigure, a minipage or a two-column layout it is the width of that box, which is why the Overleaf dialog uses it.
  • height=0.4\textheight sets the height instead. Give width or height, not both, unless you add keepaspectratio.
  • width=\textwidth, height=0.5\textheight, keepaspectratio scales the image as large as possible without distorting it so that it fits inside both limits (LaTeX2e reference manual). Use it for a tall chart that would otherwise run off the page.
  • scale=0.5 halves the natural size. It is the least predictable option, because the natural size of a bitmap depends on its saved resolution.
Figure 2Red: an image wider than the text block. \centering cannot centre it: it starts at the left margin and runs past the right one (dashed lines), and the log reports "Overfull \hbox (… too wide)". Blue: the same image at width=\textwidth, which fills the space between the margins exactly.

If a wide figure really must be wider than the text, for example a full-width diagram in a thesis with wide margins, wrap it so it is centred on the page instead of starting at the left margin: \makebox[\textwidth][c]{\includegraphics[width=1.1\textwidth]{model}}. Check your university's minimum margins first; a figure in the binding margin is a common reason a thesis is sent back.

Tip

Use \centering inside the figure, not \begin{center}. The center environment adds its own vertical space, so a figure wrapped in it sits further from its caption and the text than the class intends (LaTeX2e reference manual, \centering).

03Control where it goes: [htbp], [H] and barriers

The letters after \begin{figure} list the places LaTeX may put the figure. LaTeX tries them for the first page where the figure fits and moves it on if none works.

  • h: here, at the point in the text, if there is room.
  • t: at the top of a page (this one or a later one).
  • b: at the bottom of a page.
  • p: on a separate page that holds only floats.
  • !: for this figure, ignore the limits on how much of a page floats may fill. It does not mean "here".

The default for figures in the standard classes is tbp. h on its own is not allowed: LaTeX adds t and, when the figure doesn't fit, says so in the log with "LaTeX Warning: `h' float specifier changed to `ht'". Write [htbp] and you give LaTeX every option, which keeps figures closest to where you mention them. A figure may fill at most 70% of a page at the top (\topfraction, LaTeX2e reference manual, Floats), so a taller one that doesn't fit where it is written can only go on a float page; without p, as in [ht], it often waits until the end of the chapter.

[H]: exactly here, with one catch

The float package adds [H], which stops the figure floating: it prints exactly where it stands in the source, and if there is no room, it starts a new page and leaves a gap at the bottom of the old one. Load \usepackage{float} and write \begin{figure}[H], with no other letters ([!H] stops the compile with "Unknown float option `H'").

The catch is the order. An [H] figure doesn't wait for earlier figures that are still floating, so it can overtake them. I compiled this case in the report class: the first figure, with [htbp], was too large for the rest of its page and floated to the next one, and the second, written a paragraph later with [H], printed on the first page. The figures were numbered in source order and printed in the wrong order, as below.

Figure 3Mixing [htbp] and [H]: the first figure, too large for the rest of page 14, waits for page 15, while the second, fixed in place with [H], prints on page 14. The list of figures then shows 3.1 on page 15 and 3.2 on page 14. Use [H] for all the figures of a chapter or none of them.

The cleaner fix: let figures float, but not too far

Most thesis guides ask for figures "as close as possible" to their first mention, not exactly at it. That is what floating does, as long as figures can't drift into the next section. The placeins package adds that limit:

  • \usepackage[section]{placeins} stops figures and tables from moving past the start of the next \section.
  • \FloatBarrier, from the same package, sets the limit at any point you choose.
  • \clearpage starts a new page and prints every figure still waiting. In the report and book classes, each \chapter already does this, which is why stray figures pile up at the end of a chapter.

With [htbp], placeins and references by number, the figures stay in order and close to the text, and the pages fill without the half-empty gaps that [H] leaves.

Forty figures, and half of them at the end of their chapter?I can place them for you

04Crop and rotate a figure

Charts exported from R, Stata, SPSS or Excel often come with a wide white border, or with a title you would rather give in the caption. LaTeX can crop them without editing the file.

trim and clip
% cut 3 cm left/right, 2 cm top/bottom
\includegraphics[trim=3cm 2cm 3cm 2cm,
  clip, width=0.7\textwidth]{scree-plot}
  • trim takes four lengths in the order left, bottom, right, top, the amount to cut from each edge. The order is the one people get wrong; it is not the CSS order (top, right, bottom, left).
  • clip actually hides what was trimmed. Without it, trim only shrinks the box LaTeX reserves, and the cut-off parts still print over the text and the caption around it.
  • The lengths are measured on the image at its natural size, before width scales it. Units can be cm, mm, pt or bp; a bare number means big points (1/72 inch).
  • viewport=llx lly urx ury with clip does the same job by giving the corners of the part to keep, measured from the bottom-left corner.
Figure 4A chart exported from R with a frame and a title outside the part to keep, cropped with trim. Red: with trim alone, LaTeX reserves only the smaller box (dashed), but the whole image still prints, so the frame and the title run over the text above and the caption below. Blue: with trim and clip, only the part inside the box prints, and the title moves to the caption. In my test the spill was invisible on a plain white border and obvious once the chart had a frame or a title, as here.

Rotate a figure

angle=90 turns the image 90 degrees anticlockwise. The keys are applied from left to right, so [width=5cm,angle=90] scales the image to 5 cm wide and then turns it (it ends up 5 cm tall), while [angle=90,width=5cm] turns it first and makes the turned image 5 cm wide (LaTeX2e reference manual). For a wide chart that should run along the long side of the paper, with its caption, use sidewaysfigure from the rotating package in place of figure: it turns the whole float, caption included, and puts it on a page of its own. Whether the page number then turns too, and which way the figure faces, is a university rule; check yours.

05Two images side by side

For panels that share one number, such as Figure 4.2a and 4.2b, use the subcaption package. Each subfigure takes a width, and inside it \linewidth is that width.

preamble: \usepackage{subcaption}
\begin{figure}[htbp]
  \centering
  \begin{subfigure}{0.48\textwidth}
    \centering
    \includegraphics[width=\linewidth]
      {loyalty-monthly}
    \caption{Monthly plan}
    \label{fig:monthly}
  \end{subfigure}\hfill
  \begin{subfigure}{0.48\textwidth}
    \centering
    \includegraphics[width=\linewidth]
      {loyalty-annual}
    \caption{Annual plan}
    \label{fig:annual}
  \end{subfigure}
  \caption{Loyalty by plan}
  \label{fig:plans}
\end{figure}

Two widths of 0.48 plus \hfill fill the line with a small gap. Keep the sum under 1, or the second panel drops to the next line. \ref{fig:monthly} prints "4.2a" in my compile; \subref{fig:monthly} prints just "a". For two images that should each have their own number (Figure 4.2 and Figure 4.3) side by side, put two minipage environments of 0.48\textwidth in one figure, each with its own \caption.

Figure 5Two subfigures of 0.48 of the text width in one figure, as the code above prints them: each panel has its own letter caption, and the figure has one number and one main caption. By default only the main caption, Figure 4.2, appears in the list of figures.

06File formats and image quality

Which formats work depends on the compiler, which Overleaf sets per project (gear icon › Compiler at the bottom left of the editor, or File › Settings › Compiler):

  • pdfLaTeX (Overleaf's default): PDF, PNG and JPG. EPS files are converted to PDF during the compile; in TeX Live this happens automatically through the epstopdf package, which leaves a file ending -eps-converted-to.pdf next to the original.
  • XeLaTeX and LuaLaTeX: PDF, PNG, JPG and EPS (Overleaf docs, Selecting a TeX Live version and LaTeX compiler).
  • LaTeX (the old DVI route): only EPS and PS.
  • SVG: none of them read it directly. Export the drawing as PDF instead, or use the svg package, which calls Inkscape and needs shell escape switched on (svg package manual).

For charts and diagrams, use PDF. A PDF chart from R (ggsave("plot.pdf")), Python (savefig("plot.pdf")) or a drawing program stays sharp at any size, keeps its text selectable, and prints in the same font sizes you chose. Use PNG for screenshots and JPG for photographs, exported at the size they will appear or larger; a bitmap stretched to \textwidth from a small file looks soft in print. For a page from a multi-page PDF, add page=3 to the options; whole PDFs, such as a questionnaire in an appendix, are easier with the pdfpages package.

Avoid spaces and extra dots in file names (research-model.pdf, not research model v2.final.pdf). Current LaTeX copes with spaces, and the reference manual shows how to brace extra dots, but Overleaf lists spaces, %, \ and ~ among the characters that cause "File not found" (Overleaf, File not found error).

07Figure not showing or in the wrong place? Fixes

These are the figure problems I meet most in theses that arrive in LaTeX, with the message as it appears in the log. I triggered each one in TeX Live 2025 to check the wording.

"LaTeX Error: File `research-model' not found"

LaTeX looked for the name with each extension it accepts and found nothing. Check, in this order: the file is uploaded; the name in \includegraphics matches the file name, letter for letter; the folder is in \graphicspath with its slash and braces, or written in the path (images/research-model); and on Overleaf, paths start from the project root even when the main file is in a subfolder (Overleaf, File not found error).

"LaTeX Error: Unknown graphics extension: .svg"

The compiler can't read that format. Convert SVG to PDF, or a TIFF or BMP to PNG, and change the extension in the code (or leave it out).

"Undefined control sequence" at \includegraphics

graphicx isn't loaded. Add \usepackage{graphicx} to the preamble. The same error at \graphicspath has the same cause.

"Cannot determine size of graphic in … (no BoundingBox)"

The project compiles with LaTeX (DVI), which reads only EPS. On Overleaf, switch the compiler to pdfLaTeX under gear icon › Compiler; on your computer, run pdflatex or latexmk -pdf instead of latex.

The image doesn't appear in the Overleaf preview

If the compile shows no error, check the compile mode: Overleaf's docs say Fast [draft] mode "often doesn't show included images". Choose Normal in the menu under the Recompile button. If the image still doesn't show, switch the PDF viewer from Overleaf to Browser. A frame with the file name in it instead of the picture means the document class has the draft option; remove it from \documentclass[…].

"LaTeX Error: \caption outside float"

The \caption isn't inside a figure (or table) environment. Wrap the image and caption in \begin{figure}…\end{figure}. If you need a caption on an image that must not float, use [H] from the float package, or \captionof{figure}{…} from the caption package.

"LaTeX Error: Not in outer par mode"

A figure environment sits inside a box: a minipage, a \parbox, a table cell or a framed box. Floats can't go there. Put the minipage inside the figure, not the figure inside the minipage.

The figure is off-centre, and the log says "Overfull \hbox"

The image is wider than the text block, so it starts at the left margin and runs into the right one (Figure 2). Set width=\textwidth or less. Outside a figure environment, an \includegraphics at full width also picks up the paragraph indent; start the line with \noindent.

All the figures pile up at the end of the chapter

One figure was too large for the places it was allowed, so LaTeX held it until the next \chapter command cleared the queue, and every later figure waited behind it, because floating figures always keep their order. Use [htbp], reduce the height of the largest figure, and add \usepackage[section]{placeins}. If one figure blocks the rest, put \clearpage after the paragraph that mentions it.

"LaTeX Error: Too many unprocessed floats"

So many figures and tables are waiting that LaTeX has run out of room to hold them: current LaTeX keeps up to 52 waiting floats. It is the same problem as the pile-up above, in a long chapter. Add \clearpage or \FloatBarrier at a few points (between sections is natural) so the queue empties, and loosen the placement letters.

Figure numbers out of order in the PDF or the list of figures

An [H] figure has overtaken an earlier floating one (Figure 3). Use the same kind of placement for every figure in the chapter: all [htbp] with placeins, or all [H].

"Figure ??" in the text

Compile again. If it stays, the \label comes before the \caption or has a different name from the \ref. The label must come after the caption, because it takes the number of the last numbered item before it.

Deadline close and the figures still won't behave?Send me the project and I'll fix it

08What your university decides

LaTeX's defaults give a working figure, but thesis guides differ on the details, so check yours for each of these:

  • Where the caption goes (below the figure in most guides, above it in APA style) and how it looks: bold label, italic title, a colon or a full stop.
  • Numbering by chapter (Figure 3.1, the report class default) or straight through (Figure 1, 2, 3).
  • Whether a source line or a note goes under each figure, and in what form.
  • How large a figure may be, and whether it may run into the margins.
  • How landscape pages are bound and numbered.

The caption settings for each of these, with the caption package, are in my guide to captions in LaTeX. Figures in APA style, with the number and title above and the note below, are covered in how to format a figure in APA 7. Every figure with a caption goes into the list of figures by itself; the list is set up in my guide to the LaTeX table of contents and lists of figures.

If your university supplies a LaTeX class or template, keep its figure settings and change only what the written guide contradicts. Starting from scratch, my guide to writing a thesis in LaTeX has a complete preamble, and the final checks before you submit are in my thesis formatting checklist.

09Questions people ask

How do I force a figure to stay exactly where I put it?

Load \usepackage{float} and write \begin{figure}[H]. It then behaves like a paragraph and never floats. Use it for every figure in the chapter, or an [H] figure can print before an earlier floating one. For most theses, [htbp] with \usepackage[section]{placeins} gives a better page.

What is the difference between \textwidth and \linewidth?

\textwidth is always the width of the text block between the margins. \linewidth is the width of the current line, which shrinks inside a subfigure, minipage, list or column. In ordinary text they are the same; inside a subfigure, use \linewidth.

How do I add a figure in Overleaf?

Upload the image, then either click Insert Figure on the toolbar, which writes the figure environment with a caption, a label and a width, or type the code yourself as in section 01. Both need \usepackage{graphicx} in the preamble.

Why does LaTeX move my figure to another page?

A figure is a float: if it doesn't fit in the space left on the page, LaTeX moves it to the top of the next page or to a page of floats, so that the text keeps flowing without a gap. Give it [htbp], make it smaller, or refer to it by number so its position doesn't matter.

Can text wrap around a figure?

Yes, with the wrapfigure environment from the wrapfig package, which takes a side and a width. Most thesis guides expect figures to span the text width with text above and below, so check yours before using it.

How do I make a figure span both columns?

In a two-column layout, use \begin{figure*} … \end{figure*} with width=\textwidth. A starred figure can only go at the top of a page or on a float page, never "here" (LaTeX2e reference manual, figure). Most theses are single-column, where figure already spans the full text width.

How do I put a whole PDF page in the thesis?

For one page as a figure, \includegraphics[page=2,width=\textwidth]{questionnaire}. For several pages in an appendix, the pdfpages package's \includepdf[pages=-]{questionnaire} inserts them all as full pages.

How this was checked

I compiled every snippet and every error message on this page with TeX Live 2025 (pdfTeX 1.40.27) on Windows 11 in November 2026, including the [H] ordering case, trim with and without clip, the order of angle and width, the EPS conversion and the 52-float limit (read from the LaTeX kernel source). The Overleaf steps come from Overleaf's own documentation, and the keys and placement rules from the LaTeX2e unofficial reference manual and the package manuals. The svg package's Inkscape requirement is from its manual and was not tested. All pages are listed under Sources.

André Antão is a university lecturer in Management and Marketing at the Polytechnic University of Portalegre and a PhD candidate in Management. He has formatted theses in Word and LaTeX for 8 years.

Sources

  1. Inserting images Overleaf docs
  2. Using the Insert figure button Overleaf docs
  3. Uploading images Overleaf docs
  4. Pasting images into your project Overleaf docs
  5. LaTeX code for including images in your document Overleaf docs
  6. Selecting a TeX Live version and LaTeX compiler Overleaf docs
  7. Resolving problems with missing images Overleaf docs
  8. Positioning images and tables Overleaf
  9. File XXX not found on input line XXX Overleaf
  10. \includegraphics LaTeX2e unofficial reference manual
  11. Floats, figure and \centering LaTeX2e unofficial reference manual
  12. The graphicx package (grfguide) CTAN
  13. The float package CTAN
  14. placeins – Control float placement CTAN
  15. The subcaption package CTAN
  16. The rotating package CTAN
  17. The epstopdf package CTAN
  18. The svg package CTAN
  19. The pdfpages package CTAN