Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 38 ยท Tables, Figures and Lists

Images and Figures

Insert images in LaTeX with graphicx and \includegraphics, set width, height, scale, rotation and trimming, choose file formats, place images side by side and organise figure folders.

Pictures turn a document from a wall of text into something readable. LaTeX does not embed an image in the text; it reserves a box of the picture's size and places the file into it when the PDF is made. All you need is the graphicx package and the \includegraphics command. This lesson covers sizing, formats, rotation, cropping and placing images next to each other. How to give figures captions and numbers belongs to the next lesson.

Loading the package and inserting an image

LATEX
\usepackage{graphicx}      % in the preamble

\includegraphics{photo}    % in the body: photo.pdf, photo.png or photo.jpg

You may leave out the file extension. LaTeX looks for the formats it supports for your engine and picks one. With a long source file in a figures subfolder, give the path:

LATEX
\includegraphics{figures/plot}

Use forward slashes on every system, and avoid spaces and special characters in file names (a file called my plot (final).png is a source of obscure errors). To avoid typing the folder name again and again, tell graphicx where to look:

LATEX
\graphicspath{{figures/}{images/}}   % note the double braces

Supported formats

Format pdfLaTeX / XeLaTeX / LuaLaTeX Best for
PDF yes Plots and diagrams (vector, sharp at every zoom)
PNG yes Screenshots and images with sharp edges
JPG yes Photographs
SVG via the svg package and Inkscape Vector graphics (extra setup)
EPS converted automatically in newer installs Legacy vector figures

Prefer vector formats (PDF) for charts and diagrams, because they stay sharp when printed or zoomed. For photographs and screenshots, use PNG or JPG at about 300 dpi for the printed size: a 10 cm wide figure needs about 1200 pixels.

Size, scale and rotation

Pass options in square brackets as key=value pairs.

LATEX
\includegraphics[width=0.5\textwidth]{photo}   % half the text width
\includegraphics[height=4cm]{photo}            % fixed height, width follows
\includegraphics[width=6cm,height=4cm]{photo}  % distorts unless keepaspectratio is added
\includegraphics[scale=0.7]{photo}             % 70 percent of natural size
\includegraphics[angle=90]{photo}              % rotate by 90 degrees
\includegraphics[width=\linewidth]{photo}      % fill the current line width

Notes:

  • \textwidth is the width of the text block; \linewidth is the width of the current environment (inside a list or column it is smaller). Fractions of one of these make images that adapt to the layout.
  • Give either width or height, not both, to keep the proportions. If you give both and want the picture not to stretch, add keepaspectratio.
  • trim and clip crop an image: trim={left bottom right top} removes margins and clip activates it. Example: \includegraphics[trim={1cm 0 1cm 0},clip,width=8cm]{photo}.
  • page=3 selects page 3 of a multi-page PDF; draft replaces the image with a box and file name while you edit, making compiles fast.

To see how width fractions behave without a compiler, use the stand-in below. It draws a page-like text block with a sample picture (a drawn sunrise, embedded in the code) scaled to the fraction you choose, and prints the matching \includegraphics line. It models the layout rule width = fraction x \textwidth.

Image width as a fraction of the text width (stand-in)

Centering and spacing

An image is a character-like box, so by default it sits at the left of the line like text. To center, put it in a center environment, or, inside a figure, use \centering:

LATEX
\begin{center}
  \includegraphics[width=0.6\textwidth]{photo}
\end{center}

Images side by side

The simplest approach is a minipage or two images that together fit the line:

LATEX
\includegraphics[width=0.48\textwidth]{left}\hfill
\includegraphics[width=0.48\textwidth]{right}

Note that the width fractions must add up to a little less than 1 (here 0.96) to leave room for the \hfill. A blank line between them would stack them instead, and a stray space or line break in the source adds an unwanted gap, so keep both on adjacent lines and end a line with % if needed.

For labelled subfigures use the subcaption package:

LATEX
\usepackage{subcaption}

\begin{figure}[htbp]
  \centering
  \begin{subfigure}{0.48\textwidth}
    \centering
    \includegraphics[width=\linewidth]{left}
    \caption{Before}
  \end{subfigure}\hfill
  \begin{subfigure}{0.48\textwidth}
    \centering
    \includegraphics[width=\linewidth]{right}
    \caption{After}
  \end{subfigure}
  \caption{A comparison, with subfigures (a) and (b).}
\end{figure}

The package numbers the subfigures (a), (b) automatically and lets you refer to them with \ref. You will build this in the next lesson in context.

Drawing instead of importing

Some figures are better drawn in LaTeX. The TikZ package (a whole chapter later in this course) creates diagrams that use the document's fonts, scale perfectly and can contain math. For data plots, pgfplots plots directly from numbers or CSV data. Use an external image when the picture is a photo or the output of a tool you already have, and TikZ for schematic diagrams.

A neat folder layout

Text
paper/
  main.tex
  figures/
    architecture.pdf
    results.png

With \graphicspath{{figures/}} in the preamble, the call is just \includegraphics{architecture}. Keep source files of figures (for example the .svg or .ipynb) outside the LaTeX folder or in a figures/src subfolder, and export PDFs from them.

Common problems

  • "File not found" or "Unknown graphics extension". The path or extension is wrong, the file name contains spaces or dots (photo.final.png), or you are using an EPS file where it is unsupported. Rename the file.
  • The image is too big and runs off the page. Add width=\linewidth or a fraction of \textwidth.
  • Blurred image. Raster images have too few pixels for their printed size; export a larger version or use a vector format.
  • PDF figure has large white margins. Crop it with trim and clip, or crop the file with pdfcrop.
  • Image positioned at a strange spot. That is floating, the subject of the next lesson.
  • Compile is slow. Use draft mode while editing text, and remove it for the final compile.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which package provides \includegraphics?

    graphicx provides \includegraphics and \graphicspath.
  2. 2. How do you scale an image to half the text width while keeping its proportions?

    Give only width (or height) to keep the proportions; keepaspectratio is needed when both are given.
  3. 3. Which format is best for a chart that must stay sharp at every zoom?

    Vector PDFs scale without loss; raster formats show pixels when enlarged.
  4. 4. What does \graphicspath{{figures/}} do?

    It adds a search path so you can write \includegraphics{name} without the folder.

Summary

  • Load graphicx and insert pictures with \includegraphics[options]{file}.
  • Use fractions of \textwidth or \linewidth for size; scale, angle, trim and clip transform and crop.
  • Prefer PDF for diagrams and PNG or JPG for pictures; keep file names simple.
  • Place images side by side with fractions below 1 and \hfill, or use subcaption.
  • A caption and numbering need a figure float.

Next: Floats, Captions and Placement