Pictures and Figures

Overview:

  • Teaching: 20 min
  • Exercises: 0 min

Objectives

  • Understand how to add pictures to your document

Setup: Download the Picture

Before we begin, we need a picture to use so that we can demonstrate how to insert them into $\LaTeX$ documents! The picture we will be using can be seen below and downloaded here. You can also download the source code from the previous lesson to use as a template, if you want to follow word-for-word with this lesson.

Artist's impression of a Pheonix This image is that of a phoenix as depicted in a book of legendary creatures by FJ Bertuch (1747–1822).

Finally, make sure you include the graphicx package in your preamble, by inserting the command \usepackage{graphicx}. $\LaTeX$ can not manage images by itself, so we need to use the graphicx package.

Overleaf users:

Upload the picture to your project on Overleaf using the toolbar.

Local Editor users (non-Overleaf):

Make sure you save the picture to the same folder (directory) as the .tex file you are going to use in this lesson. Please also give it a memorable, yet not complicated, name! This name should not include full stops (except for the file extension) or spaces.

If you are familiar with paths, you can save it anywhere on your computer so long as you know the path to the file from the directory your .tex file is in.

Introduction

Images are essential elements in most scientific documents: they contain results, experimental diagrams and key concepts. The graphicx package provides $\LaTeX$ with a framework for handling images, and how they are formatted within a document. In this lesson, we will look at how to include images in your documents; and how to shrink, enlarge, rotate, and cross-reference them.

You can insert an image into a document simply by using the \includegraphics{file} command, where file is the name (plus extension) of the image file you want to include.

\documentclass{article}
\usepackage{graphicx}

\begin{document}
A picture of a Phoenix, as illustrated by FJ Bertuch.
\includegraphics{Phoenix_Fabelwesen.jpg}

\end{document}

NOTE: If you saved the file to a different folder to where your .tex file is, you will need to give the path to the file here. Because of the way we write these lessons, you will notice this difference when viewing our source code (available at the end of the lesson) - so please take care if you intend to use it in your own work!

In [1]:
%%pdflatex
A picture of a Phoenix, as illustrated by FJ Bertuch.
\includegraphics{Phoenix_Fabelwesen.jpg}
Out[1]:

Image Size and Rotation

Hooray - our image has appeared in our document! But we might decide that it's the wrong size, or we might be inserting a picture that's saved lanscape but want it displayed portrait. The \includegraphics command can also be given optional arguments to adjust these properties of the image, by including these options in square braces [ ] as follows:

In [2]:
%%pdflatex
\includegraphics[scale=0.5]{Phoenix_Fabelwesen.jpg}
Out[2]:

The scale argument tells $\LaTeX$ to resize the image, keeping the aspect ratio. The =0.5 specifies to which size we want the image scaled - in this case to 50% (0.5 as a percentage) of it's original size. However if we don't want to preserve the aspect ratio of our image, we can use the width and height keywords instead of the scale argument:

In [3]:
%%pdflatex
\includegraphics[width=6cm, height=2cm]{Phoenix_Fabelwesen.jpg}
Out[3]:

The parameters inside the brackets width=6cm, height=2cm define the width and the height of the picture, in centimetres. You can use other units of measurement including points and inches should you chose. If only the width parameter is passed, the height will be scaled to keep the aspect ratio, so the syntax

In [4]:
%%pdflatex
\includegraphics[width=3cm]{Phoenix_Fabelwesen.jpg}
Out[4]:

scales the image to be 3cm wide, but scale the height to keep the aspect ratio.

Scaling Relative to the Document Parameters

Quite often it isn't very useful specifying a given width or height in some fixed number of centimetres (or other units); instead you might want an image to be half a page wide, for example. However the length units we give to \includegraphics can also be relative to some elements in document: of particular importance is the unit \textwidth, which is the width of one line of text in your document.

In [5]:
%%pdflatex
\includegraphics[width=0.5\textwidth]{Phoenix_Fabelwesen.jpg}
Out[5]:

Instead of \textwidth you can use any other default LATEX length;

  • \textheight : height of the region on one page where the document places text (IE the space inside the margins)
  • \paperwidth : full page width
  • \paperheight : full page height

