Tell me more ×
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 produce separate .svg files for each of the TikZ diagrams in my LaTeX document. Each of the .svg files will then be used in a WordPress web page. I have tried a number of things, none of which have produced good results:

  • Convert .pdf files to .png using GIMP (after transporting the .pdf files to my linux system); the resulting image quality is poor, very blurry, but otherwise intact, and all fonts were rendered properly

  • Convert .pdf files to .svg using Inkscape; this is a disaster, as the resulting .svg file has the graph labels mashed together, and some of the labels are pushed on top of axes or graph traces

  • Convert .dvi files to .svg using dvisvgm; this is also a disaster, as the resulting .svg file contains an unrecognizable mess

  • Convert .pdf files to .svg using pdf2svg; this was better, as the graphics were very sharp, but the fonts did not convert well (mathematical symbols were not in italics, square-root symbols were mangled)

Is there a way to get .svg files directly from LaTeX/TikZ without having to first produce .pdf files? I am guessing that this will work better than having to produce .pdf files first and then have to convert them.

I compile my LaTeX document using MikTeX via WinEdt (pdflatex) on a Windows XP system.

Update: In response to Speravir, here is one of the examples I've been working with:

\documentclass{article}
\usepackage{tikz}

\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] plot (\x,{sqrt(\x)}) node[anchor=south] {$y = \sqrt{x}$};
\draw[dashed,color=black,domain=0:7.5] plot (\x,{(-1)*(sqrt(\x))}) node[anchor=north] {$y = -\sqrt{x}$};
\draw[thick,color=black,domain=-1.5:5.5] 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 question
4  
Welcome to TeX.SE. Great first question. – Peter Grill Sep 15 '12 at 1:03
Possible Duplicate: LaTeX: outputting a “tight” svg. Please let us know if this answers your question in which case we can close this as a duplicate. Another very related question: Exporting all equations from a document as individual svg files. – Peter Grill Sep 15 '12 at 1:04
Thanks, Peter. I've tried most of the suggestions in the two posts you refer to, without good results, except for the Metapost suggestion in the second post. I shall try this, although I have never used Metapost. – Santo D'Agostino Sep 15 '12 at 1:20
OK, this is strange. The second post you mentioned led me to search and find www.tlhiv.org/MetaPost/tools/mptosvg/ . On this site, it is explained that first MetaPost is converted to pdf, then to svg. So I just used the online converter at the site to convert my pdf file to svg. Although the online site claims to use the same pdf2svg program that I downloaded, for some reason the online converter produces beautiful results whereas my downloaded version gave poor results. This may be a solution for me, but it would still be nice to produce svg directly from tikz so I'll let the question stand. – Santo D'Agostino Sep 15 '12 at 2:00
3  
Possible duplicate: tex.stackexchange.com/q/35145/86 Using tex4ht it is possible to generate SVG directly from TikZ. – Andrew Stacey Sep 15 '12 at 7:56
show 7 more comments

2 Answers

up vote 3 down vote accepted

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
show 2 more comments

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

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.