2-4: Graphics, Figures, and Floats
LaTeX has a command \includegraphics
which lets you insert a picture into your document. This lesson also discusses the figure
and table
“floating” environments that are often used to make sure that your visuals align nicely with page boundaries, and how to add captions.
Including External Graphics
We will be using the graphicx
package throughout this lesson (don’t use the graphics
package, which is older). Once you include this package, you can ask LaTeX to include a graphics file by using the command
\includegraphics{filename.ext}
where filename.ext
is the name (with extension) of a graphics file which lives in the same directory as your .tex
file. For example, download the image file (on most browsers, right-click and “Save as” or “Save target as”) at
http://cemc1.uwaterloo.ca/~math600/wp/wp-content/uploads/2012/08/uw_seal.png
You’ll end up with a copy of uw_seal.png
on your computer; it is about 25 kilobytes in size. Create the following TeX file, making sure the image is located in the same directory as this file:
\documentclass{article} \usepackage{graphicx} \begin{document} The UW seal is: \includegraphics{uw_seal.png} \end{document}
Then, you’ll end up with the following document:
There are some technicalities worth mentioning here, which may also help you debug in case this doesn’t work for you.
- Whenever LaTeX runs on any system, there is a concept of “the directory in which LaTeX was executed” and this need not be the same as the location of the
.tex
file itself. All filenames will need to be relative to this execution directory. - Whenever you specify directories in LaTeX, you need to use the linux-style forward slash
/
separator. This is even true if you’re running at home on Windows. So you would writesubdirectory/filename.ext
, or to get to the parent directory write../filename.ext
- On most systems, you need to match the upper-case/lower-case of the directory and file names exactly.
- The probable source of a “bounding box” error is the following: most modern LaTeX systems run the command
pdflatex
in the backend by default, but you may be running vanillalatex
instead, which only accepts encapsulated postscript graphics. You should switch topdflatex
as the compilation mode instead. - If you’re using an online LaTeX editor, you’ll have to upload the image. In ShareLaTeX, the upload action is an icon at the top left.
Exercise
Not to be handed in; you can discuss all aspects of this in the discussion forum.Get the small example above working before proceeding. Including external files adds a new step of complexity beyond what we’ve done so far.
File Types
By default, \includegraphics
will accept three kinds of files:
- PNG files. This is a type of file with no graphics compression, sort of like a modern version of GIF with a better colour range. Typically logos and icons are in this format. The extension is
.png
or.PNG
. - JPG files. This is a type of file with intelligent compression, which is typically used for photography or very large images. The extension is
.jpg
,.JPG
,.jpeg
, or.JPEG
. - PDF files. These often have infinitely scalable “vector art” but can contain other things as well. The extension is
.pdf
or.PDF
.
We recommend converting each of your graphics files to one of these formats before including it in your document. (For the final project, we provide a tool to convert from .eps to .pdf in case you need it.)
\includegraphics
Options and Sizing
Often, the image that you include will be too small or big for the surrounding material. There is an easy way to fix this by scaling the image. There are three common ways to do this: specify a width
, a height
, or a scale
factor. They all have the same syntax:
\includegraphics[width=5in]{image.jpg}
\includegraphics[height=43mm]{image.jpg}
\includegraphics[scale=0.5]{image.jpg} % half as big as usual
In all three cases, the “aspect ratio” of the image will be preserved, rather than getting squished or stretched in one direction.
You can also rotate or crop an image by passing the angle
or trim
arguments to \includegraphics
. For more information, check out the relevant documentation.
Floats, Tables, and Captions
What we’ve seen above is the basic command for inserting an image, but often, putting a figure into your LaTeX document entails one more important step: enclosing the picture in a figure
environment. This environment has the following effects:
- The figure will be automatically floated (placed) somewhere that LaTeX thinks will look good, such as the top or bottom of the page or an adjacent page.
- You can add a caption to the figure, with all figures in the document numbered automatically; and you can refer back to it (see
\ref
, which we will cover next week). - You can centre the figure horizontally.
A figure doesn’t even need to contain a picture; here’s a brief example showing the most common usage.
\begin{figure} \centering % or, \raggedleft or right This is the inside of the figure. % or, use \includegraphics This is the inside of the figure. This is the inside of the figure. % long text, needs >1 line \caption{This is the caption.} \end{figure}
If you run this (with the usual boilerplate), you’ll get a single page with this in the middle of the page:
The Phloating Philosophy
The figure environment and its contents create a new float. This kind of container, which also exists in the web layout language HTML (see right), is separated from the text and placed in a more prominent location that is easier to read, such as the sides or centre. The idea is that you rarely want to interrupt the flow of the normal text with a big image; instead you will want it between nearby paragraphs or to the side; close by, but easier to view without disrupting the document.
LaTeX has its own system for determining where to place the float (much like it has its own image for determining line breaks, and avoiding one-line “orphan” paragraphs at the top or bottom of a page). But you may decide that you want to override this, which can be done by passing a positional hint argument to the environment:
\begin{figure}[«position»]
The «position»
can be:
t
for top orb
for bottomh
for herep
for a special floats-only paget!
,h!
, etc to give a stronger hint, asking LaTeX to pay less attention to left-over whitespace- lists like
htb
of hints
Note that even with a “hint,” LaTeX might not do the precise thing you were hoping for. There are packages to force placements, but we do not cover them in this course.
Tables are Floats
There is a table
environment which is identical to the figure
environment except in name and numbering, intended to be used for displaying tables of data. (You can even create new kinds of floats using the float
package.)
Exercise (nickname: 1000words
)
- re-size the picture that you have included;
- use a positional hint to change the location of the figure;
- take a
tabular
structure from the previous lesson and enclose it in atable
floating environment.
Your solution to Assignment 2 must include at least one included graphic, enclosed inside a figure
, with a caption
. As well, you must do at least one of the three things mentioned above. Don’t worry if the floats don’t end up appearing in proper sequence with your other solutions.
Note: Creating Graphics
Detailed information about how to produce graphics in the first place is beyond the scope of this part of the course, but here are the general options that you have:
- find software that can output to one of the formats above;
- GeoGebra can export to .PDF, .PNG, and pstricks (will be discussed next week)
- Maple can export graphics to a variety of formats (use our eps-to-pdf converter if needed)
- Popular tools for making .PDF drawings are Inkscape or IPE
- use arbitrary software such as PowerPoint, take a screenshot, and save a cropped portion as a .PNG;
- or use programmatic graphics: you’ll specify the entire drawing with commands instead of an external file. Week 3 will explain two packages that can do this —
pstricks
andpgfplots
.