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.
Solution 1:
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.
- PDF tools : http://www.pdfill.com/pdf_tools_free.html
- pdfcrop (I suppose there it will be available with an implementation of MinGW)
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
-
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}
-
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.
-
-
Solution 2:
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/
Solution 3:
Try this python CGI script.
Solution 4:
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).