# Generating SVG from LaTeX (tikz)?

How can I generate an SVG image from a LaTeX file - more precisely, a tikz diagram? I'm working in Windows.

The simplest way to do so would be to first generate a PDF or DVI with just the said diagram/equation and then convert it to a vector format.

If small page-size PDFs work fine for you, then you can use * Use ghostscript:

    gswin32c.exe -sDEVICE=pdfwrite -dMaxSubsetPct=100 -dPDFSETTINGS=/ebook -sOutputFile="outfile.pdf" -dNOPAUSE -dBATCH "infile.pdf"
where -dPDFSETTINGS is used to define how images are resampled. Using -dPDFSETTINGS=/screen will yield dpi=72. I just tried this with a sample pdf and it seems to work fine.


Otherwise, use any of the methods described at: https://stackoverflow.com/questions/10288065/convert-pdf-to-clean-svg

Also, this answer for multiple pages: https://stackoverflow.com/a/4502030

This resource is useful as well: svgkit.sourceforge.net/SVGLaTeX.html

Of course, some of the mentioned commands would work fine with using MinGW implementations or will have native Windows implementations, which you'll have to download. And of course, then add the executable file's directory to the PATH variable environment (available in the advanced settings in the computer properties). For inkscape, see this link: http://kaioa.com/node/42#inkcl_bat_usage Here,you just place a batch file (InkCL.bat)in the inkscape directory. and use it instead of typing inkscape in the command-line. inkcl

To sum it up, it is generally requires just 3 commands

1. # Create a TeX file with an empty page style

Say, input.tex:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}

\thispagestyle{empty}
\begin{document}
$2 \pi f t$
\end{document}

2. Now, Use any of the following methods:

• ## After Creating PDF:

 pdflatex input.tex


This creates the file input.pdf Now, Use any of the following methods:

• from Inkscape:

 inkscape -l output.svg input.pdf

• (Same as above):

inkscape \
--without-gui \
--file=input.pdf \
--export-plain-svg=output.svg

• For multiple pages (and single as well), use pdf2svg[cityinthesky.co.uk/opensource/pdf2svg/]. It uses poppler and Cairo to convert a pdf into SVG. This is particularly useful for bulk conversion:

pdf2svg <input.pdf> <output.svg> [<pdf page no. or "all" >]


Note: if you specify all the pages you must give a filename with %d in it (which will automatically be replaced by the appropriate page number). E.g.

pdf2svg input.pdf output_page%d.svg all

• ## Fom DVI,

latex input.tex


This creates the file output.dvi

• Then,

dvisvgm --no-fonts input.dvi output.svg


This works perfectly for instances with LaTeX formulas (with option --no-fonts)

• Also soandos' answer (Including for comprehensiveness):

dvips -q -f -e 0 -E -D 10000 -x 10000 -o eqn.ps eqn.dvi


From the dvips man page (ma.utexas.edu/cgi-bin/man-cgi?dvips%201):

• [-q] Run in quiet mode.
• [-f] (filter) Read the .dvi file from standard input and write the PostScript to standard output.
• [-D] sets the resolution between 10 and 10000 (also affects positioning of letters)
• [-E] makes dvips attempt to generate an EPSF file with a tight bounding box.
• [-e num] Each character is placed at most this many pixels from its true' resolution-independent position
• [-R] Run in secure mode.
• [-x] num Set the magnification ratio to num/1000 no matter what the dvi file says (between 10 and 100000)
• [-y] num Set the magnification ratio to num/1000 times the magnification specified in the .dvi file (between 10 and 100000)
• [-o] specifies the output file as eqn.ps

Then,

pstoedit -f plot-svg -dt -ssp eqn.ps eqn.svg


This creates the file eqn.svg

• [-f] specifies the format as plot-svg (direct svg does not work)
• [-dt] Draw text - Text is drawn as polygons.
• [-adt] Automatic Draw text - This option turns on the -dt option selectively for fonts that seem to be no normal text fonts, e.g. Symbol.
• [-ssp] simulate sub path for backends don't support PostScript pathes containing sub pathes, i.e. pathes with intermediate movetos.
• [-usebbfrominput] If specified, pstoedit uses the BoundingBox as is (hopefully) found in the input file instead of one that is calculated by its own. (Doesn't seem to make a difference for plot-svg)
• [ -noclip ] don't use clipping (relevant only if backend supports clipping at all)

In order to get good quality text to polygon conversion, we had to run dvips with a high magnification, so one will have to scale down the resulting SVG.

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"?>
<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>


Try this python CGI script.

• doesn't work for me – IttayD Jul 26 '11 at 18:08
• Why not? (filler) – soandos Jul 26 '11 at 18:38

One method is (assuming you have a working LaTeX installation) to compile the LaTeX diagram to produce a pdf output file (usually using pdflatex).

This can then be opened using the vector graphics editor Inkscape. Inkscape can import a range of files, including pdf (using the poppler libraries) and can save them as SVG files. (Inkscape also provides command-line conversion capabilities but I don't know whether this is available on a Microsoft Windows system).

I recommend the process mentioned by @Rohan. Create a dosbatchfile first, "miktex_path.bat" as:

@echo off
path=C:\Program Files\MiKTeX 2.9\miktex\bin\x64;%path%;
cmd
@echo on


Then edit a .tex file with some equation with your favourite editor like Texmaker or Notepad2. Save it like Sample.tex. Double-click the batch and on the console

do as follows:

latex Sample.tex
dvisvgm --no-fonts Sample.dvi Sample.svg


The job is done.

Here is a collection of some equation contained in one page:

\documentclass[16pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

\usepackage[spanish]{babel}

\usepackage{color}
\usepackage[T1]{fontenc}

\begin{document}

$\psi (t)=\hat{\psi}e^{i(\omega t\, \pm\, \theta)}\sum_i \hat{\psi_i} cos(\alpha_i \pm \omega t)$

$\sum_i \hat{\psi_i} cos(\alpha_i \pm \omega t)$
$\sqrt[{\Delta+n}] {\hat {\mu}}$
$\Omega t$
$\omega Q_{1}$
$\frac{\delta (x^2)}{\delta x}$
$\frac{\delta}{\delta x} (x^2)$
$\int \hat{i}(x^2+3xi)dx$

\begin{huge}
$\psi (t)=\hat{\psi}e^{i(\omega t\, \pm\, \theta)}\sum_i \hat{\psi_i} cos(\alpha_i \pm \omega t)$

$\sum_i \hat{\psi_i} cos(\alpha_i \pm \omega t)$
$\sqrt[{\Delta+n}] {\hat {\mu}}$
$\Omega t$
$\omega Q_{1}$
$\frac{\delta (x^2)}{\delta x}$
$\frac{\delta}{\delta x} (x^2)$
$\int \hat{i}(x^2+3xi)dx$
\end{huge}

\end{document}
`
• This worked for me! – bloudraak Sep 13 '16 at 6:09

I'm using Linux but I think this will work on any platform with TeX and TikZ/PGF. I've only used it to create SVG of a TikZ picture in an otherwise empty document but it looks like you could use this to make html with SVG graphics of a large document. This solution comes from page 110 of 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 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