Inkscape images with LaTeX fonts

With the release of Inkscape 0.48, it is now possible to match the font of the text in your images with the font used in your LaTeX document. This includes math environments and references. Hence that this is a very powerful tool that will make your LaTeX documents even prettier! Previously, this integration was also possible, but it was a bit of a pain. In this post I’ll show you how to get LaTeX typesetting in your Inkscape pictures.

Creating the image

For this example, I’m going to create a double pendulum. I’m not going to show you how to draw this, because it isn’t that hard and not in the scope of this blog. The result can be viewed here (Note: the .pdf image can be downloaded below).

Inserting text

This is the easy part, where it was before rather difficult. To insert text you don’t have to bother about the fonts you’re using. In my pendulum example, I want text next to the rods and the masses. I also want to indicate two angles. What I do is just pick the text tool in Inkscape and insert $m_1$ next to the first mass. The same is done for the second mass and in Inkscape it looks like this. This doesn’t look okay at all! But remember what I said before, don’t worry about the font.

After I put the rest of the text in my image, I’d like to export it as a .pdf file. To do so, go to File > Save As… > Choose Portable Document Format (*.pdf). Now, a small window pops up. Choose the settings as shown here. After you save, Inkscape had created to files: the .pdf image and an .pdf_tex file. The last one contains the text information.

Setting up your document

Open your LaTeX editor and create a document as. I used:

\documentclass[]{article}
\usepackage{graphicx}
\usepackage{color}
\usepackage{transparent}
\begin{document}
\begin{figure}[htb]
  \centering
  \def\svgwidth{200pt}
  \input{drawing.pdf_tex}
  \caption{Double pendulum}
\end{figure}
\end{document}

Let’s explain this a bit. The graphicx package is needed because we’re inserting an image. The color package and transparant package are needed if you use colors and/or transparency in you image. I’ve used grey once, which is treated as a color.
Inside the figure environment you see the svgwidth line. This line defines the width of your image, since this is not yet done in the .pdf_tex file created by Inkscape. The next line inputs the image with the text.

Done! Render your document and you’ll see that the text in your images perfectly fits the font you use in your document.

Making adjustments to the picture

In my example, I used a rather large font in Inkscape. This way, it’s pretty hard to place the text on the exact right spot (as can be seen in the compiled PDF, downloadable below). It’s wise to use a small font, for instance Arial size 6. Using a smaller font will make it a lot easier to place the text in the right place, especially if you have a lot of text to insert.

If you find the result compiled by LaTeX not satisfying and you want to adjust the position of some text elements, there are a few ways to do this. The first, of course, would be to adjust the whole image in Inkscape. But another way is to open the .pdf_tex file in your LaTeX editor. Scroll down to see the picture environment and notice the \put commands. These can be interpreted as follows:

\put(x,y)

There is more code following this command, but by changing the x and/or y components of the command you can shift the picture horizontally and/or vertically. This is a nice solutions to make small adjustments.

Download

You can download the content of this post in a zip file. In here, the image files are included. Also I used the blindtext package to create dummy text. In the .tex file, the used commands are commented.

