2024-10-01 11:25:46 发布
网友
有没有python代码允许使用latex代码(用于表达式)并将其解析为mathml或svg代码?一个简单的函数将字符串(latex代码)作为参数并输出字符串(svg或mathml代码)将是完美的。在
我发现了这个http://svgkit.sourceforge.net/SVGLaTeX.html,但它是一个基于web的项目,不知道如何使用它。在
编辑:或者用任何语言(不是强制性的python),或者至少是一个可以通过命令行执行的exe文件(不需要安装额外的东西)。在
我的解决方案是使用latex生成DVI文件,然后使用^{}将DVI转换为svg:
latex
latex file.tex
dvisvgm no-fonts file.dvi file.svg
根据我的经验,最终的svg完全按照需要呈现(使用InkScape或QSvgRenderer)。在
我使用的乳胶模板是:
\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}
关于SVGLaTeX:
我想说你可以在你的电脑上使用它作为一个python脚本(非webbased)[edit:not as it is],但它不能满足你的要求“不安装额外的东西”,因为我认为你需要一个latex发行版。在
关于MathML与SVG:
将Latex转换为mathml(我只能找到基于Web的解决方案)与将Latex转换为SVG不同,因为mathml更像是对数学源(如Latex source)的描述,SVG是一种存储排版公式的格式,如PDF。在
从LateX生成SVG比将LateX转换为MathML要复杂得多,前者(据我所知)最终总是使用Knuts-TeX程序。因此,如果你不安装任何LateX[编辑:或远程使用],你就必须转换为MathML。[希望其他人知道一个工具。我不熟悉JavaScript。它能从控制台运行吗?]. 在
编辑:
从LateX生成SVG的Python脚本(沿着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)
您可以在不安装任何东西的情况下执行此操作:
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的文本(试试看)。在
请注意,与任何依赖第三方web应用程序的解决方案一样
这假设您有可靠的internet连接
它的性能取决于您的连接速度和服务器的速度
这依赖于第三方网站保持一致性(如果第三方网站将其删除,或格式发生重大变化,则不进行调整将无法继续工作)
我的解决方案是使用} 将DVI转换为svg:
latex
生成DVI文件,然后使用^{latex file.tex
产生文件.dvi在dvisvgm no-fonts file.dvi file.svg
无字体:仅使用SVG路径根据我的经验,最终的svg完全按照需要呈现(使用InkScape或QSvgRenderer)。在
我使用的乳胶模板是:
关于SVGLaTeX:
我想说你可以在你的电脑上使用它作为一个python脚本(非webbased)[edit:not as it is],但它不能满足你的要求“不安装额外的东西”,因为我认为你需要一个latex发行版。在
关于MathML与SVG:
将Latex转换为mathml(我只能找到基于Web的解决方案)与将Latex转换为SVG不同,因为mathml更像是对数学源(如Latex source)的描述,SVG是一种存储排版公式的格式,如PDF。在
从LateX生成SVG比将LateX转换为MathML要复杂得多,前者(据我所知)最终总是使用Knuts-TeX程序。因此,如果你不安装任何LateX[编辑:或远程使用],你就必须转换为MathML。[希望其他人知道一个工具。我不熟悉JavaScript。它能从控制台运行吗?]. 在
编辑:
从LateX生成SVG的Python脚本(沿着SVGLatex/eqtexsvg的行):
您可以在不安装任何东西的情况下执行此操作:
这个脚本调用您提到的SVGKit服务器,它负责将LaTeX转换为SVG。它返回SVG的文本(试试看)。在
请注意,与任何依赖第三方web应用程序的解决方案一样
这假设您有可靠的internet连接
它的性能取决于您的连接速度和服务器的速度
这依赖于第三方网站保持一致性(如果第三方网站将其删除,或格式发生重大变化,则不进行调整将无法继续工作)
相关问题 更多 >
编程相关推荐