How to include SVG diagrams in LaTeX?
The latest 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 official site.
There is a free article about the export on CTAN: How to include an SVG image in LaTeX.
In May 2015, you could read from that article, that the quickest way to get this done was:
inkscape -D -z --file=image.svg --export-pdf=image.pdf --export-latex
But as Peter Mukhachev pointed out in his comment, the inkscape command has changed, it is now (as of EDIT July 2020) on more recent versions of inkscape correct like this:
inkscape -D image.svg -o image.pdf --export-latex
In Jan 2020 and Inkscape 1.0beta2, this command is correct for macOS Catalina (thanks to moazzem's comment):
/Applications/Inkscape.app/Contents/MacOS/inkscape -D -z image.svg --export-type="pdf" --export-file=image.pdf --export-latex
Then include the generated TeX file, which will automatically import the image:
\begin{figure}
\centering
\def\svgwidth{\columnwidth}
\input{image.pdf_tex}
\end{figure}
Be aware that this uses \includegraphics
to insert the graphic, so you need to include graphics
or graphicx
in your document.
Notice that using \input
you lose the resizing functionality of \includegraphics[]{}
. Instead, you can use something like \scalebox{}{}
. For instance:
\begin{figure}
\centering
\def\svgwidth{\columnwidth}
\scalebox{0.5}{\input{image.pdf_tex}}
\end{figure}
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.
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:
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
andpstopdf
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
andrm
. 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!
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
):