32 Comments

  1. Emer says:

    the text part is blank after I generate the pdf file following your settings. Wierd.

  2. Andrestand says:

    Really nice and necessary article!

    But I’m sadly having some troubles with the procedure :(
    First I discovered that in my drawing InkScape is saving a 6 pages pdf while calling to pages 7 and 8 in the pdf_tex. And secondly, an “undefined control sequence” error arises when I try to use the command \text{}. Any idea to solve any of the problems?

  3. etsh says:

    Thank you very much, that was helpful

  4. […] a few post have been dedicated to figures (or floats in general) in LaTeX. Especially the post on how to use Inkscape to create figures with LaTeX fonts. I work a lot with MATLAB, a numerical computing environment, and the figures/plots it creates are […]

  5. Santiago says:

    Thanks!

  6. Michael says:

    Thanks for your help!
    Do you know how starting with the .svg figure done with Inkscape to obtain a .eps figure with the LaTex font in it (not in a a eps_tex)?

  7. Simone says:

    Ehi, very useful! But I couldn’t see the color gradient in my final pdf…! After I compile, it completely forgets about my gradient setting and I just get a uniform color in my pic!
    Any idea?

    • LL says:

      Had the same problem…try to view your pdf in another program. Some programs like Mac Preview (in my case) seem to have problems with displaying color gradients.

  8. shaho says:

    hi ,
    Thanks for trick , but when I compile the file in latex it gives me the following error :

    Cannot determine size of graphic in Figures/imageChain.pdf (no BoundingBox). …idth=\unitlength]{Figures/imageChain.pdf}}.

  9. hans says:

    Hi,
    how can i change the font in the picture?
    to change the font in my testdocument i am using the following commands:
    \fontfamiliy{ppl}
    \fontseries{m}
    \fontshape{n}
    \selectfont

    the normal text changes like i want it but in the picture ( \input{drawing.pdf_tex} )
    nothing changes… do i need to set something else? i heard that this should work… ?

  10. jose says:

    Nice Post!
    Thanks.
    It would be perfect for me y i could locate the images in a subfolder. I dont know why but i only can success if i put them in the root path (the same as the tex file)

    • Frits says:

      Hi Jose,
      That is possible by adding \input{myfigs/drawing.pdf_tex} to the preamble, where ‘myfigs’ is the name of the subfolder containing drawing.pdf_tex and drawing.pdf. If that doesn’t do the trick try to add \graphicspath{{figs/}} to the preamble as well.

  11. Jordan says:

    Hi, do you know how I get text on multiple lines? If I write “text *enter* text” to get it on two lines, it works well in inkscape but LaTeX just keeps it all in one long line.

    • Frits says:

      I think you should try the ‘LaTeX syntax’: “text \\ text” instead of “text *enter* text”

      • LaTeX User says:

        As you’ll find here on this LaTeX Community Forum, none of the usual LaTeX syntax line breaks work. You need to instead draw a text bounding box in Inkscape (rather than just clicking with the text tool and typing). Line breaks forced by the text bounding box in Inkscape will be enforced in the resulting LaTeX code because it puts this text in a minipage and uses “\\” to do a line break.

  12. Tweety600 says:

    Hi, I am experiencing difficulties with the method.

    My source code looks like the one on this page (difference: included packages are inputenc and babel) but I do not get ouptut as expected. Text is there (Title, caption and stuff) but the picture is missing.

    MacTeX2012, Inkscape 0.48.2, Mountain Lion.

    When I compile my .tex file, I do not even get an error message.

    When I open the drawing.pdf_tex, I see some code, looking reasonable to me.

    Any ideas?

    • Frits says:

      Does it work when you use exactly the same source code?

      Also, have you tried different PDF viewers? I recall that I once had a similar problem of a graphic not showing in Skim, but in Adobe or Preview.app it did.

    • Ash says:

      add this to the heading of the document
      it’s a path problem
      \graphicspath{{./the_graphics_folder/}}

    • Erick says:

      When you groupping in inkscape two objetcs together (latex fonts + rectangle), making borders of the sourrounding box invisibles you fix it. Then compile as usual.

  13. Núria says:

    Thank you for this nice trick and explanation – since psfrag stopped working well I was having real trouble. This is wonderful.

    I have a problem though. In pdf everything seems to work fine. Instead, when using eps figures, all labels appear misplaced if the tex commands surpass the drawing area.

    I believe the reason is that the bounding box of the figure seems to include the tex commands in the svg file, while the coordinates are calculated with respect to the naked drawing.

    Does anyone know how to fix this? Restricting the tex labels not to exceed the drawing is not a good option if you have many complicated tex commands.

    Thanks!

    • Frits says:

      Sorry for the (very) late reply. If you’re still having this problem, email me with a minimal working example. Then I’ll try to figure out what’s going on!

  14. Bee says:

    Hello,
    Thanks for this useful article! How do I use the \put(x,y) correctly? Do I have to write it in inkscape or latex? And where do I know the exact coordinates for x and y from?

    Thank you for your help,
    Bee

    • Frits says:

      Hi Bee,
      The idea is that you create a figure, including some text and/or formulas, in Inkscape. Then you export the figure to .pdf_tex format, as described in my post. If you’re not satisfied with the placement of the text/formulas in your figure when you compile your LaTeX document, then you can open the .pdf_tex file in your LaTeX editor and there you will see command that look like: \put(x,y). Where x and y are values.

      So, Inkscape automatically generates the \put(x,y) commands for you and if you’re not happy with that afterwards, you can manually change them. This is unfortunately trial-and-error: change the command, compile your LaTeX document, check if the result is better, etc.

      I hope that answers your question.

  15. Carlos says:

    It’s good, I didn’t know about of latex text on version 0.48, every time I had to typing on a editor latex online then download the file with extension .svg and add it to inkscape like a out file haha. By the way on linux is so easy to do it, but in OS X was hard try to do latex text, on the web there are some tips but I didn’t like it. Thanks so much!

  16. mas says:

    Frits, I checked again on a clean $HOME dir. Same result. Yes, the settings are exactly the same.

    I will check on a couple of other machines and then file a bug report.

  17. Frits says:

    MAS, that’s strange. Did you use the same settings as in the screenshot here?
    I’ve tested it once more on both OS X and W7 and it works fine, so you might want to search for help at the Inkscape Forums.

  18. MAS says:

    Nice tip. But, when I tried saving a drawing, inkscape is saving the whole page and the result is not as expected. Maybe it is an inkscape bug. I am using inkscape 0.48.1-2 (debian testing).

  19. Frits says:

    Daniele, I have heard about inkscape2tikz. It’s indeed a great package.

    The result of inkscape2tikz is basically the same as the result of the method described in this post (note the last but one paragraph of the post, about making adjustments). If you’re not familiar with TikZ, my solution is probably more straight-forward if you want to edit the text in your image later on. If you want to edit the image itself later on, the inkscape2tikz package is indeed very powerfull.
    Thanks for the heads up!

  20. Daniele says:

    Hello! Have you tried inkscape2tikz?

    http://code.google.com/p/inkscape2tikz/

    It does a very nice job converting your SVG to a tikz picture. The produced code is very clean and you can hand edit it to adjust it. You get the nice wisiwig interface of inkscape plus all the nice things of pgf/tikz. I’m really sold!

    Cheers

  21. Frits says:

    Oops, the link to the zip file was wrong. It’s fixed now, thanks for the notice.

  22. mbork says:

    Very nice, but I can’t find the final pdf!

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>