I often draw images for papers in Inkscape. What’s the best way to get them into the paper? There are many options:
- Export to an image file format, e.g., jpg or png. But that does not preserve the main advantages of vector graphics: arbitrary upscaling without quality loss.
- Export to pdf: Better. Preserves the vector graphics aspect. But uses inkscape fonts, which is annoying if you want to use and LaTeX-math in your figure.
- Direct export to tex (“LaTeX with PSTricks-Macros”): sounds better, but plainly does not work.
- Export to pdf with text rendered by LaTeX: Way to go! This works fine. The drawing-parts are saved as pdf, whereas all text is exported to a tex file and later typeset by latex.
There are only two issues that remain with the latter approach: First, as text is now rendered by LaTeX, its proportions are different compared to what you see in inkscape. This often requires some manual tweaking. Secondly, the process editing the Inkscape svg and then manually exporting the files that can be used by LaTeX is a bit tedious. In the following, I will show how this can be automated.
Luckily, the second point is solvable, as Johan B. C. Engelen shows in his tutorial on including SVGs in LaTeX. As some of the commands in the tutorial are outdated, I have adapted them to work with modern inkscape versions (I am using Inkscape 1.1).
First, let’s set up a document skeleton. It already contains the svg-to-pdf-conversion magic.
Content of document.tex file. Additionally, download demo.svg to try it yourself!
\documentclass{article}
%required for typesetting advanced math
\usepackage{amsmath}
% required for including the graphics
\usepackage{graphicx}
\usepackage{pstricks}
% Detect file changes
\newcommand{\executeiffilenewer}[3]{%
\ifnum\pdfstrcmp{\pdffilemoddate{#1}}%
{\pdffilemoddate{#2}}>0%
{\immediate\write18{#3}}\fi%
}
% Automatically generate pdf with latex fonts from inkscape svg
\newcommand{\includesvg}[1]{%
\executeiffilenewer{#1.svg}{#1.pdf}%
{inkscape --export-type=pdf --export-latex --export-filename=#1.pdf #1.svg}
\input{#1.pdf_tex}%
}
\begin{document}
This is the document body.
\begin{figure}
% use this command to scale your image up or down
% Use the filename without trailing .svg in the
% includesvg command.
\scalebox{0.9}{
\includesvg{demo}}
\end{figure}
\end{document}
To get this to run, you need inkscape in your PATH environment variable. Moreover, as pdflatex
now has to execute shell commands (i.e., calling inkscape for the conversion), it needs to be started as
pdflatex --shell-escape document.tex
. All conversions should now happen automatically as soon as the svg-file (here demo.svg) is changed.
Here is how the document looks in Inkscape
And this is how it looks in the pdf
For even more convenience, I recommend you to use latexmk, which automatically detects changes to your tex-file, re-compiles the document and updates the preview, if you call
latexmk -pvc -pdf document
. It can be configured via the .latexmkrc file in the document root. Here, we add the shell-escape-command, so that the images get rendered correctly. Moreover, when using Windows, Acrobat reader does not support updating the preview document. Instead, I recommend to use Sumatra, which is also configured in the .latemkrc file:
$pdf_previewer = 'start "C:\Program Files\SumatraPDF\SumatraPDF.exe" %O %S';
$latex = 'latex %O --shell-escape %S';
$pdflatex = 'pdflatex %O --shell-escape %S';