Vladar's Blog

LaTeX: SVG to TikZ conversion

There might come a time when I get to write a post on the functionality of the PGF1 package and its TikZ syntax layer, but I'll leave such a complex topic alone for now. Nevertheless, you can still use this package without fully understanding its inner workings. For example, you can export an SVG image directly into the TikZ code to draw in the document without the need for any external image files.

tikz-svg

To do the conversion, you need to use the Inkscape2 vector editor. Specifically, the svg2tikz3 Inkscape extension. The installation instructions can be found here.

Once you have installed the extension, you can load your SVG image and run the process from Extensions → Export → Export to TikZ path…:

tikz-export-plugin

The resulting tex file should look like this (the middle part is omitted for brevity):

\def \globalscale {1.000000}
\begin{tikzpicture}[y=1pt, x=1pt, yscale=\globalscale,xscale=\globalscale, inner sep=0pt, outer sep=0pt]
\path[fill,line width=1.000pt] (5.2798,15.5906) .. controls (4.1738, 15.5906)
[...]
\end{tikzpicture}

First, if we want to use this picture as a glyph in the text, let's define the \docFontSize length, containing the default font size of the document. If you want to use it as a normal image, you can omit this step, keeping the original y and x values.

% Document font size
\newlength{\docFontSize}
\makeatletter
\setlength{\docFontSize}{\f@size pt}
\makeatother

Now, you need to modify the tikzpicture's environment parameters:

Wrap the code into \newcommand and you are done!

\RequirePackage{tikz}

\newcommand{\iconItDR}{%
    \begin{tikzpicture}[baseline={([yshift=-0.35\docFontSize]current bounding box.center)}, y=\docFontSize, x=\docFontSize, yscale=0.1, xscale=0.1, inner sep=0pt, outer sep=0pt]
    	\path[fill,line width=1.000pt] (5.2798,15.5906) .. controls (4.1738, 15.5906)
[...]
    \end{tikzpicture}
}

Test it in a large block of text to confirm the correct placement and proportions:

tikz-export-example

You can view the whole code in the itdr-core4 repository.


Discuss this post on Reddit

  1. https://www.ctan.org/pkg/pgf↩

  2. https://inkscape.org/↩

  3. https://github.com/xyz2tex/svg2tikz↩

  4. https://github.com/Vladar4/itdr-core/blob/2024-04-20/core.sty#L270↩

#guide #inkscape #itdr-core #latex #software