How to export TikZ graphics as SVG

If you have a TikZ graphic, you can use the LaTeX standalone package to make the page fit the content:

% Minimal TikZ standalone example
\documentclass[tikz, border=1mm]{standalone}

\begin{document}
\begin{tikzpicture}
\draw (0,0) node [] {My text};
\end{tikzpicture}
\end{document}

Assuming you have saved that file as MyDiagram.tex, you can convert it to a PDF and subsequently convert that PDF to a SVG using

pdflatex MyDiagram.tex
pdf2svg MyDiagram.pdf MyDiagram.svg

which will generate this SVG:

Note that the 1mm border around the content is intentional and recommended for most usecases. The background is transparent by default (but has been set to white in HTML on this blogpost to illustrate the extent of the SVG).

You can also use this Makefile template:

%.pdf: %.tex
    pdflatex $<

%.svg: %.pdf
    pdf2svg $< [email protected]

all: MyDiagram.svg

which allows you to automatically run the commands for one or multiple TeX files.