I'm making all my diagrams in SVG format using Inkscape and then I export them to some other format (e.g. PDF, EPS or PNG). I wrote a short script that does this for me automatically, but I'd like to avoid this step, if possible --- sometimes the conversion from SVG converts the text labels into vectors, and that's annoying. Hence the question:

Is there a way to include SVG diagrams directly without conversion to an intermediate format?

Package suggestions, or instructions for specific LaTeX distribution are welcome. Tips and personal experience on which tools you have found to be working reliably would be also appreciated.

What types of diagrams are you creating with Inkscape? Maybe you could do them via TikZ or gnuplot. The latter offers a special LaTeX-export, thereby allowing you to use mathmode for labels etc.
–
Bran the BlessedAug 20 '10 at 18:09

TBH, I've never used TikZ so far, but I guess it's time to give it a try for the next paper. I make all kinds of diagrams, mostly engineering-oriented (e.g. free-body diagrams, component loads and such), but sometimes more generic boxes-and-arrows stuff.
–
Martin TapankovAug 20 '10 at 18:19

Good question. But is exporting text labels as vectors is always annoying?
–
OrionNov 4 '13 at 11:57

Be aware that this uses \includegraphics to insert the graphic, so you need to include graphics or graphicx in your document.

Further, here are two good things that I found over time of using this feature. Both are mentioned in the linked pdf, but I missed those hints for quite some time.

When you insert text, there is a little square in the textbox which shows you the alignment of the text. For example if you want to have an arrow pointing from your text to an object, make sure the justification is correct.

For some time I was thinking, that text could not handle line breaks, but it is possible if you flow text into a shape. Draw a rectangular shape, write some text, select both and then go in the text menu to "Flow into frame". The shortcut for this is Alt + W.

This is the same what Bran has written in the first comment.
–
Martin TapankovAug 20 '10 at 20:08

1

Besides that he did not mention that the 0.48 version is needed. This is however in the pdf he liked. I didn't check the link - my bad
–
Martin HAug 20 '10 at 20:14

1

By the way, Inkscape 0.48 is now officially released, so it is not an alpha version any more. The LaTeX export feature is now an official feature of Inkscape.
–
Jan HlavacekFeb 23 '11 at 15:43

This tool is really nice, but you do need to be careful about scaling - you can set \svgwidth but the text is then positioned correctly but not scaled, or you can wrap the whole \input in a \resizebox which scales the text correctly - often to too small.
–
Chris HSep 10 '13 at 10:48

2

This is clearly not an answer to the question. If the SVG file is originating from any programme other than Inkskape the extra conversion is still required. Moreover, there is nothing in this solution concerning direct inclusion of SVG files in tex documents.
–
Luís de SousaDec 10 '14 at 19:35

There is now (at time of writing for about a month) a package svg on CTAN and also included into the big TeX distributions.

This package makes use of pdfTeX primitives. Not all of these are defined in LuaTeX, so you would get errors on compiling. See answer of Heiko Oberdiek for a solution.

Every SVG file given by the command \includesvg will under the hood be converted with the help of some additional programs, which at least on Windows are not installed by default (the package claims, it wouldn’t run in Windows, but see below):

For compilation pdflatex needs the command line switch --shell-escape.

All executables/binaries must be located in the search path. In Windows only the “ImageMagick” installer does this by default. For inkscape and pstopdf one needs to add the paths oneself, or I would recommend for each a batch file in the binary path of your local texmf tree (which anyway should be itself in the search path). Additonal hint for MiKTeX users: Create a local texmf tree in MiKTeX.

inkscape.cmd(it must get this name!):

@echo off
<path-to-inkscape>\inkscape.exe %*

pdftops.cmd(it also must be named this way!), not needed for Users of TeX Live:

@echo off
<path-to-Xpdf>\pdftops.exe %*

Of course, adjust the paths to your local settings.

The package uses the *nix specific commands mv and rm. In Windows we can emulate them once more with batch scripts, which again must get the names given here and should be put into the bin folder of the local texmf tree:

mv.cmd:

@echo off
move /Y %*

The switch /Y overwrites existing files without any question! I introduced it here for the use with TeX editors.

I'd love it more if it could externalise the conversion (similar to TikZ) so I'd not need --shell-escape.
–
RaphaelJan 17 '14 at 13:27

4

