latex equations to svg

Latex is a great tool to typeset equations. However, latex creates dvi (and ps and pdf) files and I would want to have svgs for my html5-based presentations.

There does not seem to be a handy software tool for linux to convert latex coded equations to svg. There are however tools to convert ps and pdf files to svg. I first tried inkscape (using inkscape -l out.svg in.pdf). Unfortunately, inkscape fails to convert some symbols, (eg [\cdot] becomes . The commandline tool pstoedit does a better job: ). However, pstoedit seems to add a background to the equation and does not crop it. I hacked a script together which solves these problems. Download it, or copy paste:


if [ "$3" = '' ]; then
  scale=1;
else
  scale=$3;
fi
echo "\documentclass{article}           "  > formule.tex
echo "\usepackage[latin1]{inputenc}     "  >> formule.tex
echo "\usepackage{amssymb}              "  >> formule.tex
echo "\usepackage{amsmath}              "  >> formule.tex
echo "\usepackage{amsfonts}             "  >> formule.tex
echo "\begin{document} \pagestyle{empty}"  >> formule.tex
echo \\[ $1 \\]                            >> formule.tex
echo "\end{document}"                      >> formule.tex
latex formule.tex
dvips formule.dvi
pstoedit -xscale $scale -yscale $scale -f plot-svg -dt -ssp formule.ps $2
# it seems pstoedit adds a background which is easily removed:
sed -i "/^.*background.*$/d" $2
# pfff, I would want this without inkscape's gui interference...
inkscape --verb=FitCanvasToDrawing --verb=FileSave --verb=FileClose $2
# clean up:
rm formule.aux  formule.dvi  formule.log  formule.ps  formule.tex
  

This function can be called using:


  formulaTosvg.sh "YourFormula" out.svg [scale]
  
Eg:

  formulaTosvg.sh "1 + 1 = 3" wrong.svg