tikz2svg

2014-03-09

I wrote a TikZ to SVG converter. It uses pdflatex and pdf2svg. It unfortunately litters the pdflatex output (into /tmp/<sha1 of input>/). I leave it in case you need to debug. And because this was a quick hack. As such, the code is not very good. But it works well! If you have questions, post in the gist and send me an email (gist does not notify). Cheers!

Usage

# input files
> tikz2svg in.tikz > out.svg

# input stdin
> tikz2svg < in.tikz > out.svg

> cat in.tikz

\begin{tikzpicture}
  \fill[red!90!black]   ( 90:.6) circle (1);
  \fill[green!80!black] (210:.6) circle (1);
  \fill[blue!90!black] (330:.6) circle (1);
\end{tikzpicture}

> cat out.svg

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="93.472pt" height="86.205pt" viewBox="0 0 93.472 86.205" version="1.1">
<g id="surface1">
<path style=" stroke:none;fill-rule:nonzero;fill:rgb(89.99939%,0%,0%);fill-opacity:1;" d="M 76.742188 30.347656 C 76.742188 14.691406 64.050781 2 48.394531 2 C 32.742188 2 20.050781 14.691406 20.050781 30.347656 C 20.050781 46.003906 32.742188 58.695312 48.394531 58.695312 C 64.050781 58.695312 76.742188 46.003906 76.742188 30.347656 Z "/>
<path style=" stroke:none;fill-rule:nonzero;fill:rgb(0%,79.998779%,0%);fill-opacity:1;" d="M 62.011719 55.859375 C 62.011719 40.203125 49.324219 27.511719 33.667969 27.511719 C 18.011719 27.511719 5.320312 40.203125 5.320312 55.859375 C 5.320312 71.515625 18.011719 84.207031 33.667969 84.207031 C 49.324219 84.207031 62.011719 71.515625 62.011719 55.859375 Z "/>
<path style=" stroke:none;fill-rule:nonzero;fill:rgb(0%,0%,89.99939%);fill-opacity:1;" d="M 91.472656 55.859375 C 91.472656 40.203125 78.78125 27.511719 63.125 27.511719 C 47.46875 27.511719 34.777344 40.203125 34.777344 55.859375 C 34.777344 71.515625 47.46875 84.207031 63.125 84.207031 C 78.78125 84.207031 91.472656 71.515625 91.472656 55.859375 Z "/>
</g>
</svg>

> echo out.svg

Install

  • Install pdflatex (comes with your TeX distribution).
  • Install pdf2svg (brew install pdf2svg).
  • Install standalone style (with your TeX package manager).
  • Put the script below in your $PATH.
curl https://gist.github.com/jbenet/9449155/raw/tikz2svg > tikz2svg
chmod +x tikz2svg
sudo mv tikz2svg /usr/bin/tikz2svg