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!
%%pdflatex
A picture of a Phoenix, as illustrated by FJ Bertuch.
\includegraphics{Phoenix_Fabelwesen.jpg}
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:
%%pdflatex
\includegraphics[scale=0.5]{Phoenix_Fabelwesen.jpg}
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:
%%pdflatex
\includegraphics[width=6cm, height=2cm]{Phoenix_Fabelwesen.jpg}
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
%%pdflatex
\includegraphics[width=3cm]{Phoenix_Fabelwesen.jpg}
scales the image to be 3cm wide, but scale the height to keep the aspect ratio.
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.
%%pdflatex
\includegraphics[width=0.5\textwidth]{Phoenix_Fabelwesen.jpg}
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.
%%pdflatex
\includegraphics[scale=0.5, angle=-45]{Phoenix_Fabelwesen.jpg}
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.
%%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.
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. |
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.
%%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.