This should actually be the top answer because answer actually answers the question of "how to include SVG without converting".
–
user1271772Aug 4 '14 at 11:30

@user1271772: Thank you for this implicit compliment, but in fact also with this package the svg files are converted, but this happens under the hood during the compilation process.
–
SperavirAug 4 '14 at 15:29

1

@cfr Nope, there are other modes. You can have a make file to run afterwards, or a list of figures.
–
RaphaelApr 30 at 10:14

That's useful --- if only the Inkscape version required was released. Still, it mostly goes around the problem and there's an intermediate format, but at least pdflatex is supposed to detect changes in the SVG -- which is mostly what I need.
–
Martin TapankovAug 20 '10 at 18:18

You may also want to look at Ipe. It is a graphics editor similar to Inkscape, but integrates tightly with LaTeX, and exports eps and pdf files that are easy to include into TeX documents. It also comes with a svgtoipe script that converts many svg figures to the ipe format, so that they can be edited with Ipe. I'm not sure how well does the conversion handle text, but it is easy to add the necessary labels in Ipe, and since Ipe uses LaTeX to render text, you can have your labels match your text perfectly.

It not just exports to PDF, it saves the entire image as a PDF (or EPS). All Ipe data is inside that PDF, so you can just reload the PDF in Ipe and edit it if you want to change your drawing. Basically, the file you edit is the same file as you include in LaTeX, so no need to do an export each time you change the file.
–
Gijs van OortNov 20 '14 at 14:54

For including the SVG in LaTeX, you can refer to Marin H answer. But as Latex does not natively support SVG, you may run in to font size and placement issues. During the conversion from SVG to LaTeX as Inkscape carries out, all the positions are changed to absolute X, Y values, thus your alignments are lost in the conversion, as it does not recognize it.

Unless you exactly use the same LaTeX font size/type in Inkscape you will get a different output from what you see in inkscape. I gave up this route due to these problems and went for the normal SVG->PDF inclusion.

The example below illustrates the problem. The file inkscape-pdf.pdf_tex is generated using the PDF+LaTeX option in Inkscape.

It's a shame they called it a rasterizer!
–
Joseph Wright♦Aug 21 '10 at 12:24

Why is it a shame? I have ever used it therefore I asked.
–
lazyboyOct 2 '12 at 19:20

3

@lazyboy because rasterisation denotes the act of converting a vector graphic (where shapes are given by coordinates) into a raster format (typically described by its pixels or similarly lossy data), but pdf is able to contain vector graphics. So the rasterizer does more than just rasterize.
–
Bruno Le FlochMay 22 '13 at 16:48

Having read your comment, maybe a combination of Inkscape and the overpic package may be helpful. This package allows you to write any LaTeX commands on top of an existing picture. A colleague of mine uses it to draw pictures of free-form curves, for example, and adds all labels and points of interests manually. The package allows you to display a grid, so it should not be too tedious---depending on the size of your picture, of course.

The longer story: It all started some time ago, when "Oni" wrote a python script, designed as inkscape extension to convert svg to tex+pdf (can still be found on the net). Today this plugin is a standard inkscape export option. The inkscape developers provided a document how to manually use this new feature from a latex document (called svg-inkscape).

Based on that I wrote about including SVG in LaTeX documents, and added some bits to the includesvg command and some months later there was the "svg" package published by Philip Ilten, which I recommend everyone to use. Some extended examples, merging my work and that of Philip can be found on http://laclaro.wordpress.com.

Although this is a bit of a late answer, I just noticed this question today. For quite some time, I've been using a small package which I wrote to do this (as well as including simple LyX files), which I've called "cludein" -- it works well with POSIX operating-systems, and requires running pdflatex with the -shell-escape flag.

Not sure if I should post it here, but since I don't have it anywhere else on the internet at the moment, why not:

With great thanks to the work of laclaro and Philip Ilten, I suggest the following alternative.

I want to do something just very slightly different - I don't want LaTeX to do the text for me, as the text in my figures is often rather tightly sized/positioned, and I have a preference (on the basis of readability, not aesthetics) for sans serif in figures, especially when there is vertical or very small text. So here's the code:

Another alternative is to use svg2pdf, from the Gnome Librsvg library (which is available on Linux and OSX (via MacPorts/brew)), to convert the SVG file to PDF format and include it as a PDF in Latex. This appears to produce a vector version of the figure which looks good.