# LaTeX and Xfig

  How to Import Xfig Figures in Your LaTeX Files
  Changing the Size of Pictures
  Xfig and Metapost by Josselin Mouette (jmouette@ens-lyon.fr)

## How to Import Xfig Figures in Your LaTeX Files

Xfig can generate output to many different formats which TeX or LaTeX can read. The final document is produced by one of the following methods, depending on the export language.

LaTeX picture, LaTeX with pict2e macros, TeX/LaTeX with tikz macros, LaTeX with pstricks macros, LaTeX with epic/eepic/eepicemu macros:
  The file generated from xfig is directly included, e.g., `\input{fig1.tikz}`, or the entire file contents is copied into your TeX/LaTeX file. Therefore, the TeX/LaTeX document can contain all text and graphics. Usually, it will be necessary to load a macro package to enable TeX or LaTeX to process the file. Text that should be interpreted by TeX/LaTeX must have the TeX Flag set to ON, see TEXT FLAGS. Otherwise, characters special to TeX will be quoted and, e.g., `$x$` will print as $x$, not x.
  `latex`
    Include the generated file with, e.g., `\input{fig1.latex}` into your document, or simply embed it. No packages are required, but the capabilities of the `latex` output language are quite limited.
  `tikz`
    `\input{fig1.tikz}` or embed the contents of the generated tikz file into the TeX-document. In the preamble of a LaTeX document, use
    `\usepackage{tikz, graphics} % graphics, if the figure contains embedded images
    \usetikzlibrary{arrows.meta, % if the figure contains arrow-tips
    bending, % arrow tips on arcs are "bent," i.e., deformed a bit
    patterns % if the figure contains pattern fills
    }`
    The export to TeX/LaTeX with tikz macros supports all capabilites of xfig. LaTeX and Postscript fonts can be freely used and even mixed in the same figure, and the typesetting capabilities of TeX can be used.
  `pict2e`
    Embed or `\input` the generated file into your LaTeX document. In the preamble include
    `\usepackage{pict2e,
    color, % if the figure contains color
    graphics % if the figure contains embedded images
    }`
    Export to LaTeX with pict2e macros does not support pattern fills. All other features of a xfig drawing are reproduced. LaTeX and Postscript fonts can be used for text in the xfig drawing.
  `pstricks`
    `\usepackage{pstricks,
    pstricks-add, % for complex line styles and hollow PSTricks arrows (-R 1 option)
    graphicx % if the figure contains embedded images
    }`
    The pstricks output matches the quality of the Postscript driver. Text is rendered black, but LaTeX font color-changing code can be embedded in the text.
  `epic`
    `\usepackage{epic}`
  `eepic`
    `\usepackage{epic,eepic} % order matters!`
EPS, PDF, EPS and PDF:
  Export the figure from xfig to eps or pdf (or both) and import the file with `\includegraphics` into your document. Write
  `\usepackage{graphics}`
  into the preamble and include the drawing with, e.g., `\includegraphics{fig1.eps}` or `\includegraphics{fig1}`. This method supports all capabilities of xfig, but only Postscript fonts are used to render text. To make use of the fonts and of the typesetting possibilities of LaTeX, one could use the `psfrag` package to replace text strings in an eps file with code rendered by LaTeX.
Combined PS/LaTeX, Combined PDF/LaTeX, Combinded PS/PDF/LaTeX:
  Export the figure to two files or even three files, partially to eps or pdf or to both, and partially to a LaTeX file, and include the LaTeX file in your document. The LaTeX file contains the text and includes either the eps or pdf file which contains the graphics. Text marked with the TeX Flag set to ON is rendered by LaTeX and superimposed over the included graphics. In the preamble, put
  `\usepackage{graphics,color} % color, if the text is colored`
  Write `\input{fig1.pstex_t}` for latex + eps, `\input{fig1.pdf_t}` for latex + pdf, or `\input{fig1.pspdftex}` for latex + eps or pdf at the location where the drawing should appear. The export to Combined PS/PDF/LaTeX, latex + eps or pdf, can be processed by any LaTeX engine.

A variant of the second method above is to

create a stand-alone tex file and import the generated EPS or PDF:
  The tex-file cannot be exported directly from xfig, but you must invoke `fig2dev` from the command line with the `-P` option of the tikz or pict2e driver. Save your drawing, and invoke `fig2dev` on the saved fig-file, e.g., for the `tikz` driver,
  `$ fig2dev -L tikz fig1.fig fig1.tex
  $ pdflatex fig1.tex`
  Include the final `fig1.pdf` with `\includegraphics{fig1}` into your LaTeX document. This tex-file is processed by LaTeX to create a pdf or eps graphics that can be included into your LaTeX document. The advantage of this method above the direct export to pdf or eps is that text can be rendered with LaTeX- and Postscript fonts, and TeX/LaTeX expressions are interpreted in text on which the TeX flag is set to On.

To set the TEXT FLAG TeX Flag to ON and use LaTeX-fonts for all text, invoke xfig with the commandline
`$ xfig -specialtext -latexfonts -startlatexFont default`
To make the above permanently and start all drawings with LaTeX fonts and the TeX flag set to ON, put the following in your X-resources file. This is probably the file `.Xresources` in your home directory.`
Fig.latexfonts: true
Fig.specialtext: true`

### 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 (jmouette@ens-lyon.fr)

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 ]