Tuesday, May 4, 2010

Getting xfig, LaTeX, and LyX to work properly with each other

To get these three guys to cooperate is a hassle if you do not know the secrets.

In xfig, there are few things to remember:

  • Use the 'special' flag when creating text objects.
  • Use LaTeX fonts; 'default' works for me.
  • Use LaTeX code as you normally would. E.g. to enter the antiderivative of the sine function, use $\int \sin (x)$. LyX sometimes balks if slashes (\) are not preceded by empty space, which I do not understand. To place your LaTeX text objects in their correct location with respect to the graphical objects is not straightforward and may take several iterates (see below; I may post something about this intuitive though tedious process).
  • Export to 'Combined PDF/LaTeX (both parts)', though this post should apply equally to the eps format if you make the appropriate changes. If your xfig file is named foo.fig, this will output foo.pdf and foo.pdf_t by default. foo.pdf contains graphical objects without any text that had the 'special' flag on, while foo.pdf_t contains a call to \includegraphics{foo.pdf} and all the LaTeX commands necessary for superimposing the LaTeX onto the graphical objects.

To take care of the first two points, I launch xfig with

xfig -specialtext -latexfonts -startlatexFont default

In LaTeX and LyX you needs to add into your preamble the following:

\usepackage[pdftex]{graphics,color}

\DeclareGraphicsRule{.pdf_t}{pdf}{*}{}

\graphicspath{{path_to_your_graphics/}}

I do not understand why the 'color' package is necessary, but it is. The second line is to tell LaTeX how to handle the graphics extension .pdf_t. The last line is to tell the graphics package where to find graphics files. The trailing / is necessary as per the documentation of the graphics bundle. The alternative to adding the last line is to edit the

\includegraphics{foo.pdf}

line in foo.pdf_t to

\includegraphics{path_to_your_graphics/foo.pdf}

The problem with this is that you need to edit this file after every time you export foo.pdf and foo.pdf_t from xfig and you may need to adjust the LaTeX text objects and export your graphics several times.

At the point in the document where you want the graphic to appear, you can use

\input{path_to_your_graphics/foo.pdf_t}

If you are using LyX, enter this code using LaTeX input (ctrl-l on unix, or Insert -> TeX Code). If LaTeX can find your graphics (i.e. your graphics are in the TEXINPUTS path or kpathsea can find them) then you should be able to get away with

\input{foo.pdf_t}

for the last line, but I have not tried to get this to work.

No comments:

Post a Comment