Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
3  
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
2  
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. –  Loop Space 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. –  Loop Space Apr 13 '12 at 11:43
4  
Welcome to TeX.SE. Great first question. –  Peter Grill Sep 15 '12 at 1:03

5 Answers 5

up vote 46 down vote accepted

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}
%\usetikzlibrary{...}% tikz package already loaded by 'tikz' option
\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]
%\usetikzlibrary{...}% tikz package already loaded by 'tikz' option
\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
\input{standalone/standalone.cfg}% Load main 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]
% ...
share|improve this answer
2  
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  
+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. –  stalking is prohibited Mar 9 '13 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.

share|improve this answer
    
This things works for me (with $ htlatex file.tex, not with pdflatex)! (Cf. the same solution suggested in this answer and a not on using htlatex) As I had some problems with installing pdf2svg (I am on OS X), this answer solves my problem whereas the most upvoted/accepted - does not. –  Piotr Migdal Apr 22 at 15:55
    
This generally works, but one needs to be aware of a bug in the tex4ht driver of pgf 3.0.0. See sourceforge.net/p/pgf/bugs/327 There's also a thread on tex.SE about this, but I can't remember where it is. –  Respawned Fluff Aug 12 at 19:56

On linux, you can use pdf2svg (an opensource tool). All glyphs are converted to paths, thus you can't edit your text. But it is the only tool that seems to give good results for images mixing drawings and texts.

In your MWE, to get smooth result, I add the smooth option to plot.

Here is a snapshot of the svg file rendered by Firefox (click on the image to download the SVG file):

enter image description here

Here the two commands used to convert TEX to SVG:

pdflatex file.tex
pdf2svg file.pdf file.svg

Your MWE with my changes:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\begin{document}
\begin{tikzpicture}[domain=-2:2,samples=100,scale=1.0,>=latex]
\tikzset{bgrid/.style={help lines,color=blue!10,very thin}}

\draw[bgrid] (-1.5,-3.5) grid (7.5,3.5);

\draw[<->, color=black] (-1.5,0) -- (7.5,0) node[right] {$x$};
\draw[<->, color=black] (0,-3.5) -- (0,3.5) node[above] {$y$};

\foreach \x/\xtext in {-1,1,2,3,4,5,6,7}
\draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\xtext$};

\foreach \y/\ytext in {-3,-2,-1,1,2,3}
\draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\ytext$};

\draw[thick,color=black,domain=0:7.5,smooth]
plot (\x,{sqrt(\x)}) node[anchor=south] {$y = \sqrt{x}$};
\draw[dashed,color=black,domain=0:7.5,smooth]
plot (\x,{(-1)*(sqrt(\x))}) node[anchor=north] {$y = -\sqrt{x}$};
\draw[thick,color=black,domain=-1.5:5.5,samples=3]
plot (\x,{(\x)-2}) node[anchor=south] {$y = x - 2$};

\filldraw[black] (4,2) circle(2pt) node[anchor=south east] {$(4, 2)$};
\filldraw[red] (1,-1) circle(2pt);
\draw[red] (1.5,-1) node[anchor=west] {$(1, -1)$};

\end{tikzpicture}
\end{document}
share|improve this answer
    
I have tried to implement your suggestion, but this doesn't work for me in MiKTeX in Windows. I can produce a pdf file as usual, but when I use the command pdf2svg file.pdf file.svg I get the following error message: 'pdf2svg' is not recognized as an internal or external command, operable program or batch file. Using the commands htlatex or mzlatex produce html files that do not have the graphics in them, and do not produce svg files. –  Santo D'Agostino Sep 16 '12 at 19:29
    
@SantoD'Agostino: To my knowledge, opensource pdf2svg exists only for linux. –  Paul Gaborit Sep 16 '12 at 21:27
    
Sorry to hear this, Paul, but thanks very much for your time anyway. I now face the question of whether I should transfer my latex operation from my Windows machine to my Linux machine; if I do this (which seems inevitable, the only question being when), then your answer will be relevant, and I will store it for use at that time. Thanks again! –  Santo D'Agostino Sep 16 '12 at 22:51

If I run dvisvgm with option -n and the evaluation of PostScript specials is enabled, I get the expected result:

enter image description here

Since MiKTeX doesn't provide a dvisvgm binary through its repositories, you have to install it manually. Recent builds for MiKTeX are available from the dvisvgm website. Simply extract dvisvgm.exe to the MiKTeX subfolder miktex\bin, or even better, install it in a local texmf tree.

share|improve this answer
    
This doesn't work for me in MiKTeX (Windows). I get a one-line mash up of symbols, and no graph. I placed the dvisvgm.exe in the miktex\bin folder (I'm using MiKTeX 2.8). How do I enable the evaluation of PostScript specials? –  Santo D'Agostino Sep 16 '12 at 19:39
    
In order to enable the processing of PS specials, you have to install a recent version of Ghostscript, and add the directory containing gsdll32.dll to the PATH environment variable. You can check the availability of PS processing with dvisvgm -l. If the ps entry is present, everything should work as expected. –  Martin Sep 17 '12 at 6:09
    
This works! Thank-you very much, Martin! All the best! –  Santo D'Agostino Sep 17 '12 at 13:18
    
You're welcome. I'm glad to hear that you've got it working now. –  Martin Sep 17 '12 at 13:34
    
Hi Martin, I think I spoke a little too soon. The program works if I use the example file, but if I try to use standalone with the following first line <br /><br /> \documentclass[tikz,crop=true,border=0.5cm]{standalone} <br /><br /> then I get an error message <br /><br /> DVI error: no font selected <br /><br /> and no svg file is produced. Any advice? (Using dvisvgm -l shows that the ps entry is present, so that is not the problem.) –  Santo D'Agostino Sep 17 '12 at 19:14

You might want to check out a tool I just wrote: tikz2svg.

Given in.tikz (or stdin):

> 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}

It outputs:

> cat tikz2svg < in.tikz
<?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>

See http://juan.benet.ai/note/2014-03-09/tikz2svg/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.