Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 welcome. Tips and personal experience on which tools you have found to be working reliably would be also appreciated.

share|improve this question
1  
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 Blessed Aug 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 Tapankov Aug 20 '10 at 18:19
    
Good question. But is exporting text labels as vectors is always annoying? –  Orion Nov 4 '13 at 11:57
add comment

16 Answers 16

up vote 86 down vote accepted

The latest alpha version of Inkscape supports PDF+LaTeX export. The graphic itself is exported as PDF, but all text can be put into a .tex file. This way all the text appears in your document font.

The latest Inkscape version can be found on the SourceForge Project site (v0.48).

There is also a free article about the export on CTAN: How to include an SVG image in LaTeX.

EDIT:

After this answer got so many votes over the time, here a little update: The LaTeX feature is now implemented in Inkscape (as mentioned in the comments). You can download the current stable release of Inkscape.

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 abject, make sure the justification is correct. enter image description here

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.

share|improve this answer
    
This is the same what Bran has written in the first comment. –  Martin Tapankov Aug 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 H Aug 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 Hlavacek Feb 23 '11 at 15:43
1  
I've already posted this below, but since this is the accepted answer I should probably mention here that the above link (article) appears out of date. The current version (judging by context, given that I've not seen the original document - I may be mistaken) is at mirrors.ctan.org/info/svg-inkscape/InkscapePDFLaTeX.pdf –  Nick Loughlin Jun 15 '11 at 21:54
    
@Nick The link was indeed broken and I changed it to the one you provided –  Martin H Jun 15 '11 at 22:48
show 3 more comments

As the other answers already mention, the TeX backends do not support the SVG file format. That means there is no other way than to convert the SVG images.

However, ConTeXt provides transparent conversion (complete list: ConTeXt Dependencies) of several file formats that are not natively supported by LuaTeX, e.g.:

  • converting PostScript images to PDF
  • converting GIF and TIFF images
  • converting SVG and compressed SVG

The SVG conversion is done in the background using inkscape. This means from the users' point of view there is no difference if a PNG or a SVG file is included. Example:

\starttext
  \externalfigure [image.svg]
\stoptext

A single compilation run with context file creates the temporary image m_k_i_v_image.pdf which is inkscapes output and is transparently included.

Result of the code above (a file only containing the SVG image image.svg):

result

share|improve this answer
8  
ohhh... +1 for the fancy graphics. –  naught101 Apr 29 '12 at 6:31
add comment

If you figures are sufficiently simple TikZ 2.0 provides the SVG path operation:

\path . . . svg[<options>] "<path data>" ... ;

See the pgfmanual section 14.12. Only SVG 1.1 path data is supported.

share|improve this answer
add comment

Inkscape has a(n experimental) tikz exporter: link. (I've never used it but perhaps someone who has will stop by and give a few more details.)

share|improve this answer
    
Isn't it only for paths? –  Łukasz Lew Aug 21 '10 at 14:08
add comment

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):

  • Inkscape (for using the technique already mentioned in other answers)
  • ImageMagick (actually the included convert)
  • only MiKTeX users: Xpdf (actually the included pdftops)

Notes:

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

  2. 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.

  3. 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.

    rm.cmd:

    @echo off
    del /Q %*
    

    The switch /Q also suppresses any question!

share|improve this answer
    
Very useful information indeed (for miktex users) :-) –  Harish Kumar Nov 12 '12 at 1:50
    
I'd love it more if it could externalise the conversion (similar to TikZ) so I'd not need --shell-escape. –  Raphael Jan 17 at 13:27
add comment

Some additional information concerning Andrew's answer. As far as I know, LaTeX does not support including SVG files directly.

share|improve this answer
    
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 Tapankov Aug 20 '10 at 18:18
4  
This link appears broken. It appears that it is now hosted here:mirrors.ctan.org/info/svg-inkscape/InkscapePDFLaTeX.pdf (or that this is an update). –  Nick Loughlin Jun 15 '11 at 21:52
    
If you were referring to tug.ctan.org/tex-archive/info/svg-inkscape I don't think it is an addition to Andrew's answer because it is based on exporting PDF+LaTeX while Andrew is talking about this: wiki.inkscape.org/wiki/index.php/… Would you care to elaborate? –  Stephan Lehmke Jan 18 at 7:05
add comment

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.

