Up one level
Including graphics/figures/diagrams in PDFLaTex and LaTex documents
Spencer Stirling

UPDATE (Feb 2011): Since the time of this writing I have occasionally used "TikZ" to generate figures in (PDF)LaTex. TikZ is a drawing programming language (similar to the older PSTricks or MetaPost languages if you are familiar with those). TikZ is NOT a WYSIWYG drawing program. The documentation on the web is extensive. I recommend starting with a very minimal introduction to TikZ, then moving on to the huge manual (google for the latest version). I also do commutative diagrams in TikZ.

TikZ is slightly tedious to learn and use. However the results are very good. TIP: I use an editor called kTikZ because it instantly compiles the resulting graphic in real time. TIP2 (This tip is unrelated to TikZ, but it is useful anyway): for very small figures I often use a "wrapfigure" environment in (PDF)LaTex. This makes efficient use of paper real estate, and also allows for many small figures that would otherwise be left out.

Including graphics from external WYSIWYG programs
If you do NOT want to use TikZ then you will need to generate your figures/graphics in an external drawing program. First, it is important to understand that the allowed graphics file types are very different between PDFLaTex and plain LaTex.

• PDFLaTex: can use PDF, JPEG, PNG, MetaPost (POSTSCRIPT and GIF are NOT allowed!)
• (plain) LaTex: can use POSTSCRIPT (EPS or PS), Metapost
These differences affect our strategies for including graphics in (PDF)LaTex.

Since we want good integration with (PDF)LaTex (i.e. the ability to use (PDF)LaTex commands and fonts in the graphics), we must be careful which external graphics programs we use to generate our figures. Here are my recommendations:

• Gimp: used for raster (bitmap) images such as photographs (raster graphics are NOT recommended for technical figures)
• xFig: very old vector graphics program. Useful and lightweight, but is a bit annoying for newbies. Try to get a version >= 3.2 pathlevel 5a since the integration with (PDF)LaTex is the most complete
• Inkscape: modern vector graphics program. If you get a version >= 0.48 then the (PDF)LaTex integration is almost as good as xFig. Previous versions had very poor (PDF)LaTex integration
• other: I do not recommend other graphics programs such as Tgif because the (PDF)LaTex integration is rather poor
Finally, if you want to generate commutative diagrams then use the XYPic TikZ package from within (PDF)LaTex. If you want graphs (of functions) then use Gnuplot TikZ.

The instructions below apply to pdflatex version 3.141592-1.40.3 (Web2C 7.5.6).

