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

Summary before we begin: if you want to include raster (bitmap) graphics then use the Gimp. If you want to include vector graphics then use xFig (Tgif or Inkscape also work with LaTex, but not as well). If you want to include diagrams (for example commutative diagrams) then use the "xypic" package from within LaTex. If you want graphs (of functions) then use Gnuplot.

Note that PDFLaTex and LaTex handle graphics rather differently. Here are the basic differences:

PDFLaTex: the following types of files can be used for graphics:

but NO POSTSCRIPT!!! UPDATE: I guess GIF doesn't work...

LaTex: the following types of files can be used for graphics:

EPS, MetaPost

The results below apply to pdflatex version 3.14159-1.10b-2.1 (Web2C 7.4.5) and xFig version 3.2.5-alpha5 (current as of November 2005).

As far as raster graphics are concerned (pixel-by-pixel graphics) there is one clear choice: the Gimp. The Gimp is a drop-in replacement for programs such as Adobe Photoshop (some claim that it's superior - I'm not qualified to make any such claims). The Gimp allows us to create complicated artwork, modify photos, etc. HOWEVER, THIS PAGE IS NOT ABOUT INCLUDING RASTER GRAPHICS, so I have little more to say about it. If you are using PDFLaTex then you should directly export in the Gimp to PNG, JPEG, PDF, or GIF since these files can be used in your "\includegraphics" command. If you are using (plain) LaTex then you will should export from to Gimp to EPS format (warning: the file will probably be HUGE!).

Putting LaTex/PDFLaTex fonts and commands in your Gimp picture is a bit more difficult, but not impossible. I recommend exporting the image to JPEG or PNG and then open up the image in xFig for further editing (see below). To do this click the "camera" icon. Then drag out a rectangle and Right-click. This will bring up a dialog that will allow you to import the file. Note that the aspect ratio AND size can be altered with the "Scale" button.

If you are using the Gimp to edit then 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. I recommend the "Absolute" setting. Please note that the tablet is NOT very helpful for vector graphics in my experience (see below). For vector graphics buy yourself a decent trackball UPDATE: I now have stumbled upon a configuration where the Wacom is VERY handy in xFig... see below.

If you are trying to include black and white figures then maybe you should just consider DRAWING THEM ON PAPER and scanning them in (make sure to scan in black and white, otherwise it will look bad). Tip: draw very large images on paper, then shrink them later. It hides a lot of flaws. Open the scanned image in Gimp. This allows you to further edit/clean the image. Don't forget to crop it! Then save the image and import the image into xFig using the "camera" icon (described above). Now you can add LaTex fonts and commands. Then Export to "Combined Ps/LaTex" as described below in Method 3. It's old fashioned, but it works better than you think, it's fast, and you get exactly the diagram that you want.

xFig and PDFLaTex
xFig has a steep learning curve because the mouse movements are non-intuitive (remember the middle button!). However, after using it for a while you will realize how powerful and simple it is. If you REALLY hate xFig (give it a week) then you can try Tgif or Inkscape - however the integration with LaTex/PDFLaTex is not as good.

I have tried several types of hardware (tablet, mouse, trackball) and I find that the trackball is the most useful BY FAR. This is because you can move to where you want and THEN click without accidentally moving the cursor - great for precision work.

