Figures in LaTeX documents

Introduction

These days, figures (illustrations) are as important in documents as text. And to TeX, graphics are just like paragraphs: big rectangular things to be fitted into “boxes” on the page. But, unlike text glyphs, graphics don't come from font files; they come in various special file formats that require special handling, distinct from the treatment of text fonts.

The figures, like the glyphs that compose the text, aren't present in the *.dvi file that LaTeX produces. It merely reserves space for them. But they have to wind up in the *.ps file to be printed, and in the *.pdf file to be distributed. And, as the latex program knows nothing about figures, it requires the use of the graphicx package to do the work. So the proper inclusion of these figures in the final document requires still more special considerations, and opportunities for things to go wrong at every stage.

But because graphics are different from text fonts, they can't be handled by TeX's font mechanisms; instead, there are special LaTeX packages to deal with them. But, like fonts, there's still a fundamental difference between device-independent or vector graphics (like PostScript drawings) and raster graphics, which are resolution- or device-dependent. Different mechanisms (and different packages) are needed to include the different kinds of graphic files.

Graphics file formats

As far as LaTeX and its post-processors are concerned, there are only two main groups of graphics file formats: PostScript (PS), and everything else. PS is so ubiquitous that we'll deal with it first.

PostScript

PostScript figures

Because PostScript is so portable, and so capable of producing complex graphics as well as setting type, figures are usually produced in PostScript. And Level 2 PS can even include JPEG figures and decode them properly.

Note: all PS files must be “encapsulated” (i.e., they must contain a BoundingBox comment). Most programs that write PS files provide this automatically, even if they name the file *.ps instead of *.eps. But plain PS is easy to convert to EPS.

PostScript interpreter and re-formatting

Much of the file-reformatting at the PS stage (and later) is actually done with GhostScript, (gs), so it's necessary to have a good grip on the gs documentation, including its extensions to the PS language. For example, the ps2pdf command is really just a script that invokes gs to do the actual work.

The same thing is true if you use the Gimp: it uses gs as a plug-in to interpret PostScript. However, Gimp offers a simpler user interface to the PS interpreter.

In fact, almost every command that involves interpreting, displaying, or converting a PostScript file into some other format, actually uses gs to do the work. The only exceptions are xpdf and its associated utilities.

Vector and raster graphics in PS

Images created in PostScript are usually vector graphics, made with PostScript's lineto or arc and stroke or fill operators. But PS also supports raster images. So an EPS file may contain either vector or raster graphics.

Furthermore, other raster-graphics formats, such as the PBM/PGM/PPM/PNM family, can be converted to EPS files, as described here. Of course, any raster image converted to EPS is still a raster image.

Everything else

Now for the non-PS graphics files. These are all raster graphics; you need to keep resolution in mind when dealing with them. The most useful ones are:

Portable Net Graphics (PNG)
An improved replacement for GIF, with better compression. Good for line drawings and images with a limited number of colors.
TIFF
Scanners often produce TIFF images. They can be compressed; but many systems fail to recognize the better forms of compression. PNG is usually a better choice.
Portable BitMap graphics (PBM)
PBM [and its relatives, Portable GrayMap (PGM), Portable PixMap (PPM), and Portable aNyMap (PNM)] are good intermediates between various other forms, and so are widely used in format conversions. However, they are extremely bulky, and should be used only as intermediates, not as final formats.
JPEG
Lossy compression that is good for photographs. It can also be used for synthesized images that lack sharp edges. The tradeoff between information loss and compression can be adjusted; but the default level of 70 is usually satisfactory. See the docs for the  libjpeg-progs  package for details; or just  man cjpeg.  JPEG is understood by Level 2 PS interpreters; and there is a conversion command jpeg2ps that puts an EPS wrapper around any JPEG file.
Portable Document Format (PDF)
Images can be converted to Adobe's PDF format; then the resulting PDF file can be processed further (see below). A curious (and useful) feature is that EPS files are easily converted to PDF form, which can then be used by programs that can't accept PS directly.

What this list boils down to is that you should save photographs in JPEG format, and all rasterized line- and solid-color graphics in PNG format. These are the most compact formats.

Format conversions

Besides the interconversions between EPS and raster graphics discussed on another page, note the conversions mentioned above:

jpeg2ps
converts JPEG to EPS
epstopdf and ps2pdf
convert EPS to PDF

By default, epstopdf observes the BoundingBox, while ps2pdf produces a full-page PDF image; so epstopdf is generally the more convenient choice.

With these tools, you can convert any graphics file either to EPS, or from EPS to something else (usually PNG or PDF).

LaTeX and graphics formats

What is all this important? Because the graphicx package used with the latex command requires all graphics to be in EPS format; while the pdflatex command, perversely, requires graphics to be either JPEG, TIFF, PNG, or PDF — but not EPS.

So if you compile your LaTeX source file with the latex command, you'll have to convert any non-EPS files to EPS format. But, if you use pdflatex, you'll have to convert any EPS files to either PNG or (preferably) PDF format.

The use of the  graphicx  package is well documented in the graphics guide; try

gv /usr/share/doc/texmf/latex/graphics/grfguide.ps.gz
on any Debian box.

Printing

Finally, there are problems in printing figures on laser printers — particularly if scanned images are the starting point, instead of PostScript. Even if you start with PS, you can have problems in printing grayscale (as opposed to simple black-and-white) images. These matters are so complex that they are taken up on a separate page.

 

Copyright © 2005, 2006, 2008, 2010 Andrew T. Young


Back to the . . .
graphics-format conversion page or the

LaTeX formatting page or the

LaTeX top page

or the website overview page