Figures

Not to discourage any pure mathies out there, but if you can't get a pretty picture out of your work at the end of the day, well, maybe you should take up painting or something. And what good is a pretty picture if you can't show it to anyone unless they're looking at the terminal over your shoulder.

This page won't tell you how to turn your work into a picture. You've got to have a .ps or .eps or .tex file in your hands already. There's a million ways to get these---gnuplot, xv, xfig, Maple, Mathematica, matlab, you name it. So once you've got your picture, how do you get it into a LaTeX document? Here's some ways that seem to work:

[Table of Contents]

epsf

The style file epsf.sty is a collection of macros for including encapsulated postscript files inside LaTeX documents. You can check whether or not your figure is PostScript or Encapsulated PostScript by reading the first line of the figure file:
PostScript
%!PS-Adobe-2.0
Encapsulated PostScript
%!PS-Adobe-2.0 EPSF
Despite what you may hear, it is really easy to get simple figures using this macro. To get it loaded, use
\documentstyle[epsf]{article}
at the beginning of your source. Or \input{epsf} before you need it. Then you use the macro \epsffile (notice the double f) to include your .eps file:
\begin{figure}
\epsffile{foo.eps}
\caption...
\label...
\end{figure}
This puts in the figure, all right. But it just puts it in at its natural size, and probably way over on the left margin. One thing at a time...

Setting the size of the eps file

Before you use \epsffile, you can specify the height or width of the incoming figure with
\epsfysize=5in
\epsfxsize=4.4cm
\epsffile....
respectively (notice the "y" and "x" in the middle of these words...) These commands scale the PostScript to the desired dimensions. If you only specify one, the other is changed to keep the shape of the picture. You can probably specify both and stretch the figure around, but this will most likely wreck the label fonts and so on.

The Bounding Box

What turns an ordinary PostScript file into an Encapsulated PostScript is the Bounding Box, a box that describes where the figure sits on the page. You can read what the Bounding Box is for your figure by looking through the first few lines of the .eps file for the line
%%BoundingBox:  0 0 453 216
                | |  |   |_distance from bottom of page to top of figure
                | |  |_distance from left side of page to right side of figure
                | |_distance from bottom of page to bottom of figure
                |_distance from left side of page to left side of figure
Or just do
grep BoundingBox foo.eps
In this example, the figure sits right down in the bottom left-hand corner of the page. The numbers are points with 1pt = 1/72 inches. So this figure is about 6 inches wide and 3 inches high.

Or so you think! The numbers in the Bounding Box are the very outsides of the region of the page where some PostScript stuff might happen. gnuplot, for instance, puts a lot of white padding around plots made with the set term postscript option. So centering the BoundingBox might not center the stuff that gets printed. To get around this "bug," you can enter your own BoundingBox dimensions. Coming up with the four numbers out of the blue is pretty well impossible, so your best bet is to add and subtract and test out what it looks like. For instance, to cut off a 1 inch wide band on the left and bottom of the example above, you would call \epsffile like this:

\begin{figure}
\epsffile[72 72 453 216]{foo.eps}
\caption...
\label...
\end{figure}
This is usually pretty trial and error, but it can be done. You can try to use Ghostview or BBfig to find some suitable dimensions.

Centering Figures

For some reason, if you just randomly stick in a \begin{center}...\end{center} around, or even inside, the figure environment, LaTeX, to quote Jim, blows chunks. You have to insert a magical \leavevmode in at the appropriate spot:
\begin{figure}
\epsfxsize=5.5in
\begin{center}       <-------------------------- start center
\leavevmode       <---------- \leavevmode!           |
\epsffile{myhome/foo.eps}                            |
\end{center}         <-------------------------- end center
\caption{...}
\label{...}
\end{figure}
Here's another way to center a Figure, which uses \centerline and \hbox rather than \leavevmode. Thanks to Dave for the idea and the debugging. This example uses epsf but you can modify it to include whatever figure format you have.
\begin{figure}[htbp]

   \centerline{\hbox{
   \epsfxsize=4.0in
   \epsffile{path/figure.eps}
     }
  }
  \caption{This is a caption refering to 
           Dilber and Lewis \protect\cite{D&L:variational} 
          }
  \label{fig:test}

\end{figure}
Notice the \protect in the caption to cite a reference.

\label and \caption

Don't ask---it just is: If you want to be able to refer to a Figure in the text, then you have to give it a label, just like you do for equations, sections, etc. To get things to work, you cannot put the \label line before the \caption line. You have two choices:
  1. Put the \label inside the \caption: you really refer to "Caption 5.2" which is, of course, associated only with Figure 5.2.
    \begin{figure}
    ...
    \caption{\label{myfigure-fig} The caption for this Figure.}
    \end{figure}
    
  2. Put the \label after the \caption: you refer directly to Figure 5.2
    \begin{figure}
    ...
    \caption{The caption for this Figure.}
    \label{myfigure-fig} 
    \end{figure}
    
The second one is a bit easier to use because the label sits on a line all by itself and it might be easier to find and edit.

The picture Environment

The picture environment is just like any other LaTeX environment that you \begin and \end. You can use it to make your own simple figures, or use it to assemble more complicated figures. You should have a look at "figures within figures" to see the basics of this environment.

