figtex2eps
Converting xfig-figures with LATEX to eps
Håvard Berland∗
June 2003
Abstract
This document describes how to use a bash script for automating
the compilation of xfig-figures with embedded latex commands into
encapsulated postscript (eps) or into pdf.
Contents
1 Introduction 1
2 Usage 2
2.1 Setting the special flag . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Draw your figure and annotate it with text . . . . . . . . . . 3
2.3 Converting to eps . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.4 Converting to pdf . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Modifying the LATEX-preamble 4
4 How it works 5
5 Source code 5
1 Introduction
Figures in publications, theses, reports and alike with the same typograph-
ical quality as the rest of the document, often typeset by LATEX, is not seen
too often.
A natural prerequisite is to be able to use the same font for any text
in the figures as you use for the rest of your document. Also, for many
purposes, special symbols are needed which correspond to symbols in your
∗
[Link]
1
report. These should look exactly the same in both your figures and body
text.
The fig-file format used by xfig, supports inserting LATEX-commands by
some neat tricks, leaving all that has to be done with LATEX to LATEX itself.
This script, named figtex2eps does nothing else than providing an easy
interface to underlying commands that do the real work. The real work
involves running several commands and making a tex-file for each figure
you need, so there is definitely the need for some utility to automate the
process.
2 Usage
The usage of the script itself is fairly simple, produce your figure using xfig,
and save it in the vector format “fig”, say in the file “[Link]”, and then
run
$ figtex2eps [Link]
and after some seconds, you will have a file called “[Link]” in the same
directory.
If you didn’t do any special things to embed latex-commands in your
fig-file, this command will be pretty equivalent to an ’export to eps’ in xfig.
But the important thing is to include LATEX-commands and -symbols.
2.1 Setting the special flag
The first thing you should do after launching xfig, is to
ensure that all text you write, has the special-flag set.
1. Open xfig.
2. Push the big ’T’ button in the toolbar at the left,
and go into text mode.
3. Push the ’Text flags’ button at lower (text-input-)
toolbar, and set t special flag from Normal to Spe-
cial, as done in the figure at right.
Now, all the text you input from now on will have this
flag set, and this flag means that all treatment of text in your figure will
be delegated to some other utility when exporting from the fig format. We
want LATEX to take care of everything regarding text.
2
2.2 Draw your figure and annotate it with text
This document will not try to learn you xfig, only how to insert LATEX-
commands. We provide a simple example of a figure with a modest use
of LATEX-features. There are some things to remember:
Figure 1: An example figure as shown in xfig.
• Write LATEX as usual, that means enclosing LATEX-constructs which re-
quire math mode with dollar signs ($), as in our example Figure 1.
• Be prepared to adjust the position of your text by trial and error.
Often, the text and commands will overlap with your figure when
viewed in xfig, but they won’t in your final postscript.
• Be prepared for trouble if you include very complicated LATEX-constructs
in your figures. Debug the compilation with the verbose option (figtex2eps
-v [Link]). You might need to alter the preamble file, see below.
• Try to avoid resizing your figure to much when your final import into
your document is done, you might get to small or to big fonts. Have
in mind the final size of your figure in the document when you start
to draw in xfig.
2.3 Converting to eps
When the above is done, this is the easy step. Save your figure as for exam-
ple “[Link]” (do not use any of the export routines!) and run from
your command line:
$ figtex2eps [Link]
3
and the file “[Link]” will be produced. During compilation, LATEX
produces a lot of output. Use the option “-v” (verbose) if you want to
see this (could be necessary for some debugging if you write faulty LATEX-
commands). The outputted file from the figure in Figure 1 is shown in
Figure 2.
Figure 2: Postscript produced by figtex2eps.
2.4 Converting to pdf
This is equally easy, by using figtex2eps’ counterpart, figtex2pdf. This is
merely a wrapper for your convenience, it calls figtex2eps on your behalf,
and converts the eps-file to pdf using epstopdf (which must be available on
your system). It also leaves the eps-file behind.
3 Modifying the LATEX-preamble
When running figtex2eps for the first time in a directory, some output is
actually produced:
$ figtex2eps [Link]
(Info) Generating preamble file [Link]
(Info) You may edit to suit your needs if necessary and rerun
This tells you that a file has been generated in this directory. If you want
to or have to, you may modify this file and rerun figtex2eps. If figtex2eps
finds this file, it will use it, and not its own default. There could be several
reasons for modifications to this:
• You have used special LATEX-symbols in your figures, which require
some more packages to be included. Then include more usepackage-
lines in the preamble-file.
• You would like to adjust the default font size in the figures. This has
been set to 12, despite that one usually uses 11 or 10 in reports. This
is because one usually downscales figures when included in reports,
but this may not be the case for you.
4
• You would like to change the font being used from Roman to some-
thing else (Palatino or Times for example).
4 How it works
The process of converting the fig-figure to eps narrows down to some sim-
ple steps:
1. Make a file with suffix pstex. This is a (postscript) file containing
all parts of the figure that are not LATEX-commands. This file can
be viewed by gv if one wants to. This step is accomplished by a
fig2dev -L pstex command.
2. Make a file with suffix pstex t. This is a partial latex-document
that includes latex-commands for putting the postscript picture in
the pstex file in the background, and putting the wanted the latex-
commands and other text on top of the eps-picture at the correct po-
sition. This step is accomplished by a fig2dev -L pstex t com-
mand and some manual insertions of latex-code.
3. Make a tex-file which defines the font, packages to use and other nec-
essary things, and make it include the pstex t-file.
4. Run latex on our tex-file, which produces a dvi-file.
5. Make encapsulated postscript from our dvi-file with dvips -E
6. If figtex2pdf was called, convert the eps-file to pdf by the use of
epstopdf.
Running figtex2eps with the verbose option (“-v”) also leaves behind
all the intermediate files that were created in the process.
5 Source code
The source code is available for download at the web-address
[Link]
and is also included here for reference.
# !/ bin /bash
#
# Converts a . fig − file to a . eps − file , by using graphics from . fig as
# a backgroundpicture and latex −compiles tex −code from . fig on top
#
# Can also make pdf , by first making ps, and then running epstopdf
#
# Håvard Berland http :// [Link]/˜berland
#
# $Id : figtex2eps , v 1.7 2003/10/28 18:55:31 berland Exp $
5
# $Source: /home/pvv/d/berland / etc / cvsrepo / figtex2eps /prog/ figtex2eps , v $
#
function usage
{
echo ””
echo ”Usage:”
echo ” figtex2aeps <options> <[Link]>”
echo ””
echo ” Options: −v Verbose, and don’t delete temp files”
echo ” −pdf Generate pdf in addition via epstopdf”
echo ””
echo ”Script written by Håvard Berland [Link]
echo ”Documentation available at [Link]
exit
}
function die
{
echo ”$1”
echo ”Exiting ... ”
exit
}
# A prefix used for outputting messages to the user
errorprefix=” (Error) ”
infoprefix=” (Info) ”
# The file where the preamble is to be written and/or found
preamblefile=figtex2eps−[Link]
# Option for running latex .
latexoption=”−interaction=batchmode”
# Option for dvips . These are set for tex − installations based
# on teTeX, and ensures that Type1 fonts are used ( via the file
# config . pdf loaded by −Ppdf), this gives correct fonts in the
# pdf −document. Set this variable to ”” if this does not work,
# and try to find other ways of getting Type1 fonts .
dvipsoptions=”−Ppdf −G0”
# Each of these commands must be available and in $PATH !
neededcommands=”latex dvips fig2dev”
for cmd in $needecommands ; do
which $cmd >/dev/null 2>&1 \
|| die ”${errorprefix}Could not find $cmd in your path!”
done
if [ −z ”$1” ]; then
echo ”${errorprefix}Mandatory input arguments not provided.”
usage
fi
# Default , throw away unwanted output from commands.
out=”>/dev/null”
# Default option to run dvips in quiet mode:
dvipsout=”−q”
6
# If verbose , alter the above set variables .
if [ ”$1” = ”−v” ] ; then
out=””
dvipsout=””
latexoptions=””
shift
fi
# Check if user wants pdf (we might be called via wrapper script )
dopdf=””
if [ ”$1” = ”−pdf” ] ; then
dopdf=”yes”
shift
fi
# In case of changed order of options ( this way of dealing
# with options is certainly not scalable !!!)
if [ ”$1” = ”−v” ] ; then ## Do SAME as above..
out=””
dvipsout=””
latexoptions=””
shift
fi
# Check our mandatory input argument
figfile =$1 # may or may not include . fig as an ending
if [ ! −f $figfile ] ; then
# Test if user just dropped the ending:
if [ −f ” $figfile . fig” ] ; then
figfile =” $figfile . fig”
else
die ”${errorprefix}Could not find the xfig−file $figfile”
fi
fi
# Check that the figfile is not empty
if [ ! −s $figfile ] ; then
die ”${errorprefix}The file $figfile is empty.”
fi
# If the ’ file ’ and ’ grep ’ command is available , we just as
# well check that it really is a FIG, not just
# some other file with the suffix . fig
format=‘which >/dev/null 2>&1 file && which >/dev/null 2>&1 grep && file $figfile‘
if [ ! −z ”$format” ] ; then
if [ −z ‘echo ”$format” | grep ”FIG image text” >/dev/null && echo yes‘ ] ; then
echo ”${errorprefix}According to the utility ’ file ’, $figfile is not a proper fig−file.”
die ”${errorprefix}’ file ’ says: \”$format\””
fi
fi
base=”${figfile%.fig}”
outfile =$[Link]
# The user is also allowed to provide an output file if really necessary :
if [ ! −z ”$2” ]; then
outfile =$2
fi
7
## Make a preamble file if it does not exist
if [ ! −s ”$preamblefile” ] ; then
echo ”${infoprefix}Generating preamble file $preamblefile”
echo ”${infoprefix}You may edit to suit your needs if necessary and rerun”
# CVS tip if the directory CVS exists .
if [ −d ”CVS” ] ; then
echo ”${infoprefix}You might want to do a ’cvs add $preamblefile’ as well”
fi
touch $preamblefile \
|| die ”${errorprefix}Could not write to $preamblefile, check your permissions”
## This could have been redone with a HERE document..
echo ”%” > $preamblefile
echo ”% This is a preamble file for ’figtex2eps’. You may edit things” \
>> $preamblefile
echo ”% here if necessary. Typically you might want to change the font” \
>> $preamblefile
echo ”% size, the font or add some more packages for your latex commands” \
>> $preamblefile
echo ”% in your figures. ” >> $preamblefile
echo ”%” >> $preamblefile
echo ”% If you make errors in here, rerun figtex2eps with ’−v’ (verbose) and” \
>> $preamblefile
echo ”% check the error messages from latex, and then fix here.” >> $preamblefile
echo ”% You may also just delete this file if you are in trouble” >> $preamblefile
echo ”% and a new default one will be generated” >> $preamblefile
echo ”%” >> $preamblefile
echo ”\documentclass[12pt]{article}” >> $preamblefile
echo ”” >> $preamblefile
echo ”% Packages for most mathematical latex commands:” >> $preamblefile
echo ”\usepackage{amsfonts}” >> $preamblefile
echo ”\usepackage{amsmath}” >> $preamblefile
echo ”\usepackage{amssymb}” >> $preamblefile
echo ”” >> $preamblefile
echo ”\usepackage{ae} % This is in case you also want to make pdf afterwards” \
>> $preamblefile
echo ”” >> $preamblefile
echo ”% You might want the palatino font instead, then uncomment the following” \
>> $preamblefile
echo ”% two lines, and do not use the ae package above” >> $preamblefile
echo ”%\usepackage{palatino}” >> $preamblefile
echo ”%\usepackage{palatcm} % Palatino math fonts ” >> $preamblefile
echo ”” >> $preamblefile
echo ”\usepackage[dvips]{color}” >> $preamblefile
echo ”\usepackage{epsfig}” >> $preamblefile
fi
if [ ! −z ”$out” ] ; then
# Not verbose :
fig2dev >/dev/null −L pstex $figfile $[Link]
fig2dev >/dev/null −L pstex t $figfile $[Link] t 2
else
# verbose
fig2dev −L pstex $figfile $[Link]
fig2dev −L pstex t $figfile $[Link] t 2
fi
echo ”\begin{picture}(0,0)%” > $[Link] t
echo ”\epsfig{file=$[Link]}%” >> $[Link] t
echo ”\end{picture}%” >> $[Link] t
8
cat $[Link] t 2 >> $[Link] t
cat $preamblefile > $[Link]
echo ”\setlength{\textwidth}{100cm}” >> $[Link]
echo ”\setlength{\textheight}{100cm}” >> $[Link]
echo ”\begin{document}” >> $[Link]
echo ”\pagestyle{empty}” >> $[Link]
echo ”\input{$[Link] t}” >> $[Link]
echo ”\end{document}” >> $[Link]
if [ ! −z ”$out” ] ; then
# Not verbose :
latex $latexoptions $[Link] >/dev/null \
|| die ”${errorprefix}Latex failed, rerun with ’−v’ (verbose)”
else
# Verbose :
latex $[Link] || die ”${errorprefix}Latex failed”
fi
dvips −E $dvipsoptions $[Link] $dvipsout −o $outfile \
|| die ”${errorprefix}dvips failed, rerun with ’−v’ (verbose)”
# Make pdf if user wants to .
if [ ! −z ”$dopdf” ]; then
cmd=”epstopdf”
which $cmd >/dev/null 2>&1 \
|| die ”${errorprefix}Could not find $cmd in your path, only eps generated!”
$cmd $[Link] −−outfile=$[Link]
fi
# delete if not verbose
if [ ! −z ”$out” ]; then
rm −f $[Link] $[Link] t $[Link] t 2 \
$[Link] $[Link] $[Link] $[Link]
fi