Math on the Web: Latex Lesson 6
Graphics in LaTeX Documents

Including graphics in LaTeX documents

Today our main goal is to learn the basics of including externally produced graphics in our LaTeX documents.

LaTeX has a picture environment that makes it possible to draw simple pictures, and there are various packages to enhance this capability. For example, there is a package that physicists can use to draw Feynman diagrams. However, the picture environment has a reputation for being difficult to use. It does have one advantage: documents that use only the picture environment for producing graphics are completely portable (in the sense that they produce essentially the same output no matter what computer system is used to process them).

Since a major attraction of LaTeX is the portability of its formatting and document source files from system to system, it is unfortunate that there is no standard format for embedded graphics. This means that a LaTeX file that contains embedded graphics may not be completely portable. Still, it is not so difficult to put externally generated graphics into LaTeX documents.

So far as I know, the packages that are used for putting graphics into LaTeX documents all give instructions to the driver used for converting the .dvi file produced by LaTeX into a PostScript file so that the driver will take external PostScript files (with extension .ps), or Encapsulated PostScript files (with extension .eps) and incorporate them into the PostScript output. This means two things:

  1. You cannot view your included files with xdvi, but must use a PostScript viewer such as ghostview or, on the SGI platform, ShowPS
  2. Some packages cannot be used with every driver. For example, the epsf package works only with the dvips driver. Since the SUN and SGI networks have the dvips driver, driver, this is not a problem as long as we are using these networks.

On the Rutgers-Camden Unix servers clam, carp and crab, we have Tom Rokicki's driver dvips. If you happen to have a Postscript file at hand, you can get it into a LaTeX document by using Tomas Rokicki's package epsf, which is also on clam, carp and crab. We'll now get some Postscript files to use with LaTeX. For information direct from the author, follow the link Dvips: A DVI Driver Here is an extremely simple example of a Postscript file. You can cut it out with your mouse to make a file, call it greensquare.ps, that can be viewed in ghostview. Try it.


%!PS
%%BoundingBox 0 0 120 120
0.000 1.000 0.000 setrgbcolor
newpath
0 0 moveto
120 0 rlineto
0 120 rlineto
-120 0 rlineto
closepath
fill

If you want to know what the commands above are doing, you can try looking at Bill Casselman's course notes Mathematical Illustrations subtitled "A Manual of Geometry and Postscript" --well worth studying.


Now we will look at how you can include a Postscript file in a LaTeX document. First let's try incorporating the green square into a simple LaTeX document. Use your mouse to clip the following and create a file green-test.tex.

\documentclass[12pt]{article}
\usepackage{epsf}
\begin{document}
This is a test of the epsf graphics package.

\epsfxsize=4in \epsffile{greensquare.ps}
\end{document}


Then use
dvips green-test -o green-test.ps
ghostview green-test.ps
to view the output. You may have to wait a bit and search for the green square.

Here's how to get a blue rectangle:

 
%!PS
%%BoundingBox 200 400 320 620
0.000 0.000 1.000 setrgbcolor
newpath
200 400 moveto  
120 0 rlineto
0 220 rlineto
-120 0 rlineto
closepath
fill

Let's use a more interesting image. Try opening the following link to a Postscript file called golfer.ps, which generates a picture of a golfer. You should get a ghostview window containing an image. I thought that you could save this by using the control panel at the top left of your screen, but this doesn't work, so I am emailing each of you a copy, which you should save to the directory in which you are working now. Strip off the mailing information at the top, so the file starts with PS!.

Here is a simple exercise, which will put a box containing a picture of the golfer, resizing the horizontal dimension so that the box is 5 inches in the horizontal direction, and scaled proportionately in the vertical direction. Without the command \epsfxsize you will find that LaTeX complains about an overfull hbox. You should use your mouse to clip the LaTeX source file and run it as usual. Use dvipsfilename and ghostview filename Try temporarily removing the \epsfxsize command. Then try putting it back in with an argument of 4 centimeters, typed as 4cm. There is a corresponding command for scaling in the vertical direction. It is \epsfysize

\documentclass[12pt]{article}  %You don't need the dvips option for epsf
\usepackage{epsf}

\begin{document}
This is a test of the espf postscript graphics package
  %notice the blank line.  What if we left it out?
\epsfxsize=5in \epsfbox{golfer.ps}
\end{document}