SKIP THIS NEXT COUPLE OF PARAGRAPHS IF YOU DO NOT HAVE LINUX AND/OR IF YOU HAVE NO INTEREST IN A WACOM TABLET. A more elaborate setup that uses a Wacom tablet is the following: Buy a trackball AND a Wacom tablet (note: larger is NOT better in my experience). I use the trackball in one hand, and the Wacom stylus (pen) in the other. Set up the Wacom tablet as in my Wacom page (hint: in order for any of the following to work the tablet must be plugged in BEFORE starting XWindows. Hotplugging STILL doesn't work very well as of September 2009). Now I actually DISABLE the "Button1 press when stylus touches pad" feature of the tablet by increasing the "Threshold". This can be permanently configured in /etc/X11/xorg.conf by using the following stanza:
Section "InputDevice"
        Identifier      "stylus"
        Driver          "wacom"
        Option          "Device"                "/dev/input/wacom"
        Option          "Type"                  "stylus"
        Option          "Mode"                  "Relative"
        Option          "Threshold"             "1000"
        Option          "Speed"                 "0.5"
        Option          "USB"                   "on"
We can also do it (on the fly) with the "xsetwacom" utility. First open a shell and figure out which devices are active on the Wacom:
xsetwacom list
In my case I found a "stylus" device (which I configured earlier as above in /etc/X11/xorg.conf). If nothing shows up then make sure that you set up the tablet correctly AND that it is plugged in BEFORE starting XWindows. I promise!!! Now disable the "Button1 press" by setting the Threshold very high
xsetwacom set stylus Threshold 1000
xsetwacom set stylus Mode Relative
xsetwacom set stylus SpeedLevel 4
Notice that SpeedLevel and Speed use different numbers... I hope they make these options more consistent with each other in the future. The disabled features (pressure sensitivity, etc) are AWESOME in the Gimp, but terrible in xFig. The point is that the pen just acts now like a very slow and sensitive pointer. Furthermore, it doesn't click when you touch the surface. It also is in relative mode (great for xFig) and Now you can move around in a very sensitive fashion. When you need to click use the trackball buttons with the other hand. Also, for large screen movements use the trackball. The pen gives a lot of fine-tuned movement capability. This is my current configuration and I love it. END OF LINUX SECTION AND BACK TO THE MAIN STORY.

I recommend "Method 3" below, although to understand it you need to read straight through (I'm too lazy to rewrite). The "Combined PS/LaTex" export along with the utility "epstopdf" gives us a setup that works for both LaTex and PDFLaTex simultaneously.

Method 1: Export to Metapost
I like this method because it is robust. It works in both PDFLaTex and (plain) LaTex the same way. Unfortunately, it only supports basic LaTex commands (no other packages such as AMS). Without AMS support I cannot use it.

While you are generating your figure in xFig, you may want to put some text in there. Although xFig has limited text capabilities, it is possible to put LaTex commands straight in the figure. The way to do this is to 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 (by PDFLaTex, in this case). So now you can put in a LaTex command like

and it will *eventually* be processed by LaTex. Note that you can set all text to "special" as the default by starting xFig from the command line with these options:
xfig -specialtext -latexfonts -startlatexFont default

When you are done in xFig, it is time to export the figure to so-called Metapost format.

If you don't mind spending some time, then you might want to learn the MetaPost language written by John Hobby. This is a low-level language for drawing professional-grade vector figures. The command-line utility "mpost" seems to already come standard with my "tetex" installation, so we're halfway there :).

However, for most tasks it's much better to just draw the figure in xFig. 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" - xFig won't do it for you. Both xFig and Metapost will respect your "special" LaTex strings and happily pass them to LaTex.

Once the figure has been exported to "foo.mp" then it's time for the Metapost compiler to turn it into a figure that LaTex can read (you can first edit "foo.mp" if you want to manually tweak anything in the picture). 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 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". LaTex doesn't know what foo.0 is, but it DOES know what .mps files are! Then include the graphics using the usual
	\caption{A really neato picture}
commands (make sure that you have \usepackage{graphicx} in the Header). That's IT! VERY IMPORTANT TIP: LaTex is idiotic about labelling. Make sure that your "\label" command is the LAST command in your figure - otherwise it won't work as you expect. I don't know why this is, but it is certainly a bug that won't go away.

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 too strange for ghostview there is a small utility called "mps2eps" that converts the mps file to a self-contained eps file 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 2: Export to Combined PDF/LaTex
Here is a better method (but still not the best). Draw your image in xFig with PDFLaTex commands using the "special" text option (as described above). When completed choose File->Export. Now you must select "Combined PDF/LaTex" as the Export type (NOT just straight PDF, although obviously this is fine if you have no PDFLaTex commands in your figure).

Then type "foo.pdf" (WITH THE EXTENSION .pdf) in the "Output File" field. This creates 2 files:

foo.pdf and foo.pdf_t
Obviously one of these contains the graphical part of the figure, and the other contains actual PDFLaTex commands (you will only have to directly deal with foo.pdf_t).

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

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

Now, within your document, you can include this picture with the command

	\caption{A really neato picture}
The does IT!!! Now you can compile your PDFLaTex document by running "pdflatex".

Method 3: Export to Combined PS/LaTex
Here is the best method since it works in both PDFLaTex and LaTex simultaneously. This method is exactly the same as the previous section, although there we exported to "Combined PDF/LaTex". Now we export to "Combined PS/LaTex". For the "Output File" type "foo.eps" (WITH EXTENSION .eps)! Note that xFig is a bit silly about the filenames. Make sure that you export to an ".eps" extension rather than the ".pstex" that xFig defaults to. You need to actually hover the mouse over the box in xFig to change the name from the default. Just clicking on it won't let you type!

This creates two files

foo.eps and foo.eps_t
We're NOT DONE YET. Obviously this creates 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 an extra package 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
The advantage is that now there are TWO files (PDF and EPS). PDFLaTex will automatically choose the one with the PDF extension, but (plain) LaTex will choose the EPS extension (so this works either way).

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

Both are absolutely essential.

Now, within your document, you can include this picture with the command (this same command works in PDFLaTex and in plain LaTex - you MUST NOT alter "foo.eps_t" in the following lines to "foo.pdf_t"):

	\caption{A really neato picture}

xFig and (plain) LaTex
It is useful to read the above section on PDFLaTex to set the stage.

Method 1: Export to MetaPost
This method is identical to that already described for PDFLaTex. Remember that if you need other packages like AMS then this WON'T WORK FOR YOU!!!

Method 2: Export to Combined PS/LaTex
This method is identical to the same section described above in the PDFLaTex section.

Method 3: PSFrag method
I will outline this section for users of Inkscape rather than xFig, but the following applies to xFig as well. You must be using plain LaTex (not PDFLaTex) to use the "PSFrag" package in LaTex. NOTE: there is an option in Inkscape to export to "LaTex PSTricks" format, but I cannot see how that helps us to put LaTex fonts in our pictures!

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 string AS A STRING 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

Then you can include the picture with a stanza like so
	\caption{A really neato picture}
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".

Diagrams and Graphs in (PDF)LaTex using XYPIC and GNUPLOT
For commutative diagrams I have nothing to add (at this time) to the official XY-Pic page (scroll down for some good tutorials). There is a program called "dia", but I haven't found a need to use it yet, so I can't really comment.

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 (besides, this package isn't included in my tetex distro, so I figure that there MUST be a better way to do such a COMMON task). 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, or I use "mp" (MetaPost) format if I want to send it directly to a MetaPost file. BOTH formats support PDFLaTex fonts, which is why I use them. I don't generally worry about the other formats since I haven't found it necessary so far (however, if you are using plain LaTex method 3 (PSFrag method above) then maybe you'll want to export to EPS).

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).

This page has been visited  10,181 (2 today) times since November 17, 2005