Skip to content

Instantly share code, notes, and snippets.

@sdiehl
Created November 17, 2010 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdiehl/702882 to your computer and use it in GitHub Desktop.
Save sdiehl/702882 to your computer and use it in GitHub Desktop.
Input Tex Output SVG
#!/usr/bin/env python
"""
eqtexsvg.py
Modified from the script by Julien Vitard <julienvitard@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
import inkex, os, tempfile, sys, xml.dom.minidom, optparse
def create_equation_tex(filename, equation):
tex = open(filename, 'w')
tex.write("""%% processed with eqtexsvg.py
\\documentclass{article}
\\usepackage{amsmath}
\\usepackage{amssymb}
\\usepackage{amsfonts}
\\thispagestyle{empty}
\\begin{document}
""")
tex.write(equation)
tex.write("\n\\end{document}\n")
tex.close()
class EQTEXSVG(inkex.Effect):
def __init__(self, formula):
inkex.Effect.__init__(self)
self.formula = formula
def effect(self):
base_dir = tempfile.mkdtemp("", "inkscape-");
latex_file = os.path.join(base_dir, "eq.tex")
aux_file = os.path.join(base_dir, "eq.aux")
log_file = os.path.join(base_dir, "eq.log")
ps_file = os.path.join(base_dir, "eq.ps")
dvi_file = os.path.join(base_dir, "eq.dvi")
svg_file = os.path.join(base_dir, "eq.svg")
out_file = os.path.join(base_dir, "eq.out")
err_file = os.path.join(base_dir, "eq.err")
def clean():
os.remove(latex_file)
os.remove(aux_file)
os.remove(log_file)
os.remove(ps_file)
os.remove(dvi_file)
os.remove(svg_file)
os.remove(out_file)
if os.path.exists(err_file):
os.remove(err_file)
os.rmdir(base_dir)
create_equation_tex(latex_file, self.formula)
os.system('latex "-output-directory=%s" -halt-on-error "%s" > "%s"' \
% (base_dir, latex_file, out_file))
try:
os.stat(dvi_file)
except OSError:
print >>sys.stderr, "invalid LaTeX input:"
print >>sys.stderr, self.formula
print >>sys.stderr, "temporary files were left in:", base_dir
sys.exit(1)
os.system('dvips -q -f -E -D 600 -y 5000 -o "%s" "%s"' % (ps_file, dvi_file))
# cd to base_dir is necessary, because pstoedit writes
# temporary files to cwd and needs write permissions
separator = ';'
if os.name == 'nt':
separator = '&&'
os.system('cd "%s" %s pstoedit -f plot-svg -dt -ssp "%s" "%s" > "%s" 2> "%s"' \
% (base_dir, separator, ps_file, svg_file, out_file, err_file))
# forward errors to stderr but skip pstoedit header
if os.path.exists(err_file):
err_stream = open(err_file, 'r')
for line in err_stream:
if not line.startswith('pstoedit: version'):
sys.stderr.write(line + '\n')
err_stream.close()
if os.path.exists(svg_file):
output_svg = open(svg_file, 'r')
for line in output_svg:
sys.stdout.write(line + '\n')
err_stream.close()
clean()
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option("-f", "--formula", dest="formula", help='LaTeX formula to render')
(options, args) = parser.parse_args()
e = EQTEXSVG(options.formula)
e.effect()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment