converting a latex code to mathml or svg code in python

半世苍凉 提交于 2019-12-03 15:52:02

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 you mention, which does the work of converting LaTeX to SVG. It returns the text of SVG (try it out).

Note that, as with any solution that relies on third-party web apps,

  1. This assumes you have a reliable internet connection

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

  3. This relies on the third-party site to stay consistent (if they take it down, or the format significantly changes, this will no longer work without adjustment)

About SVGLaTeX:

I would say you can use it as a python script on your computer (non-webbased) [edit: not as it is], but it does not fulfill your requirement 'without installing additional stuff' since I think you'd need a latex distribution.

About MathML vs. SVG:

Converting Latex to mathml (I could find only webbased solutions) is different to converting LateX to SVG, in the sense that mathml is more like a description of the math source like LateX source, and SVG is a format to store the typeset equations, like PDF.

Producing SVG from LateX is a much more involved process than converting LaTeX to MathML, the former (to my knowledge) always ultimately using Knuts TeX program. So if you do not install any LateX [edit: or use it remotely] you'd have to convert to MathML. [Hopefully someone else knows a tool for it. I am not familiar with JavaScript. Can it be run from console?].

Edit:

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

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)

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

  1. latex file.tex # produces file.dvi
  2. dvisvgm --no-fonts file.dvi file.svg # --no-fonts: use SVG paths only

In my experience, the final svg is rendered exactly as wanted (with InkScape, or QSvgRenderer).

The LaTeX template I use is this:

\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}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!