The key thing to note is that in the picture environment, all you can do is \put. But inside the \put, you can do just about anything.

figures within figures

Lines and Arrows

Inside a LaTeX picture, you can put lines and arrows, among other things (see Lamport, p.102ff). As Lamport admits, these objects are just a bit tricky to use:
lines
LaTeX doesn't actually draw lines between two points like, say, PostScript does. Instead, there is a library of line segments of various slopes that LaTeX glues together to make the line you want. This is a small library, which is why some care must be taken. You get a line with the line
\line(x,y){length}  <---- notice the mixture of () and {}
where (x,y) gives the slope of the line, and length gives something like the length. Remember that where the line starts is given by the \put statement.

slope
The numbers x and y set the slope of the line to x/y. Sounds simple, huh? Except:
  • Both must be integers between +6 and -6.
  • The fraction x/y must be in its simplest form.
  • If one is 0 the other can only be +1 or -1.
"length"
If the line is not vertical (x not equal to 0), then length is the horizontal component of the line. Positive x gives a line to the right, negative x gives a line to the left. If the line is vertical, the length is the height (both positive and negative) of the line.
arrows
LaTeX has the same problems with arrows that it has with lines, but worse. You get an arrow with
\vector(x,y){length}
Again, the numbers x and y set the slope of the line to x/y, and the arrow head appears on the far end, the end away from the \put. For arrows, however

You can use these lines and arrows on top of an existing .eps figure to point features. Just like in figures within figures, put can \put down a figure, then an arrow and then some LaTeX labels or text. To find out just what coordinates use in the \put, you can try Ghostview or BBfig.

Labels and Text in a Figure

Another thing you can \put into the picture environment is plain old LaTeX text. This is a great way to add LaTeX labels to figures generated by programs that don't have greek fonts and stuff. Just as with lines and arrows, you just find the coordinates of the point where you want to add something, and
\put(x,y){Put some Mathematical $\Tau\epsilon\chi\tau$ here.}
You cannot rotate the text to make a label for the y-axis (at least, not with picture.) You might be able to get something reasonable by \put'ting down a \shortstack{}, but any math in the label might look a bit funny.

Finding coordinates using Ghostview

If you look at the raw .eps or .ps file using Ghostview, you'll see in the top left a set of coordinates.

Ghostview

Guess what? The numbers (68,597) are points, the default unit of measurement in the picture environment. So all you have to do is move the cursor around and read off the coordinates. Now, they might not be quite right, because who knows if the coordinates you use in the \put point to the middle of the label or what? So you might have to do some fine-tuning later.


Two Side-by-Side Figures

Maybe LaTeX isn't such a fragile thing, after all. One simple way to get two figures side-by-side, or more generally, an array of figures, is with an array (well, what did you expect?). Thanks to Lynn and Gerda.

Just open up an array environment and put figures in as entries. These entries can be

Here is the LaTeX source for this example:

This is the last line before Figure \ref{figtest-fig} starts---it should
be long enough to show the left and right margins.

\begin{figure}[h]
\begin{center}
$\begin{array}{c@{\hspace{1in}}c}
\multicolumn{1}{l}{\mbox{\bf (a)}} &
	\multicolumn{1}{l}{\mbox{\bf (b)}} \\ [-0.53cm]
\epsfxsize=2in
\epsffile{figure1.eps} &
	\epsfxsize=2in
	\epsffile{figure2.eps} \\ [0.4cm]
\mbox{\bf (aa)} & \mbox{\bf (bb)}
\end{array}$
\end{center}
\caption{The caption for Figure \protect\ref{figtest-fig}}
\label{figtest-fig}
\end{figure}

Here is a line of text after Figure \ref{figtest-fig}.  It is indented because
of the leading blank line.

Things to Notice:

  1. Math mode is turned on with a $, not \[.
  2. Extra space is added between the two centred columns with @{\hspace{1in}}.
  3. You can use \multicolumn to try to position the "(a)" and "(b)" labels. Their vertical position is adjusted with the [-0.53cm] at the end of the line.
  4. You can set the size of each sub-figure with \epsf?size.
  5. You can have "(aa)" and "(bb)" labels centred underneath the figures by putting another row in the array.
  6. Notice the \protect in the caption so that a reference can be made.

Boxes around Figures

If you don't like the way some Figures just trail off the page without a definate boundary, like (bb) below, you can add a black line around the figure like in (aa):

The LaTeX for this is the same as the side-by-side figures, except for

...
\framebox{   «--- open a "framebox"
\epsfxsize=2in
\epsffile{figure1.eps}
} &   «--- close it
...
Warning: The \framebox is drawn around the Bounding Box for .eps figures or around the outside of the region defined in the \begin{ picture} figures. You may have to adjust the dimensions by hand to make figure sit in the middle of the box.

The width of the line that draws the \framebox is set in the preamble by \fboxrule=0.5mm. You could probably change it each time you used it, too.


[Table of Contents] [Top of Figures]
Peter Newbury e-mail: newbury@math.ubc.ca
Last update: Added subequations 11 October 1996