There is another common option when including a picture within your document, to rotate it. This can easily accomplished by passing the keyword angle to \includegraphics and specifying an angle in degrees anticlockwise. A negative angle will be interpretted as degrees clockwise.

In [6]:
%%pdflatex
\includegraphics[scale=0.5, angle=-45]{Phoenix_Fabelwesen.jpg}
Out[6]:

Positioning

So far we have only explained how to include images in your document, but the combination of text and images may not look as we expected. We might want our image to be centred on the page, or to have a caption, or even a label so that we can refer back to it later. To access this functionality we need to use a new environment, the figure environment.

The figure environment is used to display pictures as floating elements within the document. This means you include the picture inside the figure environment and you don't have to worry about it's placement, $\LaTeX$ will position it in a such way that it fits the flow of the document. An additional parameter can be passed in square braces to change the positioning of a figure. In the following example, the \begin{figure}[h] is used, providing an optional argument of h. This will result in $\LaTeX$ attempting to place the image "here"; if the image appears between two lines of text, or at the end of a paragraph, $\LaTeX$ will attempt to keep it there.

In [7]:
%%pdflatex
Phoenixes, with their healing ability and ease of flight, could effectively act quickly to prevent disaster in environmental crises.
\begin{figure}[h]
    \centering
    \includegraphics[scale=0.5]{Phoenix_Fabelwesen.jpg}
\end{figure}
Plus their natural bravery could be used to improve moral in such situations.
Out[7]:

The additional command \centering will centre the picture (width-wise) on the page. The default alignment is left.

Below is a table listing the other placement options for figures.

Parameter Interpretation Description
h Here Place approximately at the same point as it occurs in the source text (however, not exactly at the spot)
t Top Place at the top of the nearest page.
b Bottom Position at the bottom of the nearest page.
p Page Place this on it's own page.
! Override Place an exclaimation mark after one of the other letters to override what $\LaTeX$ thinks is a "good" place for the image to go. Essentially this forces the option you chose to be executed exactly, rather than in a way that $\LaTeX$ thinks suits the document layout.

Captioning, labelling and referencing

Using the figure environment also allows you to add a caption to an image, as well as a \label to refer back to the figure later in the document. You can then use the \ref command to have $\LaTeX$ fill-in your cross-references for you. Including a caption is done using the \caption command, typically you do this after the \includegraphics command in the figure environment. Note that if you include a \caption command before an \includegraphics command, your caption will appear above the image!

It is good practice to always include your label inside the caption, as this ensures that $\LaTeX$ associates the label to the figure rather than one constituant part of the the figure (which can cause numbering issues when the document renders).

Below is an example where we have added a caption to our image.

In [8]:
%%pdflatex
Phoenixes, with their healing ability and ease of flight, could effectively act quickly to prevent disaster in environmental crises.
\begin{figure}[h]
    \centering
    \includegraphics[scale=0.5]{Phoenix_Fabelwesen.jpg}
    \caption{A Phoenix, as depicted by FJ Bertuch.}
\end{figure}
Plus their natural bravery could be used to improve moral in such situations.
Out[8]:

\label when cross-referencing

As for cross-referencing and where to place the \label for your figure, we recommend the following:

Phoenixes, with their healing ability and ease of flight, could effectively act quickly to prevent disaster in environmental crises.
\begin{figure}[h]
    \centering
    \includegraphics[scale=0.5]{Phoenix_Fabelwesen.jpg}
    \caption{A Phoenix, as depicted by FJ Bertuch. \label{fig:PhoenixFJBertuch}}
\end{figure}
Plus their natural bravery could be used to improve moral in such situations.
Indeed there have been many images depicting their natural bravery and heroism, such as that in figure \ref{fig:PhoenixFJBertuch} illustrated by F.J. Bertuch.

Of course, if you are not going to refer back to an image and would like to prevent the automatic "Figure X" appearing in the caption, you can use a figure* environment instead.

Source Code

To download the source code that combines this lesson with the previous lesson, use this link.

Key points:

  • Inserting images requires the graphicx package.
  • Use the figure environment to handle images correctly, and control their alignment.
  • \includegraphics allows us to add images to our document, rotate and resize them.
  • Use \caption to add captions to figures, and place any \labels inside the \caption command.