在python中将乳胶代码转换为mathml或svg代码 [英] converting a latex code to mathml or svg code in python

查看:144
本文介绍了在python中将乳胶代码转换为mathml或svg代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何允许带乳胶代码(用于方程式)并将其解析为mathml或svg代码的python代码?一个简单的函数以字符串(乳胶代码)作为参数并输出字符串(svg或mathml代码)将是完美的.

Is there any python code that allow take a latex code (for equations) and parse it to mathml or svg code ? A simple function that take as argument a string (the latex code) and output a string (the svg or mathml code) would be perfect.

PS.我已经找到了 http://svgkit.sourceforge.net/SVGLaTeX.html ,但这是一个基于网络的项目,不确定如何使用.

PS. I've found this http://svgkit.sourceforge.net/SVGLaTeX.html but it's a web based project and not sure how to use it.

或使用任何语言(不是强制性的python),或者至少是一个可以通过命令行简单地执行该操作的exe文件(无需安装其他内容).

or in any language (not obligatory python), or at least an exe file that can be simply executed by a command line to do that (without installing additional stuff).

推荐答案

您可以执行以下操作而无需安装任何东西:

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}")

此脚本调用您提到的SVGKit服务器,该服务器完成将LaTeX转换为SVG的工作.它返回SVG的文本(尝试一下).

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. 这假定您具有可靠的Internet连接

  1. This assumes you have a reliable internet connection

它的性能取决于您的连接速度和服务器的速度

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

这依赖于第三方网站保持一致(如果他们删除了该网站,或者格式发生了重大变化,那么在没有调整的情况下将不再起作用)

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)

这篇关于在python中将乳胶代码转换为mathml或svg代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