As far as raster graphics are concerned (pixel-by-pixel graphics such as photographs) there is one clear choice: the Gimp. The Gimp is similar to proprietary programs such as Adobe Photoshop (some people claim that it's superior).

If you do NOT need to use any (PDF)LaTex commands or fonts in your figures then the instructions are simple:

• PDFLaTex instructions: directly export (from the Gimp) to PNG, JPEG, or PDF. The resulting graphic can be used in your PDFLaTex document with an "\includegraphics" command (google it).
• (plain) LaTex instructions: export from Gimp directly to EPS format (warning: the file will probably be HUGE!).

Integrating (PDF)LaTex commands (and fonts) in the Gimp is still not possible (as far as I know). However, you can export the image to JPEG or PNG and then import that file into xFig or Inkscape (which have good (PDF)LaTex integration). For example, within xFig click on the "camera" icon, then drag out a rectangle and Right-click. This will bring up a dialog in xFig that will allow you to import the file. Note that the aspect ratio AND size can be altered with the "Scale" button. Now follow the xFig/Inkscape instructions below to include (PDF)LaTex commands.

TIP: you may want to purchase a tablet of some kind (e.g. the mid-sized Wacom 6x8 is all that you need - bigger isn't better). Check out my Wacom guide if you need help in Linux. I recommend the "Absolute" setting while using the Gimp.

TIP: if you do not care about quality, and if you only want black and white figures, then you may consider DRAWING YOUR FIGURES ON PAPER and scanning them in (make sure to scan in "black and white", otherwise it will look very bad). Tip: draw very large images on paper, then shrink them later. This hides a lot of flaws. Then you can open the image in the Gimp to further edit/clean the image (don't forget to crop it).

xFig or Inkscape
For vector graphics I recommend either xFig >= 3.2 patchlevel 5a, or Inkscape >= 0.48. xFig has a steep learning curve because the mouse commands are from the early days of X (remember the middle button!). I use it because I learned it a million years ago, and it is still a very useful graphics package. If you do not already know xFig then you might prefer learning Inkscape (>= 0.48) instead.

I have tried several types of hardware and I find that the trackball is the most useful (a simple touchpad works somewhat OK, too). This is because you can move to where you want and THEN click without accidentally moving the cursor - great for precision work. An ordinary computer mouse is terrible. I also combine my trackball (or touchpad) with a Wacom tablet for an even better experience (see below).

In the following I will describe 3 ways to put (PDF)LaTex commands into your xFig or Inkscape picture (these will be processed by (PDF)LaTex later). The first method is really all you need, although I'm leaving the other ones for historical reference.

Method 1: Export to Combined PS/PDF/LaTex (3 parts)
This method works for both PDFLaTex and LaTex. I will describe the method for xFig, although Inkscape version 0.48 or newer has similar functionality (actually Inkscape is more like older versions of xFig because it only exports the PDF part - see the paragraphs "Workaround for older xFig" below).

Suppose that you want to put (PDF)LaTex commands into a figure generated in xFig. Although xFig has built-in text capabilities, they are clunky. To include (PDF)LaTex fonts, select the text tool (the big T) and put it where you want (configuring the size/etc as usual). Now (BEFORE YOU TYPE THE TEXT!!!) you need to set the "special flag" to "special" (as opposed to "normal"). This can be found at the bottom of the screen in the "Text Flags" button. This flag tells xFig to NOT process the text but instead leave it for further postprocessing. So you can put in a (PDF)LaTex command like

$x=\text{sin}(\phi^4)$
and it will *eventually* be processed by (PDF)LaTex. You can make the "special" flag the default if you start xFig from the command line with these options:
xfig -specialtext -latexfonts -startlatexFont default

When your are finished drawing go to File->Export. Select "Combined PS/PDF/LaTex (3 parts)" for the Language. Accept the default file extension and export it. This creates 3 files:
foo.pspdftex and foo.pdf and foo.eps
Obviously two of these files encode the graphical part of the figure (one version for PDFLaTex and one for plain LaTex), and the third contains (PDF)LaTex commands.

Now, in your (PDF)LaTex document, you must have the following lines in the Header of your document (after the "\documentclass" line)

\usepackage{graphicx}
\usepackage{color}

Both are absolutely essential (the color thing is dumb, but necessary).

Within the body of your document you can include the picture with the stanza

\begin{figure}[h]
\centering
\input{foo.pspdftex}
\caption{A really neato picture}
\label{fig:abigfoo}
\end{figure}

Now you can compile your document in either PDFLaTex or LaTex. VERY IMPORTANT SIDE REMARK ABOUT (PDF)LATEX WEIRDNESS: if you give the figure a "\label" then make sure that is the LAST line in the stanza - otherwise it won't work as you expect. I don't know why this is - maybe a bug?

Workaround for older xFig
xFig only introduced the above 3-file strategy after patchlevel 5a. Before that it allowed for either "Combined PDF/LaTex" or "Combined PS/LaTex" but not both. Here is a workaround if you have an older version.

Follow the instructions for the previous section, although instead export to "Combined PS/LaTex". For the "Output File" type "foo.eps" (CHANGE THE EXTENSION TO .eps!!! xFig has an old bug where it won't work if you accept the default ".pstex" extension). Also, you need to actually hover the mouse over the textbox to type in it (an ancient remnant from the early days of X)

This creates two files

foo.eps and foo.eps_t
We're NOT DONE YET. This created a Postscript file, which cannot be used by PDFLaTex. Now we need to create an EXTRA PDF file from the EPS file with the command (you may need to download extra software for this command):
epstopdf foo.eps
We're STILL NOT DONE YET!!! Now go in and edit the file "foo.eps_t" and remove the ".eps" extension, i.e. change the line
\includegraphics{foo.eps}%
to
\includegraphics{foo}%
Now we have the same 3 files as above (except the file extension is ".eps_t" instead of ".pspdftex").

Method 2: Export to Metapost
This method works in both PDFLaTex and (plain) LaTex, but it is outdated. Also, it only supports basic LaTex commands (no extended font packages such as AMS).

Draw your image in xFig with (PDF)LaTex commands using the "special" text option (as described in the previous method). Then go to File->Export. Select "MetaPost" for the Language. In the "Output File" field put "foo.mp". It is IMPORTANT to put the file extension ".mp" because xFig won't do it for you. Both xFig and Metapost will respect your "special" flag and happily pass them to (PDF)LaTex.

Once the figure has been exported to "foo.mp" then it needs to be compiled with the MetaPost compiler (if you know the MetaPost language then you can first edit "foo.mp" to manually tweak anything). Run the command

mpost foo.mp
This creates a "MetaPost output" file called foo.0 (or foo.1 or foo.2 etc). Incidentally, this file is actually pretty close to Postscript (but it's NOT). It can be included into (PDF)LaTex documents. To do this you MUST do the following (maybe there's a better way, but this works): rename "foo.0" to "foo.mps". (PDF)LaTex doesn't know what foo.0 is, but it DOES know what .mps files are! Then include the graphics using the usual
\begin{figure}[h]
\centering
\includegraphics{foo.mps}
\caption{A really neato picture}
\label{fig:abigfoo}
\end{figure}

(make sure that you have \usepackage{graphicx} in the Header).

TIP: if you are manually tweaking a MetaPost file then you will probably want to preview it from time to time. Since the .mps format is a slightly modified version of Postscript there is a small utility called "mps2eps" that converts the mps file to eps for previewing: it can be found here. Just put this script into a directory in your PATH and type

mps2eps foo.mps
and VOILA... foo.eps is ready to be previewed.

Method 3: PSFrag method
This method is also outdated. Just to mix things up a little, I will give instructions here for Inkscape (although the following applies to xFig as well). You must be using plain LaTex (not PDFLaTex) to use the "PSFrag" package. NOTE: there is another option in Inkscape to export to "LaTex PSTricks" format. PSTricks is an old graphics programming language that is being replaced by TikZ. If you do not already know PSTricks then do not bother with this option.

First, create your graphic in Inkscape. Wherever you want a LaTex command put in a unique string like "eqn1" or something. Then you can save the picture in EPS format TIP: Inkscape by default will NOT save your text AS TEXT in the EPS file. It will DRAW the letters. You DON'T WANT THIS. Make sure that when you save to EPS you UNCHECK the option "convert text to path".

Within your LaTex document you need to include the package

\usepackage{psfrag}
Then you can include the picture with a stanza like this
\begin{figure}[h]
\centering
\psfrag{eqn1}{$x=\frac{\cos(y)}{z}$}
\input{foo.eps}
\caption{A really neato picture}
\end{figure}
When the graphic is rendered this will replace everywhere in "foo.eps" the string "eqn1" with the LaTex command "$x=\frac{\cos(y)}{z}$". Note that you WON'T see this replacement in the DVI file produced by LaTex. You will ONLY see the changes after you convert DVI to PS with the command "dvips".

Wacom TABLET setup for xFig or Inkscape
In this section I want to describe how I combine a Wacom tablet with the trackball for a better xFig/Inkscape experience. Again, a small tablet (e.g. 6x8) is fine (bigger is not necessarily better). Also, a touchpad works OK if you do not want to buy a trackball. I use the trackball in one hand, and the Wacom stylus (pen) in the other.

Inkscape is designed to integrate with the tablet, hence no further configuration is required. xFig, on the other hand, needs some tweaking (xFig just treats the tablet like a mouse). In xFig I only use the pen for carefully-controlled cursor movements. Unfortunately, by default the pen will send a "button press" signal whenever it touches down on the tablet. This is horrible for xFig. I only want a click when I explicitly press the trackball button with my other hand. In order to disable the click I increase the "Threshold" option for the pen. Also, I like to put the pen in "Relative" mode, and slow it down.

Here are instructions for Xorg version 1.7 or newer (note that both Xorg and the Linux Wacom project have changed DRASTICALLY from previous versions. There is no more "xorg.conf" file, and the "xsetwacom" utility is no longer necessary).

First, you need to make sure that the command "xinput" is installed on your system (in Debian Squeeze this is the "xinput" package). Then, after plugging in your Wacom tablet, you can determine what the tablet is named with the command

xinput --list

In my case this spits out a huge list, and in there I find "Wacom Graphire4 6x8". Now that I know its name, I can list its properties with the command
xinput --list-props "Wacom Graphire4 6x8"

There are 2 properties that we are interested in: "Device Accel Constant Deceleration" and "Wacom Pressure Threshold". I can slow down the stylus and disable the click with the following commands
xinput --set-prop "Wacom Graphire4 6x8" --type=float "Device Accel Constant Deceleration" 2.00000
xinput --set-prop "Wacom Graphire4 6x8" --type=int "Wacom Pressure Threshold" 2000

Now I want to put the tablet into "Relative" mode. The following command takes care of this:
xinput --set-mode "Wacom Graphire4 6x8" RELATIVE


Diagrams and Graphs in (PDF)LaTex using XYPIC and GNUPLOT
NOTE: The following has been superceded by TikZ, but I leave it for historical reference.
For commutative diagrams I have nothing to add (at this time) to the official XY-Pic page (scroll down for some good tutorials).

For graphs there IS a package called "graphs.sty" but I've been a little too annoyed in general to figure out ANOTHER way of graphing things. However, a brief guide is given here.

Hence, for graphs I usually just generate them in Gnuplot and include them as pictures. Gnuplot has the typical steep learning curve, but I love it (like xFig, it still is the only thing that I can use to ACTUALLY GET THE JOB DONE). Gnuplot can save files in a million different formats: I use "fig" format if I want to mess around further in xFig.

There ARE a couple of trip-ups with (PDF)LaTex fonts in Gnuplot. First, if you use double quotes for strings then each backslash "\" is actually interpreted by Gnuplot (and will NEVER make it through to (PDF)LaTex). Hence if you want a single "\" then you must use actually a double "\\". You can also just enclose your expressions in single quotes - this sends the string through without Gnuplot even looking at it.

Second, saving a file in ".fig" format (if you want to play in xFig) does NOT mark the text as "special" in xFig. The ONLY method that I have found (that actually works) is to put the "textspecial" option in your Gnuplot file like so:

set terminal fig textspecial
Now you MIGHT think that you could just forget this and set the flags to "special" from within xFig... but I'm telling you that (as of the time of this writing) this DOES NOT WORK (and that little bit cost me an hour to figure out... AHHHH).