Wednesday, June 11, 2008

Integrating Inkscape graphics in LaTeX

Getting good-looking diagrams and figures into a LaTeX document can be tricky. My favourite software (and I think it ought to be anyone's favourite) for drawing such figures is Inkscape. This post explains how to get text in the proper font into Inkscape, how to put equations into Inkscape drawings, and how to get those drawings out of Inkscape and into your LaTeX document.

It is a good idea to use the latest version of Inkscape, because the program is rapidly being improved all the time.

LaTeX font in Inkscape

To make your figure look good in its environment, you can use the same font family that the surrounding body text uses. In LaTeX's case: Computer Modern.

Installing fonts

As Computer Modern is written in the METAFONT format, it cannot be directly used in Inkscape. For that, we need the font in OpenType (OTF) format, preferred for Linux (and MacOS?) systems, or in TrueType (TTF) on Windows. (Side note: here's an interesting article on the differences between all the font formats.)

The BaKoMa font bundle provides the Computer Modern font in these and some more formats. Download it here, then extract to a temporary directory. Installation is as follows:

Open up Fonts in the Control Panel and drag-and-drop all files from the ttf directory into here.
Ubuntu Linux
Open a file browser (Nautilus) and navigate to fonts://, then drop the fonts from the otf directory here. You may need to run Nautilus as root using the command gksudo nautilus.
Non-Ubuntu Linux (and Ubuntu Hardy, because they broke it)
Copy the files (as root) from the otf directory to anywhere you like inside /usr/share/fonts, then run sudo fc-cache -fv. (For a single-user installation, ~/.fonts might work, but no guarantees!)
Creating the figure

If you start Inkscape, new fonts with names like BKM-cmr10 should be available. Here, cm stands for Computer Modern, r means roman (normal body-text font) and 10 is the point size. Simply use this font for all the text in your illustrations to make them integrate seamlessly with the text in LaTeX.

Or, almost seamlessly. It seems that Inkscape (version 0.46) does something strange with the font size, or the BaKoMa fonts are too small to begin with. In any case, I find that using BKM-cmr10 at 12 points in Inkscape provides the best match to the default 10-point LaTeX body font. I personally prefer BKM-cmss10, the sans-serif version of Computer Modern, because it integrates nicely with abstract line drawings and with the surrounding serif body text, but if you use mathematics in you figures this is probably not an option.

LaTeX equations into Inkscape

Yes, it is possible to add mathematical symbols and equations to your Inkscape drawing! You can also use this for normal text, but it is more cumbersome than the font approach detailed above.

Installing textext

First, you need textext. Simply extract the two files from the archive into /usr/share/inkscape/extensions for a systemwide installation, and fix the permissions:
sudo chmod 644 textext.inx
sudo chmod 755

(For a single-user installation, ~/.inkscape/extensions should work.)

You'll need some extra packages for the script to work. On Ubuntu:
sudo apt-get install python-lxml
sudo apt-get install pstoedit

On non-Debian based Linux distributions, install pstoedit and the Python lxml package in some other way. On Windows, see the textext web page for details.

Using textext

Now (re)start Inkscape, click Effects, Tex Text and type your LaTeX code! You can even load a preamble from a file to include additional packages such as amsmath. (Unfortunately the file must contain only the preamble, not an entire document.) The same problem occurs as with the previous approach: you need to set a scale factor of 1.25 to (approximately) match the font size of the LaTeX document. Close the dialog with the OK button or with Ctrl+Enter.

The equation (or other LaTeX text) is then placed as a group of shapes into Inkscape. To edit it (yes, that is possible!), select it and click Effects, Tex Text again. This feature is a little feeble, however: do not ungroup the text object, or else it will become uneditable.

From Inkscape to LaTeX

Exporting from Inkscape

When the figure is done, deselect all objects, then go to Document Properties and click Fit page to selection. This will adjust the page boundaries to fit exactly around all objects. Then save the figure to Inkscape SVG format (for later editing), but also save a copy as “PDF via Cairo”. Check the box to Convert text to paths, because otherwise the kerning seems to be messed up in the export.

Including in LaTeX

This is a simple matter of
in the preamble, and then placing the figure using
as usual. Do not use any of the scaling options of \includegraphics, since they will cause the text in the figure to scale as well, and it will no longer match the size of the surrounding body text.

Compile your LaTeX document using pdflatex (not normal latex, since that only handles inclusion of EPS files), and there you go!


Georg Muntingh said...

Show an example LaTeX page!

Anonymous said...

Is there anyway LaTeX equations (math symbols) can be added to Inkscape figures in Windows?

Anonymous said...

Okay, got it:
For Windows, etc.

Anonymous said...

I have also noticed that any opacity below 100 is not displayed very slowly in latex. Also the size of the pdf file is large. Before generating I make sure that the opacity of all objects is set to 100.

Thomas ten Cate said...

Yes, I've also noticed problems with transparency. In particular, the colours of my beamer slides would change on each slide with a partially transparent illustration on it...

mghansen256 said...

Well, the problem with the font-size could be explained by this bug-report:
One way around it would be to use pixels as the default unit for the document, set the font-size to 10px, and then just scale it up in LaTeX by 1.25:
This works with textext as well.

Thomas ten Cate said...

Yes, that might very well be the problem! Since the Inkscape folks are usually pretty quick to improve their software, I hope to see this fixed in the next version. That would definitely come in handy for my thesis :)

Anonymous said...

I use Latex (pdflatex) + Inkscape for my papers and presentations. Works nice - great finished product - one problem: when I create a new tex object in an inkscape drawing (>>Effects>>Tex Text), it is often inserted in some bizarre place way out of the screen where Im working - recently it has started appearing mirrored! No big deal, but anybody else get this??

Captain Crash said...

An alternative may be to use inkscapeLatexExtension, now standalone, which which converts a svg file into a latex text overlay and a pdf/eps. In this way, the text can be typeset by latex directly.

Thomas ten Cate said...

It's now called "svg2latex" and seems like a very good option indeed. Thank you, Captain Crash, for the pointer!

Captain Crash said...

Well, it's actually buggy. But in its web page, another tool called svg2tex is mentioned. I tried it and it worked much more stable, with a nice and neat manual.

gfh said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

Could anyone give me the link to download svg2latex?

I tried to use tex text, but i get an error in installation which i'm not so sure what to do about.


Thomas ten Cate said...

Here it is:

Anonymous said...

About the font size:
word, openoffice, latex and anything else uses as a measure "pt", which is defined as 72 pt per inch. So a text with 12 pt is 0.166666 inch tall.

For some reason (I guess you have to live with it) svg is using as measure px. Which is defines as 90 px per inch. So if you set 12 px, the text is only 0.133333 inch tall.

Solution: Set 15 px as font size.

I admit this is very annoying about svg. Svg was not inteded for print I guess.

kimberly said...
This comment has been removed by a blog administrator.
Penis Enlargement Pills said...
This comment has been removed by a blog administrator.
Anonymous said...

Inkscape 0.48 comes with a new PDF/EPS/PS+LaTeX output feature, that is worth looking at as an easier alternative to including graphics in latex. Have a look here:

pedro velasquez said...
This comment has been removed by a blog administrator.
pedro velasquez said...

Inkscape is an excellent program for creating vector graphics of all
kinds,dominical costa rica tours
especially SVG, but until recently I thought it was pointless to try
using it for anything containing mathematics because I couldn’t even figure
out how to make a subscript with it’s text tool. However, I recently
realized that it now has built-in support for embedding LaTeX equations, found
found right on the menu: Effects > Render > LaTeX formula.