我想将方程渲染为 PNG 文件并将它们嵌入到我的库的 HTML 文档中。我已经在其他项目中使用 pylab (matplotlib)。
我还没有找到任何线索http://matplotlib.sourceforge.net/users/usetex.html http://matplotlib.sourceforge.net/users/usetex.html and http://matplotlib.sourceforge.net/users/mathtext.html http://matplotlib.sourceforge.net/users/mathtext.html
当我做
plt.title(r'$\alpha > \beta$')
plt.show()
我得到一个带有轴的标题空图形。
Update:
经过一些研究后,我发现将 LaTeX 渲染为 png 的最简单方法是使用 mathext (http://code.google.com/p/mathtex/ http://code.google.com/p/mathtex/ ).
令人惊讶的是,我拥有从源代码构建它所需的所有库。
不管怎样,谢谢大家的回复。
更新2:
我对 mathtex 做了一些测试,发现它不支持矩阵 (\begin{pmatrix}) 和我需要的其他一些东西。
所以,我要安装 LaTex (MikTeX)。
更新3:
我安装了proTeXt。它很大,但易于使用且速度快。恕我直言,目前这是渲染方程的唯一方法。
这对我有用:
# https://gist.github.com/tonyseek/95c90638cf43a87e723b
from cStringIO import StringIO
import matplotlib.pyplot as plt
def render_latex(formula, fontsize=12, dpi=300, format_='svg'):
"""Renders LaTeX formula into image.
"""
fig = plt.figure(figsize=(0.01, 0.01))
fig.text(0, 0, u'${}$'.format(formula), fontsize=fontsize)
buffer_ = StringIO()
fig.savefig(buffer_, dpi=dpi, transparent=True, format=format_, bbox_inches='tight', pad_inches=0.0)
plt.close(fig)
return buffer_.getvalue()
if __name__ == '__main__':
image_bytes = render_latex(
r'\theta=\theta+C(1+\theta-\beta)\sqrt{1-\theta}succ_mul',
fontsize=10, dpi=200, format_='png')
with open('formula.png', 'wb') as image_file:
image_file.write(image_bytes)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)