Without the command \epsfxsize you will find that LaTeX complains about an overfull hbox. You should use your mouse to clip the LaTeX source file and run it as usual. Use dvipsfilename and ghostview filename Try temporarily removing the \epsfxsize command. Then try putting it back in with an argument of 4 centimeters, typed as "4cm", that is, with no space between "4" and "cm".

There is a corresponding command in epsf for scaling in the vertical direction. It is \epsfysize.


Getting PostScript Plots from Maple

Maple 8 is available on clam, carp and crab. It will produce PostScript files as output from plotting. Here is an example of how to get a more interesting graphic than the green boxes.

By default, Maple saves plots as full-page images in landscape mode. That is the reason in maplems.sty for rotating and scaling the included graphic. By giving Maple the command plotsetup(postscript, plotoutput=`myplot.ps`,plotoptions=`portrait, noborder,height=200,width=200`); before executing the plot command, you get Maple to save the plot (without opening a plot window) to a PostScript file named myplot.ps in portrait orientation, with no border, and with height and width of 200 points (where 1 inch is 72 points). Then in your LaTeX file, you could just put the command \epsffile{myplot.ps}.

Here is another example. Let's capture some graphics off the World-Wide Web to include in a LaTeX document. Click on one of the following links to art works on the Web and find a picture that interests you.

The Web Museum
A collection of famous paintings from around the world.
World Art Treasures
Includes a special program on art from Egypt, China, Japan, India, Myanmar/Burma, Laos, Cambodia and Thailand.
Civil War Photos
Works of Vermeer
Images from the Sistine Chapel
Crossing the Frontier Photo Exhibition
List from Yahoo where you can find many more links.

One of two situations will occur. Either you will get a fairly small image that appears inside your Web browser, or you will get a large image that shows up in its own window under the control of the graphics program xv. In both cases, you want to capture the image and save it to a file. The first case is the usual one, but the second may occur as well.

If the image is in its own xv window, skip this paragraph and the next one. If the image is inside the browser window, use the mouse to move the cursor over the image, and hold down the right mouse button. A pop-up menu should appear that has as an option "Save this Image As...". Select this option, make a note of the filename, and click on OK. You have just captured that image to a file.

Probably the image is in either gif or jpeg graphics format. We need to convert it to a PostScript file so that LaTeX dvi to Postscript driver can handle it. To do this, we just open the graphics file with the graphics program xv and then use xv to save the file in a different format. Open a terminal window and execute the command xv filename & where filename is the name that you just noted above.

You should have your image on screen inside an xv window. The xv program has a control panel that is hidden by default. To access the control panel, click the right mouse button in the window. Now click on "Save". A new window pops up that prompts you to save the file in one of a list of different formats. Depending on the version of xv, there is either a drop-down Format menu or a list of radio buttons. With the version here at Rutgers-Camden, there is a list of radio buttons. Select PostScript, type in a file name ending with the extension .ps and then click on OK. Another window pops up that offers options to adjust the size and resolution of the image. You can ignore this and click on OK again. You have just saved the graphics file in PostScript format. Now click Quit on the xv control panel.

Now open your test.tex file in a text editor and add the line \includegraphics{name.ps} where name.ps is the name you just saved the graphics file under. Save your test.tex and run through the latex, dvips, ghostview cycle again to see if your new image shows up properly in the processed LaTeX file.

Of course, when you print the result on paper, you will not get color output unless you can get your hands on a color printer.

A word of caution: graphics files are big. If you save a few large images on the SUN server, CLAM, you might get the message "Disc quota exceeded." It is therefore useful to know how to cut out a small piece of an image, say just a face, and save that. Open the image in xv, bring up the xv control panel, and click on the "Grab" button. If you move the mouse into the image and hold down the middle mouse button, you can draw a cropping rectangle. When you let the button up, xv will grab the region inside the rectangle and load it into a window. You can then save the cropped region into a new file.

Another way to save space if you are not going to be using a graphics file immediately is to use gzip filename to compress it. The compression factor for a Postscript file may be around 5 or 6. It will be much smaller for .jpeg because they are somewhat compressed to start with. The .gif files are already compressed fairly efficiently.


There is a project at the Temple University Mathematics Department, called Calculus on the Web (COW), which shows a student various problems in calculus and allows the student to use a form interface to submit answers to the problems. The answers are parsed, using a PERL script, and sent to MAPLE for checking, so the students get rapid feed-back. If you are interested, visit The COW website.


email: karel@camden.rutgers.edu :Martin Karel