Sonntag, 5. Dezember 2010

Create diagrams with math symbols from Dia / Xfig to embed in LaTeX

Ok, here's the first reasonable post which originates from the frustrating search of how to embed diagrams in your LaTeX document. Of course there are various ways to embed vector graphics but things get tricky if you want to use math symbols in the figure. Then you need TeX to render the math stuff.

What finally worked for me was to draw nice diagrams in Dia, write the labels as TeX source, export it as a .fig file and then convert it to PDF. For the last step you need Linux or maybe Cygwin for Windows. When you have a PDF you can embed it into your LaTeX document whether you're using pdflatex or latex.

The short version:
  1. $> sudo apt-get install fig2ps
  2. Write TeX - math code in your diagram, surrounded by $-signs
  3. Don't use special characters like underscores in non-math labels (or escape them)
  4. Export diagram from Dia to .fig (Xfig format)
  5. $> fig2pdf yourDia.fig
  6. \includegraphics{yourDia.pdf}
The long version:
Step 1 - Create diagram
As you would write formulas in TeX you have to set Dollar signs around your code. So to write the fraction 4/5 the label in Dia would be $\frac{4}{5}$
Finally your diagram will look something like this. Hopefully not as ugly if "snap to grid" worked better for you ;) You can see it won't be easy to set the label to the correct position as the rendered math text will (usually) be more compact.Step 2 - Export and convert
Now export the diagram to a .fig file (Xfig format). The conversion is then done painlessly by the perl script fig2pdf (or fig2ps) which is a part of TeXmacs. I guess you can find this in the standard repositories of most Linux distros. For Windows Cygwin should probably work too, any feedback is welcome. In Ubuntu just apt-get the package fig2ps. There is another script which I didn't test with the same name for the same purpose from here.

The compilation, rendering, composition,.. (dont really care) is simply done by:
fig2pdf yourDia.fig




Be careful not to use special characters like underscores in your non-math labels or TeX is gonna mess things up. The fig2pdf script will hang during compilation. I guess you could for example escape underscores and backslashes as \_ and \\ but I didn't try that.

0 Kommentare:

Kommentar veröffentlichen