share|improve this answer
add comment

There are several SVG → PDF converters available, e.g. Apache Batik's rasterizer.

share|improve this answer
22  
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. –  lazyboy Oct 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 Floch May 22 '13 at 16:48
add comment

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.

The original Inkscape figure: The image you see in Inkscape

\documentclass{article}
\usepackage{graphicx,xcolor}
\begin{document}

\begin{figure}
\centering
\def\svgwidth{\columnwidth}
\input{inkscape-pdf.pdf_tex}
\end{figure}

\begin{figure}
\centering
\def\svgwidth{0.5\columnwidth}
\input{inkscape-pdf.pdf_tex}
\end{figure}

\end{document}

LaTeX output:

enter image description here

share|improve this answer
add comment

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.

share|improve this answer
add comment

As this questions is still one of the first hits for "svg latex" I would like to add something to this topic.

  • You can't directly include svg vectorgraphics. You have always to convert them.
  • Conversion at the moment is easiest possible with inkscape, because it offers access to this functionality via command-line.

To make it short: use the svg-package by Phillip Ilten and see this blog post on laclaro.wordpress.com for examples and more features.

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.

share|improve this answer
add comment

If you are using LyX, then it is simple; add the directory of Inkscape to the "Paths" in LyX's settings, reconfigure twice, and you can include SVG files just like any other files.

If you are writing LaTeX code, maybe the package svg-inkscape will help you. (I never tried)

share|improve this answer
add comment

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:

% cludein.sty: Include various file formats directly in a LaTeX file

% *****************************************************************************
% *******   NOTE: pdflatex must be run with the -shell-escape flag in order for
% *******         this package to work.
% *****************************************************************************

% This package provides the following commands:
%    \includesvg -- include an SVG image
%    \svgfig     -- create a Figure environment with an SVG image
%    \includelyx -- include a LyX image
%
% USAGE:
%   \includesvg[width]{path/to/dir/of/svg/file}{name-of-svg-file-without-extension}
%
%   \svgfig[placement][label][width]{figname}{caption-text}
%       -- default arguments can be used with an empty []:
%       for example, \svgfig[tb][][5cm]{myfigname}{Some caption text.}
%       uses the default label of fig:myfigname (in this example).
%
%   \includelyx{path/to/dir/of/lyx/file}{name-of-lyx-file-without-extension}

\NeedsTeXFormat{LaTeX2e} 
\RequirePackage[multidot]{grffile} % allow multiple dots in graphics filenames
\RequirePackage{xargs}
\RequirePackage{color}
\RequirePackage{graphicx}

\ProvidesPackage{cludein}[2011/01/17 v0.1
                          direct inclusion of various file formats]
\typeout{-> cludein.sty <- by Mark Edgington, 2011}
%
%%%%%%%%%%%% BEGIN: auto-include SVG / LyX files %%%%%%%%%%%%%

% usage: \includesvg[width]{path/to/dir/of/svg/file}{name-of-svg-file-without-extension}
% if source file doesn't exist, nothing is done...


