SVG LaTeX
Introduction
The goal is to turn LaTeX into SVG. Right now there's a Python CGI script to do this. Here are some issues:
- Conversion must be done on server side through cgi
- Must validate the LaTeX for security reasons to prevent cross-site scripting. This means only a small subset of LaTeX is supported, the same as Texvc, which Wikipedia uses. Texvc also spits back MathML, which is the W3C way to include equations in SVG -- maybe this can be used better in the future.
- Should the SVG be positioned
<text>
elements with embeded fonts?- Eventually, because this is the correct way, but not for now.
- Inkscape and Firefox do not support embeded fonts.
- This is hard to do with the tools available. dvi2svg is the only one that comes close.
- A random equation uses a lot of different fonts: cmsy6, cmsy8, cmsy10, cmr6, cmr8, cmr12, cmmi8, cmmi12, cmex10
- It will bloat the SVG with lots of fonts unless we only include the fonts and characters we need, but then it's not very editable.
- Instead of
<text>
elements, our conversion spits back a bunch of<path>
and<polyline>
elements.
Live Demos
- latex_live demo - Table of tests that you can modify and re-run client side. Unfortunately a round-trip to a server is required, which is slow.
Browse Code in SVN Repository
Server-Side Software Requirements
- latex
- dvips - included in a latex distribution
- plotutils - needed by pstoedit to generate svg output
- pstoedit
Basic Procedure
Inspired by Inkscape's eqtexsvg.py: Say you had a file eqn.tex that looked like
\documentclass{article} \usepackage{amsmath} \usepackage{amssymb} \usepackage{amsfonts} \thispagestyle{empty} \begin{document} \[ 2 \pi f t \] \end{document}
Here are the commands you would run:
latex eqn.tex
- This creates the file eqn.dvi
dvips -q -f -e 0 -E -D 10000 -x 10000 -o eqn.ps eqn.dvi
- the dvips man page
- [-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
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 pollygon conversion, we had to run dvips with a high magnification, so we have to scale down the resulting SVG. The transformation that we'll stick in the svg group:
- scale(0.0017361) goes from the font's point coordinates to pixel coordinates. 1/0.0017361 = 576 = 8*72 = 8 inches worth of points
Additional Links:
- ps to svg through sketch/skencil : Electroncs
- Document Engineering Lab's PostScript to SVG
- long pdf to read: Vector Grphics
- DVI2SVG long pdf report
- dvi2svg in germany
- pydvi2svg