# LaTeX and Xfig

How to Import Xfig Figures in Your LaTeX Files
Changing the Size of Pictures
Xfig and Metapost by Josselin Mouette ([email protected])

## How to Import Xfig Figures in Your LaTeX Files

Xfig can generate output to may different formats which TeX or LaTeX can read. Most importantly, in order that TeX/LaTeX interprets text in the figure, the text flag TeX Flag must be set on the text. The final document is produced by one of the following methods.
1. The file generated from xfig is directly included or embedded into your TeX/LaTeX file. Usually, it will be necessary to load a macro package to enable TeX or LaTeX to process the file. The latex picture, tikz, pict2e, pstricks, epic and eepic output languages fall into this category.

For example, export your figure to tikz and include the generated file into your document with \input{fig1.tikz}. In the document preamble, load \usepackage{tikz} or, with TeX, use \input tikz.tex. If the figure contains color, \usepackage{color} is required. If images were imported into the xfig-drawing, \usepackage{graphics} is necessary. It depends on the LaTeX engine, e.g., xelatex, lualatex, latex + dvips, latex + dvipdfmx, pdflatex or others, whether the imported image can be processed with the \includegraphics command.

2. Export the figure from xfig to eps or pdf (or both) and import the file with \includegraphics into your document. For instance, write \usepackage{graphics} into the preamble and use \includegraphics{fig1.eps} or \includegraphics{fig1}.

3. Export the figure to two files, partially to eps or pdf or both, partially to a latex file, and include the latex file. The latex file contains the text and includes the eps or pdf file which contains the graphics. For example, try \input{fig1.pstex_t} for latex + eps, \input{fig1.pdf_t} for latex + pdf, or \input{fig1.pspdftex} for a latex file that includes an eps or a pdf file. The latter can be processed by any LaTeX engine.

For methods A and C, the Text flag TeX Text must be set for text that should be typeset using TeX. For instance, to render the text $\beta$ in the xfig-drawing as β. Otherwise, characters special to TeX are quoted and $\beta$ prints as $\beta$. The choice of the font, namely, latex of postscript fonts, is irrelevant to the interpretation of text strings.

The most powerful method, in terms of capabilities and output quality, is to export the figure to tikz. The file can be processed using TeX or LaTeX, TeX and Postscript fonts can be freely used and even mixed in the same figure, and the typesetting capabilities of TeX can be used. If xelatex is used, eps and pdf graphics can be imported into the same drawing. The tikz-file can be embedded into the TeX-document, hence a single document can contain all text and graphics.

The postscript and pdf output languages support all capabilities of xfig, except that only postscript fonts can be used and text is not interpreted by TeX. With method C, all text which has the Text flag "TeX Text" set is exported to the LaTeX file and a TeX-font is used. Text without the "TeX Text" flag is printed using a Postscript-font. A variant of method B is to use the psfrag package and replace strings in the eps file from within LaTeX.

To set the Text flag TeX Text and use LaTeX-fonts for all text, use the commandline

xfig -specialtext -latexfonts -startlatexFont default
To start all drawings with LaTeX fonts and with TeX Text set, put the following in your X-resources file, probably .Xresources in your home directory,
Fig.latexfonts: true
Fig.specialtext: true


With method A, other notable export formats are the pict2e and the pstricks output. The pict2e output language does not allow pattern fills and can not generate a background grid, but the macro package is much smaller and the document compiles faster than with the tikz macro package. The pstricks output language also generates graphics output which is on a par with the tikz or the postscript output.

### Changing the Size of Pictures

If possible, try to not scale drawings at all. Different line thicknesses should be a avoided, similarly to avoiding differently sized letters in text. The rulers at the top and at the side, and the magnification setting in the lower left corner of the xfig window may aid at producing the graphics at the right size.

If scaling cannot be avoided, the commands \scalebox and \resizebox from the graphics package can be used,

\scalebox{1.5}{\input{fig1.pstex_t}}	% \scalebox{factor}{object}
\resizebox{10cm}{!}{\input{fig1.latex}}	% \resizebox{width}{height}{object}

An exclamation mark in place of one of the dimensions retains the aspect ratio of the object.

To set the width of tikz-drawings produced by xfig, you can use

\newlength\XFigwidth\XFigwidth84mm
\newlength\XFigheight\XFigheight56mm

before input'ing your figure. This will scale the coordinates of the drawing, but not line widths or text. If only one of the two dimensions is given, the figure retains its aspect ratio. To render subsequent figures in their original size, both dimensions must be undefined or set to zero,
\XFigwidth0pt \XFigheight0pt

## Xfig and Metapost

written by Josselin Mouette ([email protected])
1. METAPOST
There is nothing special to do in xfig to use MetaPost. All the text you type will be treated as plain TeX code - note, this will be not compiled within your document, so you don't have acess to packages like AMS-TeX, neither have you to your macros. In xfig, export your file with the MetaPost filter, it creates foo.mp. Then, type mpost foo.mp, it will generate foo.0 (or foo.1, sometimes). In your document, put this in the preamble:
    \input supp-pdf.tex
\usepackage[pdftex]{graphicx}

And to include your figure :
    \convertMPtoPDF{foo.0}{1}{1}

That's it. Quite simple, and you can put a bit TeX inside.
Pros: Can be easily included in a dual-output (pdf/dvi) file: for including it as PS, just put a \includegraphics{foo.0} in the document.
Cons: Not adapted to big formulas, as AMS-LaTeX is not accessible. Long phrases may look bad as well, if your document is not in English (babel cannot be used).

2. MULTI-METAPOST
This method is designed to be used in PDF presentations. Using the \pause command, it will display step by step the layers of your figure as you click on the button, which can look very nice (and can even be useful sometimes). All that have been told about MetaPost inclusions is true, but there are a few extra things to know:

1. When creating your figure, be careful with the depth of your objects. When exporting your figure in the MultiMetaPost format, transfig will treat the consecutive depth levels where is an object as a single layer, for example:
   Circle at depth 51 \__first displayed layer
Text at depth 50   /
*** Nothing at depth 49
Square at depth 48 \
Text at depth 48    > Second displayed layer
Curve at depth 47  /
... and so on.


2. After exporting, mpost foo.mmp will create a set of files named foo.0, foo.1... To include them in the document, you will need the mpmulti.sty provided with the latest version of PPower4 (still in Beta stage at the time of writing). The preamble of your document should look like this:
    \input supp-pdf.tex
\usepackage[pdftex]{graphicx}
\usepackage{pause,mpmulti}

And to include your animation, just put:
    \multiinclude{foo}

You can adjust it to a defined size by using:
    \multiinclude[graphics={width=5cm}]{foo}

Compile your document, then ppower4 it. Nifty, isn't it?
Pros: The only way to insert automatically animations. Benefit of the existing xfig's depth system.
Cons: Are there any?

[ Contents | Introduction | Credits ]