\newcommand{\executeiffilenewer}[3]{%
\ifnum\pdfstrcmp{\pdffilemoddate{#1}}%
{\pdffilemoddate{#2}}>0%
{\immediate\write18{#3}}\fi%
}


\newcommand{\includesvg}[3][\undefined]{%
\IfFileExists{#2/#3.svg}{%
\executeiffilenewer{#2/#3.svg}{#2/output/#3.pdf}%
{mkdir -p #2/output ; %
inkscape -z -D --file=#2/#3.svg %
--export-pdf=#2/output/#3.pdf --export-latex}%
\graphicspath{{#2/output/}}% search here for output pdf
% set figure width
\ifx#1\undefined
    \let\svgwidth\undefined
\else
    \def\svgwidth{#1}
\fi
% properly center a potentially overwide image
\makebox[\textwidth][c]{%
  \input{#2/output/#3.pdf_tex}%
}%
}{\colorbox[rgb]{0.7,0.7,0.7}{\textcolor{black}{MISSING FIGURE}} }% end of IfFileExists
}


\newcommand{\includesvgnobox}[3][\undefined]{%
\IfFileExists{#2/#3.svg}{%
\executeiffilenewer{#2/#3.svg}{#2/output/#3.pdf}%
{mkdir -p #2/output ; %
inkscape -z -D --file=#2/#3.svg %
--export-pdf=#2/output/#3.pdf --export-latex}%
\graphicspath{{#2/output/}}% search here for output pdf
% set figure width
\ifx#1\undefined
    \let\svgwidth\undefined
\else
    \def\svgwidth{#1}
\fi
% properly center a potentially overwide image
\input{#2/output/#3.pdf_tex}%
}{\colorbox[rgb]{0.7,0.7,0.7}{\textcolor{black}{MISSING FIGURE}} }% end of IfFileExists
}

% TODO: make secondary \includesvg command which instead of directly exporting to PDF (which
% currently is uncompressed with inkscape), does something which results in a compressed PDF. (maybe
% postprocess the uncompressed pdf?)

% usage: \svgfig[placement][label][width]{figname}{caption-text}
% -- default arguments can be used with an empty []:
%    for example, \svgfig[tb][][5cm]{myfigname}{Some caption text.}
%    uses the default label of fig:myfigname (in this example).
\newcommandx{\svgfig}[5][1=tb, 2=\undefined, 3=\undefined, usedefault]{%
\begin{figure}[#1]%
\begin{center}%
  \includesvg[#3]{svg}{#4}%
  \caption{#5}%
\ifx#2\undefined%
    \label{fig:#4}%
\else%
    \label{#2}%
\fi%
\end{center}%
\end{figure}%
}

% export and include lyx files!
% sed is used to keep only the stuff inside the document-environment 
% sed command is: sed -i '0,/\\begin{document}/d; /\\end{document}/,$d' file-to-replace.tex
\newcommand{\includelyx}[2][lyx]{%
\executeiffilenewer{#1/#2.lyx}{#1/output/#2.lyx_tex}%
{
mkdir -p #1/output ; %
lyx -e pdflatex #1/#2.lyx ; %
mv #1/#2.tex #1/output/#2.lyx_tex ; %
/bin/sed -i '%
0,/\string\\begin{document}/d; /\string\\end{document}/,$d
' %
#1/output/#2.lyx_tex}
\input{#1/output/#2.lyx_tex}%
}

%%%%%%%%%%%% END: auto-include SVG / LyX files %%%%%%%%%%%%%
share|improve this answer
    
Note, that also the recently published package svg contains a command \includesvg, cf. my answer. Not that you need it with your own approach … –  Speravir Sep 30 '12 at 0:53
add comment

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:

\newcommand{\executeiffilenewer}[3]{%
\ifnum\pdfstrcmp{\pdffilemoddate{#1}}%
{\pdffilemoddate{#2}}>0%
{\immediate\write18{#3}}\fi%
}
% includesvg[includegraphics args]{file} command (linux-version)
\newcommand{\includesvg}[2][]{%
\executeiffilenewer{#2.svg}{#2.pdf}{%
/usr/bin/inkscape -z -C --file="#2.svg" --export-pdf="#2.pdf" >temp.txt}%
\ifthenelse{\equal{#1}{}}{%
\includegraphics{#2}}{%
\includegraphics[#1]{#2}}%
}

It's directly based on laclaro's "old way" (in German, but the LaTeX is comprehensible combined with my rusty rudiments of German), but with the inkscape options changed.

I'm inclined to suggest this as an option to the really good svg package (with some upgrades to things like the scale specifier, which I may get round to putting in).

Edit: it can now pass arguments to the \includegraphics command

share|improve this answer
add comment

If you are in engineering or any of the sciences, I would check out Ngraph also:

http://sourceforge.net/projects/ngraph-gtk/

share|improve this answer
add comment

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.

Here's an example of how to use it:

svg2pdf mydiagram.svg mydiagram.pdf
share|improve this answer
3  
Note that the question explicitly asks for inclusion without conversion to an intermediate format. –  Stephan Lehmke Jan 15 at 16:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.