Including Images
local graph
Chapter 6 — Including Images
Section titled “Chapter 6 — Including Images”In the previous chapter, we organized data using tables. However, not all information fits into rows and columns; sometimes, images communicate it better, such as diagrams, illustrations, screenshots, and photos. In this chapter, we’ll learn how to include images as figures, add captions, and reference them just like tables. We’ll also take a close look at how to control the placement of floating figures, a concept that applies to tables as well.
We will cover the following topics:
- Choosing the right image format
- Creating images
- Embedding images
- Scaling an image
- Including entire pages
- Putting images behind the text
- Managing floating figures
- Arranging multiple images
- Letting text flow around images
By the end of this chapter, you’ll be able to control the placement of images within your document with confidence and accuracy.
We will start by looking at the graphics formats.
Choosing the right image format
Section titled “Choosing the right image format”Let’s quickly look at the supported image file formats: EPS, PDF, JPG, and PNG. PS stands for PostScript, while EPS stands for Encapsulated PostScript. You’re already familiar with PDF, which stands for Portable Document Format. PDF files don’t have to be full pages; they can also be small images. And you know the image formats PNG and JPG, which are widely used for screenshots and photos.
If your image is already in one of these four formats, you can include it directly in your document, as these formats are supported by default. Converting between formats won’t improve quality. However, when you create or export an image, you can still choose the most suitable file format. Here’s how to decide.
EPS and PDF are both vector graphics formats. They are scalable and maintain high quality even at high resolution or when zoomed in. Therefore, whenever possible, you should prefer PDF (or EPS), for example, when exporting drawings or diagrams from other office software. Vector formats are typically used to achieve the best quality while keeping file sizes small.
PNG and JPG are bitmap formats, also known as raster graphics, and are commonly used for photographs and screenshots. When you zoom into bitmap images, they may lose sharpness as individual pixels become more visible, leading to blocky or blurred edges. PNG images use lossless compression, while JPG images may degrade in quality during saving. So, if you make screenshots, it’s best to use PNG; or, if you choose JPG, make sure no lossy compression is applied: usually, you can choose a quality level when saving. For photos, the JPG format is a good choice to keep the resulting PDF file size small because its compression works well with natural images that have soft color transitions. You’ll rarely notice a loss in photo quality, while the same compression can easily distort sharp lines in diagrams or screenshots.
In addition to supporting vector graphics, both EPS and PDF can also contain bitmap images. They are also referred to as container formats in general because they can embed multiple types of content within a single file, such as images, text, fonts, and multimedia content. We focus on single images here.
There are numerous tools available to convert between graphic formats. The following three programs are popular, and both TeX Live and MiKTeX include them:
dvipsconverts DVI (.dvi) files to PostScript (.ps) formatps2pdfconverts PostScript files to PDFepstopdfconverts EPS (.eps) files to PDF
These are command-line tools. Some LaTeX editors integrate them to provide single-click compiling, for example, from .tex to .dvi, then to .ps, and then to .pdf.
Since 2011, pdflatex calls epstopdf automatically when you include EPS images to convert them on the fly.
Creating images
Section titled “Creating images”Let’s briefly look at some powerful, free, and open source tools for creating, processing, and converting graphics you can include in your documents. All are available for Windows, macOS, and Linux. On Linux, check your app store or software repository first to see whether the program is available for your specific system version before downloading it from the official website.
Producing and processing bitmap images
Section titled “Producing and processing bitmap images”The following tools are especially recommended. All of them support many standard formats, including PNG, JPG, and GIF, and can export to PDF:
- GIMP (https://www.gimp.org) is an image editor often compared to Adobe Photoshop. It’s ideal for photo editing and retouching tasks. You can use it to crop, resize, sharpen, adjust colors, or combine multiple images. GIMP supports many standard formats, including PNG, JPG, and even Photoshop’s PSD files.
- Pinta (https://www.pinta-project.com) is an image editor inspired by Paint.NET, which is also free but available only for Windows. Compared to GIMP, Pinta has fewer features but uses far less memory and disk space. Its simpler design also makes it much easier to use.
- Krita (https://krita.org) is a full-featured digital art studio. You can paint with realistic brushes and layers and use it on tablets, making it great for digital illustrations.
- MyPaint (https://www.mypaint.app) is a lightweight, fast painting program that’s ideal for quick sketches and tablet drawing.
- ImageMagick (https://imagemagick.org) is a software suite for creating, editing, and converting images. Unlike graphical editors, it’s mainly used from the command line or in scripts to automate image processing and batch tasks. You can use it to crop, resize, rotate, adjust colors, or apply effects to images. It supports hundreds of file formats, making it especially useful for converting between them.
These tools cover most needs for working with bitmap images.
Editing vector graphics
Section titled “Editing vector graphics”The following tools use vector shapes, which keep your graphics sharp at any size. They work best with the SVG format, but can also export to bitmap formats when needed. The recommended workflow is to create your graphics in SVG and export them to PDF before adding them to your LaTeX document:
- Inkscape (https://inkscape.org) is a vector graphics editor for creating scalable artwork such as diagrams, icons, technical drawings, and illustrations. It provides tools for drawing shapes, paths, and text, as well as layers, gradients, and alignment options—features comparable to those found in professional design software.
- SVG-edit (https://github.com/SVG-Edit/svgedit) is a lightweight, browser-based SVG editor with basic features, ideal for quick edits without installation. You can use it at https://svgedit.netlify.app.
- draw.io (https://drawio.com) is a web-based tool for creating diagrams, flowcharts, and other structured drawings. You can use it directly in your browser at https://app.diagrams.net or install the desktop version, called draw.io Desktop, for offline use.
You can even create vector graphics directly in LaTeX. Since this is a more advanced topic, I wrote an entire book about it: LaTeX Graphics with TikZ, released by Packt Publishing. For now, we’ll focus on images created outside of LaTeX, such as photos and illustrations.
Embedding images
Section titled “Embedding images”To include images, the standard package to use is graphicx. The x in the name indicates that it extends the original (now outdated) graphics package.
Let’s walk through a short document where we insert a picture between two paragraphs. Follow these steps:
- Start a new document and load the
babelandblindtextpackages to print some placeholder text, as follows:\documentclass[a5paper]{article}\usepackage[english]{babel}\usepackage{blindtext}\usepackage{graphicx}\pagestyle{empty}\begin{document}\section{Including a picture}\blindtext - Begin a
figureenvironment and use\centeringto center the image:\begin{figure}\centering - Insert the image using the
\includegraphicscommand with the filename as the argument. We will useexample-imageas the filename since that’s a sample image included with TeX Live:\includegraphics[width=4cm]{example-image} - Add a caption, end the
figureenvironment, and end the document with filler text:\caption{Test figure}\end{figure}\blindtext\end{document} - Click Typeset to compile the document and check the result, as shown in the following screenshot:

Figure 6.1 – An image in a document
Figure 6.1 – An image in a document
The key command here is \includegraphics, where we specified the name of the file to be included. LaTeX loads this file if it exists; otherwise, it will display an error message. LaTeX supports the following file types we mentioned before:
- PNG, JPG, and PDF if you directly compile to PDF (using pdflatex)
- EPS if you compile to DVI and convert to PS and PDF (traditional LaTeX)
You don’t need to specify a filename extension, as LaTeX automatically searches for supported extensions. If multiple files share the same name but have different extensions, pdflatex selects the first one it finds in this order: .pdf, .png, .jpg, .jpeg.
Either put the file into the same directory as your document or specify a full or relative pathname, as follows:
\includegraphics{appendix/figure1}In file paths, use forward slash characters (/); don’t use backslash characters (\), as the latter begins a LaTeX command.
Go ahead and copy a picture of your choice into your document directory. Give \includegraphics its filename, and compile. LaTeX embeds the picture with its original size.
In the following sections, we will see how to add an image of a specified size, include a whole PDF page, or place it in the background behind the text.
Scaling an image
Section titled “Scaling an image”When inserting an image, you may choose a different size. The \includegraphics command supports this with a list of key-value options, as follows:
\includegraphics[key=value list]{file name}The graphicx package documentation, available at http://texdoc.org/pkg/graphicx, lists all keys and possible values. Here are the most popular ones and what they do:
width: Image width, for example,width=0.9\textwidthheight: Image height, for example,height=3cmscale: Scaling factor, for example,scale=0.5angle: Rotation angle, for example,angle=90
There are additional options, such as for clipping, but you can easily do such processing with any graphics software before you include it.
Instead of turning a figure by 90 degrees, you could also use the sidewaysfigure environment of the rotating package (see https://texdoc.org/pkg/rotating).
Including entire pages
Section titled “Including entire pages”What if we want to include pictures that are wider or taller than the text area? While the \includegraphics command can handle this, LaTeX may complain about the width or size and might even move them to the next page if there’s not sufficient space.
Using the pdfpages package, we can insert large images and even entire pages. The pdfpages package provides the \includepdf command, which can embed a single page, a partial page, or an entire multi-page PDF document at once. Despite its name, it can also include PNG and JPG files, not just PDF files.
An example of its basic usage could look like this:
\usepackage{pdfpages}…\includepdf[pages=-]{contract}% include entire contract.pdf\includepdf[pages=2-4]{spec}% include pages 2-4 of spec.pdfA common use is combining several PDF files into a single PDF file. We can also use the pdfpages package to resize several PDF pages and arrange them on a single sheet. For more details, take a look at the documentation at https://texdoc.org/pkg/pdfpages.
Putting images behind the text
Section titled “Putting images behind the text”Do you need watermarks? Background images? Textboxes positioned at arbitrary positions on the page? And all of this not interfering with other text? The eso-pic package does this for you.
In LaTeX Cookbook, you can read a step-by-step example of how to do this in the Absolute positioning of text section in Chapter 2, Tuning the Text.
The textpos package offers another approach. It’s developed for placing boxes with text or graphics at absolute positions on a page; see https://texdoc.org/pkg/textpos.
With a modern LaTeX installation, you can use a hook. That’s a place in the code where other code can be inserted to run at that moment, such as a callback or insertion point. In short, this is like \AddToHook{shipout/background}{<your image commands>}.
Now, we will take a look at dynamic image positioning.
Managing floating figures
Section titled “Managing floating figures”When a page break occurs, LaTeX can split regular text and continue on the next page. However, it cannot break an image across pages. That’s why LaTeX made the figure environment that we used in our first example, a floating environment. These floating environments are also called floats in short. LaTeX may push their content, including their captions, to a place that works well with page layout and page breaks.
Let’s look at how to address this.
The figure environment takes an optional argument suggesting the final placement of the figure. We will test the effect in our earlier example, as shown here:
- Return to the previous example from Figure 6.1. This time, add the
handtoptions in the highlighted line (wherehandtstand for here and top), as follows:\begin{figure}[ht]\centering\includegraphics{example-image}[width=4cm]\caption{Test figure}\end{figure} - Compile the document and take a look at the output. It should look like this:

Figure 6.2 – An image within the text
Figure 6.2 – An image within the text
- Change the options in the highlighted line to
!b(wherebstands for bottom), as follows:\begin{figure}[!b] - Compile again. The figure is now forced to float to the bottom, and now we see this page:

Figure 6.3 – An image at the bottom of the page
Figure 6.3 – An image at the bottom of the page
By adding some characters standing for placement options, we could force the figure to appear wherever we want.
There is a starred form—namely, figure* —in a two-column layout that puts a figure into a single column. In one-column mode, there’s no difference from the non-starred form.
Let’s now take a closer look at the positioning of figures. We will see how we can set preferences for where figures appear, such as at the top or bottom of the page, force immediate output, or at least limit floating, and arrange images next to each other or within the text flow.
Understanding placement options
Section titled “Understanding placement options”The optional argument of the figure environment specifies where LaTeX can position the figure. Four letters stand for four possible places, as outlined here:
hstands for here. The figure may appear right where we wrote it in the source code.tstands for top. It permits placing the figure at the top of a page.bstands for bottom. It allows he figure to appear at the bottom of a page.pstands for page. The figure can be placed on a separate page, where only floats may reside, but no regular text.
A fifth option might come in handy:
!overrides LaTeX defaults; it tells LaTeX to relax some internal constraints, increasing the chance of placing the float earlier.
If you don’t specify any option, LaTeX might place the figure far away, which can be surprising to new LaTeX users. Specifying more options helps to keep it as near as possible. The most flexible option is to use the [!htbp] placement, which allows the figure to go virtually anywhere. You can still consider removing a placement specifier if you don’t like the outcome.
Forcing the output of figures
Section titled “Forcing the output of figures”LaTeX holds figures and tables until they fit the page layout without violating LaTeX’s typographic rules. If you want to force LaTeX to print them, there’s a command to do so: the \clearpage command ends the current page and outputs all queued figures and tables. You can also use \cleardoublepage, which does the same but in a two-sided layout. It ensures that the next non-float page is a right-hand page. If necessary, it inserts a blank page.
Ending a page immediately, however, might not be the best approach, as it could leave a lot of empty space on the current page. The afterpage package offers an elegant solution. This package can delay the execution of \clearpage until the current page is ended, finished, as shown:
\usepackage{afterpage}...body text\afterpage{\clearpage}We may not need to use the afterpage package often, as we can simply run a \clearpage command at the desired location, such as at the end of a section. We can automate that case. Let’s look at this in the next section.
Limiting floating
Section titled “Limiting floating”As previously said, a figure may float far away, possibly even into the next section. The placeins package helps to restrict the floating. If you load placeins with \usepackage{placeins} and write \FloatBarrier somewhere in your document, no figure can float beyond that. This macro ensures that floats stay closer to their original place.
A very convenient way to prevent floats from crossing section boundaries is by stating the section option, as follows:
\usepackage[section]{placeins}This option inserts an implicit \FloatBarrier at the beginning of each section. Two further options—namely, above and below —allow you to lower the restrictions, preventing floats from appearing above the start of the current section or below the beginning of the next section.
Figures don’t pass into the next chapter because the \chapter command includes an implicit \clearpage.
Avoiding floating altogether
Section titled “Avoiding floating altogether”Do you want an image to appear exactly where you placed it in the code? The obvious answer is: in that case, don’t use a floating figure environment. You can use \includegraphics without a figure environment. For example, you could include and center an image by doing the following:
\begin{center} \includegraphics[width=4cm]{example-image}\end{center}Captions are designed for floating environments, though, so a \caption command won’t work here. If you still want to have a caption, you may use the \captionof command. The caption package, KOMA-Script classes, introduced in Chapter 3, Designing Pages, and the tiny capt-of package provide that command, which you can use in the following way:
\usepackage{capt-of}% or caption…\begin{minipage}{\linewidth} \centering \includegraphics{example-image} \captionof{figure}{Test figure}\end{minipage}The minipage environment keeps a picture and its caption together because no page break can occur in a minipage environment. The definition of \captionof is the same as \caption, except that there is an additional argument specifying the float type—in this case, figure, as illustrated in the following code snippet:
\captionof{figure}[short text]{long text}Be aware that the numbering could become inconsistent if you mix real floats and fixed figures. As you would give up LaTeX’s positioning capabilities, you have to take care that pages are still adequately filled.
The float package provides a convenient and consistent-looking approach to this. It introduces the H placement option, causing the float to appear right there, as illustrated in the following code snippet:
\usepackage{float}…\begin{figure}[H] \centering \includegraphics{example-image} \caption{Test figure}\end{figure}Choose the method that best fits your needs. When I first started with LaTeX, I used to think “don’t use a floating environment when you don’t want it to float” and preferred using minipage instead. These days, I recommend a figure environment with the H option for consistent syntax, and H is easy to change later into other options.
For more detailed information about float placement, see https://latex.net/floats, which collects explanations in several languages.
Arranging multiple images
Section titled “Arranging multiple images”To group several subfigures with individual captions inside a single figure, LaTeX offers several supporting packages you can choose from, outlined as follows:
subcaptionis a package for subfigures with individual subcaptions and belongs to thecaptionpackage. If you use hyperlinks in a document, this is the recommended choice, as it best supports hyperlinks. For hyperlinks, also see Chapter 12*, Using Hyperlinks and Designing Headings*.subfigis a sophisticated package that supports the inclusion of small figures. It handles positioning, labeling, and captioning within a single float.subfigureis still available for this purpose and you will find it online, but it’s no longer maintained, and you can consider it obsolete sincesubfighas appeared.
Don’t load two of these packages together. In general, loading two packages that serve the same purpose can lead to conflicts.
For aligning images, stacking images, or positioning them in a grid, examples are provided and explained in LaTeX Cookbook, Chapter 5, Working with Images.
Letting text flow around images
Section titled “Letting text flow around images”If you want to add a playful or visually appealing layout, you can let the text flow around an image or figure. We can achieve this using the wrapfig package and its wrapfigure environment.
Let’s modify our previous example that embedded an image (see Figure 6.3). We would like the image to appear on the left side, accompanied by the body text on the right side, with the help of the following steps:
- In our example code for Figure 6.3, additionally load the
wrapfigpackage, as follows:\documentclass[a5paper]{article}\usepackage[english]{babel}\usepackage{blindtext}\usepackage{graphicx}\usepackage{wrapfig}\pagestyle{empty}\begin{document} - Start an unnumbered section, and place a
wrapfigenvironment within some placeholder text, like this:\section*{Text flowing around an image}\blindtext\begin{wrapfigure}{l}{4cm}\includegraphics[width=4cm]{ example-image}\caption{Test figure}\end{wrapfigure}\blindtext\end{document} - Compile the document and take a look. You should see the following output:

Figure 6.4 – Text flowing around an image
Figure 6.4 – Text flowing around an image
The wrapfigure environment options differ from the figure environment. We used just two of them. If you need more, here’s the complete definition:
\begin{wrapfigure}[number of lines]{placement}[overhang]{width}The first optional argument states the number of wrapped text lines. If omitted, this would be automatically calculated from the height. The second argument, placement, can be one of the r, l, i, or o characters for the right, left, inner, or outer side, or the corresponding uppercase letters R, L, I, or O, with the same meaning, but allowing the figure to float. Only one character is allowed for specifying the option. The other optional argument, overhang, can set a width by which the figure should hang out into the margin; the default is 0 pt. The final—and mandatory—argument gives the width of the figure.
You can read more in the manual at https://texdoc.org/pkg/wrapfig.
Summary
Section titled “Summary”In this chapter, we covered how to include images in our LaTeX documents. You learned which image formats are supported and how to control figure positioning.
LaTeX can automatically generate a list of figures, similar to a table of contents. We will deal with such lists in Chapter 7*, Using Cross-References*.
Since figures are numbered automatically, you can cross-reference them within your text. In the next chapter, you’ll learn how to do that using LaTeX’s built-in referencing features.