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.
To do the conversion, you need to use the Inkscape2 vector editor. Specifically, the svg2tikz
3 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…
:
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:
baseline={([yshift=-0.35\docFontSize]current bounding box.center)}
- adjust theyshift
's value to place the image at the correct height in the line of text.y=\docFontSize
andx=\docFontSize
- set both of these to\docFontSize
yscale=0.1
andxscale=0.1
- adjust the scale until the image is scaled properly compared to the rest of the text.
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:
You can view the whole code in the itdr-core
4 repository.
Discuss this post on Reddit