I would like to use TikZ in other settings than TeX and I'd especially like to use the drawings on websites where the text should be searchable and selectable. However, I'm unsure of the best way to convert my drawings to SVG.
How do I best take some TikZ code, render the drawing and turn the output into SVG?
Best Answer
You can use the
standalone
class to produce tight PDF files for one or multiple TikZ pictures. I originally wrote it to simplify the creation of the many pictures of my thesis. Since v1.0 it includes aconvert
option which can convert the produced PDF into a graphics file automatically (using external software, which requires the-shell-escape
compiler option).This is very similar to Compile a LaTeX document into a PNG image that's as short as possible, but SVG needs some extra care.
You can write your TikZ pictures the following way:
Then either you compile the file as usual with
pdflatex
or anotherlatex
and convert the PDF to a SVG manually or compile it with the-shell-escape
option and letstandalone
convert it for you.Manual conversion can be done with a number of tools. It is simpler under Linux, because these tools are easily available there, but should be possible under Windows as well. (The
convert
options isn't really tested under Windows, btw.) By defaultstandalone
uses Image Magick'sconvert
, which can do PDF to SVG but will not always give you good results. Thepdf2svg
tool seems to be better suited, but isn't supported out-of-the-box bystandalone
yet. It can of course be used manually as shown in Exporting all equations from a document as individual svg files.You can configure
standalone
to usepdf2svg
directly by using thecommand
key of theconvert
option. Unfortunately, there is a small bug instandalone
preventing it. I just fixed that and will upload the new version today.With this you can write:
The
\unexpanded
is required because LaTeX expands class options. You can also add\noexpand
before every macro instead.If you need this more often you can also use a
standalone.cfg
file which enables this for all (local)standalone
files. Simply create this file as follows in the same directory:I might add a special
pdf2svg
key in the next version as well, so you only need to write the following then: