# How can I use TikZ to make standalone graphics?

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?

-
Use the standalone class. See Compile a LaTeX document into a PNG image that's as short as possible which should be easily adjustable for SVG graphics. –  Martin Scharrer Apr 13 '12 at 11:27
You can write an answer with that? I'd like to give the credit for it. –  Henrik Hansen Apr 13 '12 at 11:29
Alternatively, when loaded with the correct driver (put \def\pgfsysdriver{pgfsys-tex4ht.def} before loading tikz) then you can run htlatex on the file to produce SVGs directly. I prefer to run them through scour (a python script for cleaning up SVGs) before posting them on the web. It works well in my experience so long as there isn't too much text. –  Andrew Stacey Apr 13 '12 at 11:41
It's not a great one for how it's organised (the "answer" is in the question) but see tex.stackexchange.com/q/15039/86 for more details. –  Andrew Stacey Apr 13 '12 at 11:43
I will post an answer. However, I don't think you can search and select text inside SVG graphics in your browser. At least Firefox under Linux can't do it. –  Martin Scharrer Apr 13 '12 at 11:51

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 a convert 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:

\documentclass[tikz,convert={outfile=\jobname.svg}]{standalone}
\begin{document}
\begin{tikzpicture}% Example:
\draw (0,0) -- (10,10); % ...
\draw (10,0) -- (0,10); % ...
\node at (5,5) {Lorem ipsum at domine standalonus};
\end{tikzpicture}
\end{document}


Then either you compile the file as usual with pdflatex or another latex and convert the PDF to a SVG manually or compile it with the -shell-escape option and let standalone 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 default standalone uses Image Magick's convert, which can do PDF to SVG but will not always give you good results. The pdf2svg tool seems to be better suited, but isn't supported out-of-the-box by standalone 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 use pdf2svg directly by using the command key of the convert option. Unfortunately, there is a small bug in standalone preventing it. I just fixed that and will upload the new version today.

With this you can write:

\documentclass[crop,tikz,convert={outext=.svg,command=\unexpanded{pdf2svg \infile\space\outfile}},multi=false]{standalone}[2012/04/13]
\makeatletter
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (10,10); % ...
\draw (10,0) -- (0,10); % ...
\draw (5,0) -- (0,10); % ...
\node at (5,5) {Lorem ipsum at domine standalonus};
\end{tikzpicture}
\end{document}


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:

% Local standalone.cfg file
\standaloneconfig{convert={command={pdf2svg \infile\space\outfile}}}


I might add a special pdf2svg key in the next version as well, so you only need to write the following then:

\documentclass[crop,tikz,convert=pdf2svg]{standalone}[2012/04/13]
% ...

-
The new version v1.0b is now in CTAN. Mirrors might need up to 24 hours to sync. TeXLive should have it inside the next few days. –  Martin Scharrer Apr 13 '12 at 13:01
+1 for standalonus! :) –  zeroth Apr 13 '12 at 13:05
Escaping some special characters in the command key seems to be too complicated. That is why I prefer to invoke the conversion outside the standalone. –  Donut E. Knot Mar 9 at 21:33

I use tex4ht and set the PGF output format to SVG. This solution comes from page 110 of the pgfmanual http://mirrors.ctan.org/graphics/pgf/base/doc/generic/pgf/pgfmanual.pdf.

I've only used it to create SVG of a TikZ picture in an otherwise empty \documentclass{article} but it looks like you could use this to make html with SVG graphics of a large document.

The advantage of this approach is that the SVG is produced by PGF and you know you're getting vector graphics. Also you get the result in a single step. It won't do functional shading or matricies and text in the pictures can be a problem but there's more on fixing that in the pgfmanual.

• In the TeX or LaTeX document preamble before you load the TikZ package, e.g. with \usepackage{tikz} in LaTeX type:

\def\pgfsysdriver{pgfsys-tex4ht.def}

• Then process the TeX or LaTeX with httex or htlatex as appropriate. You may need to add tex4ht to your tex installation; it's at http://www.tug.org/applications/tex4ht/. For example to process my file called logoname.tex I do

htlatex logoname.tex

• The following output files are created in the current directory

logoname.html
logoname.css
logoname-1.svg


If you have more TikZ pictures in the document I assume they would become logoname-2.svg and so on.

I'm able to look at the SVG output in firefox and inkscape so it seems to produce good results.

-