Convert latex code to mathml or svg code in python

Is there any python code that allows you to take latex code (for equations) and parse it into math or svg code? A simple function that takes a string as an argument (latex code) and outputs a string (svg or mathml code) would be ideal.

PS. I found this http://svgkit.sourceforge.net/SVGLaTeX.html , but this is a web project and don't know how to use it.

EDIT: either in any language (not necessarily python) or at least an exe file that can be simply executed using the command line (without installing additional files).

+6
source share
3 answers

You can do this without installing anything:

import urllib import urllib2 def latex2svg(latexcode): """ Turn LaTeX string to an SVG formatted string using the online SVGKit found at: http://svgkit.sourceforge.net/tests/latex_tests.html """ txdata = urllib.urlencode({"latex": latexcode}) url = "http://svgkit.sourceforge.net/cgi-bin/latex2svg.py" req = urllib2.Request(url, txdata) return urllib2.urlopen(req).read() print latex2svg("2+2=4") print latex2svg("\\frac{1}{2\\pi}") 

This script calls the SVGKit server, which you mention, which does the job of converting LaTeX to SVG. It returns SVG text (try it).

Please note that, like any solution based on third-party web applications,

  • This assumes that you have a reliable Internet connection.

  • Its performance depends on the speed of your connection and server.

  • It depends on the fact that the third-party site will remain constant (if they are removed or the format changes significantly, this will not work without configuration)

+4
source

About SVGLaTeX:

I would say that you can use it as a python script on your computer (not a website) [change: not as it is], but it does not meet your requirement “without installing additional material”, since I think that you 'd need latex distribution.

About MathML vs SVG:

Converting Latex to mathml (I can only find web solutions) is different from converting LateX to SVG in the sense that the math is more like describing a mathematical source, such as a LateX source, and SVG is a format for storing an equation set like PDF.

Getting SVG from LateX is a much more complicated process than converting LaTeX to MathML, the first (as far as I know), always ultimately using the Knuts TeX program. Therefore, if you do not install LateX [edit: or use it remotely], you will have to convert it to MathML. [Hope someone knows a tool for this. I am not familiar with JavaScript. Can I start the console?].

Edit:

Python script to make SVG from LateX (along SVGLatex / eqtexsvg line):

 from subprocess import call import sys, re if not len(sys.argv) == 2: print "usage: tex2svg input_file.tex" exit(1) tex_name = sys.argv[1] svg_name = tex_name[:-4] + ".svg" ps_name = tex_name[:-4] + ".ps" dvi_name = tex_name[:-4] + ".dvi" if call(["latex", tex_name]): exit(1) if call(["dvips", "-q", "-f", "-e", "0", "-E", "-D", "10000", "-x", "1000", "-o", ps_name, dvi_name]): exit(1) if call(["pstoedit", "-f", "plot-svg", "-dt", "-ssp", ps_name, svg_name]): exit(1) 
+2
source

My solution is to use latex to create a DVI file, and then use dvisvgm to convert dvi to svg:

  • latex file.tex # creates a .dvi file
  • dvisvgm --no-fonts file.dvi file.svg # --no-fonts: use only SVG paths

In my experience, the final svg is displayed exactly as you need (with InkScape or QSvgRenderer).

I am using LaTeX template:

 \documentclass[paper=a5,fontsize=12pt]{scrbook} \usepackage[pdftex,active,tightpage]{preview} \usepackage{amsmath} \usepackage{amssymb} \usepackage{amsfonts} \usepackage{tikz} \begin{document} \begin{preview} \begin{tikzpicture}[inner sep=0pt, outer sep=0pt] \node at (0, 0) {texCode}; % <--Put your tex-code here \end{tikzpicture} \end{preview} \end{document} 
+2
source

Source: https://habr.com/ru/post/910100/


All Articles