Reader small image

You're reading from  LaTeX Beginner's Guide - Second Edition

Product typeBook
Published inOct 2021
PublisherPackt
ISBN-139781801078658
Edition2nd Edition
Tools
Right arrow
Author (1)
Stefan Kottwitz
Stefan Kottwitz
author image
Stefan Kottwitz

Stefan Kottwitz studied mathematics in Jena and Hamburg. He works as a network and IT security engineer both for Lufthansa Industry Solutions and for Eurowings Aviation. For many years, he has been providing LaTeX support on online forums. He maintains the web forums LaTeX and goLaTeX and the Q&A sites TeXwelt and TeXnique. He runs the TeX graphics gallery sites TeXample, TikZ, and PGFplots, the TeXlive online compiler, the TeXdoc service, and the CTAN software mirror. He is a moderator of the TeX Stack Exchange site and matheplanet. He publishes ideas and news from the TeX world on his blogs LaTeX and TeX. Before this book, he authored the first edition of LaTeX Beginner's Guide in 2011, and LaTeX Cookbook in 2015, both published by Packt.
Read more about Stefan Kottwitz

Right arrow

Chapter 5: Including Images

Documents do not only consist of text. You might wish to include pictures, diagrams, or drawings made with other programs. In this chapter, we will learn how to insert such images with optimal quality and good positioning.

We will now learn about the following topics:

  • Including an image
  • Managing floating images

By the end of this chapter, we will know how to position images within text exactly as we want.

Technical requirements

You need to have LaTeX installed, or you can use Overleaf. You can also edit and compile on the book's web page at https://latexguide.org/chapter-05.

The code for this chapter is available on GitHub at https://github.com/PacktPublishing/LaTeX-Beginner-s-Guide-2nd-Edition-/tree/main/Chapter_05_-_Including_Images.

In this chapter, we will use the following LaTeX packages: babel, blindtext, capt-of, float, graphicx, pdfpages, and wrapfig.

We will also briefly talk about these packages: afterpage, caption, epstopdf, eso-pic, microtype, placeins, rotating, subcaption, subfig, subfigure, and textpos.

Including an image

For including images, the standard package is called graphicx. The x in the name means it's extending the original, but outdated, graphics package.

We will create a short document, and between two paragraphs, we would like to insert a picture. We can do so with the help of the following steps:

  1. Begin a new document and load babel and blindtext to print some filler text, as follows:
    \documentclass[a5paper]{article}
    \usepackage[english]{babel}
    \usepackage{blindtext}
    \usepackage{graphicx}
    \pagestyle{empty}
    \begin{document}
    \section{Including a picture}
    \blindtext
  2. Open a figure environment and declare \centering, as follows:
    \begin{figure}
      \centering
  3. Use the \includegraphics command with the filename as the argument. We will use example-image as the filename since that's a sample image included with TeX Live. The code is illustrated in the following snippet:
      \includegraphics[width=4cm]{example-image}
  4. Set a caption...

Managing floating images

When a page break occurs, regular text can be broken to continue onto the next page. However, the automatic page breaking cannot divide pictures. That's why LaTeX provides a floating environment—namely, figure. Such floating environments are also called floats. LaTeX may push their content, including captions, to a place suitable for page layout and page breaking.

We will now figure out how to deal with this.

The figure environment takes an optional argument affecting the final placement of the figure. We will test the effect in our graphics example, as shown here:

  1. Go back to the previous example we had for Figure 5.1. This time, add the h and t options in the highlighted line (where h and t stands for here and top), as follows:
    \begin{figure}[ht]
      \centering
      \includegraphics{example-image}
      \caption{Test figure}
    \end{figure}
  2. Compile the document, and take a look at the output. It should look like...

Summary

In this chapter, we have learned how to include images in our document. We now know which file types we can use and how to place figures in our document.

LaTeX can generate a list of figures like a table of contents. We will deal with such lists in Chapter 8, Listing Contents and References.

As our figures are numbered, we can use these numbers to refer to them within the text. In Chapter 7, Using Cross-References, we will figure out how to do this, benefiting from LaTeX's built-in cross-referencing capabilities.

In the next chapter, we will work with tables, and we will see that table positioning is very similar to figure positioning.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
LaTeX Beginner's Guide - Second Edition
Published in: Oct 2021Publisher: PacktISBN-13: 9781801078658
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Stefan Kottwitz

Stefan Kottwitz studied mathematics in Jena and Hamburg. He works as a network and IT security engineer both for Lufthansa Industry Solutions and for Eurowings Aviation. For many years, he has been providing LaTeX support on online forums. He maintains the web forums LaTeX and goLaTeX and the Q&A sites TeXwelt and TeXnique. He runs the TeX graphics gallery sites TeXample, TikZ, and PGFplots, the TeXlive online compiler, the TeXdoc service, and the CTAN software mirror. He is a moderator of the TeX Stack Exchange site and matheplanet. He publishes ideas and news from the TeX world on his blogs LaTeX and TeX. Before this book, he authored the first edition of LaTeX Beginner's Guide in 2011, and LaTeX Cookbook in 2015, both published by Packt.
Read more about Stefan Kottwitz