使用 Python 将乳胶代码转换为图像(或其他可显示格式)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1381741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting latex code to Images (or other displayble format) with Python
提问by Mark Roddy
I have a function I am consuming that returns a string of latex code. I need to generate an image from this. Most of the methods I have seen for doing so suggest calling an external application via say the subprocessmodule which will generate the image for me.
我有一个正在使用的函数,它返回一串乳胶代码。我需要从中生成一个图像。我见过的大多数方法都建议通过子进程模块调用外部应用程序,该模块将为我生成图像。
However, management is not keen on this as it will require external users to install additional software in addition to our own which, with our user base, is not something we can assume to be a simple task.
然而,管理层并不热衷于此,因为除了我们自己的软件之外,它还需要外部用户安装额外的软件,对于我们的用户群,我们认为这不是一项简单的任务。
So are there any python libraries that will accomplish the task of taking latex into a format (such as an image file) which is displayable in a GUI?
那么是否有任何 python 库可以完成将乳胶转换为可在 GUI 中显示的格式(例如图像文件)的任务?
回答by asmeurer
SymPy has a builtin preview function that does this.
SymPy 具有执行此操作的内置预览功能。
expr = sin(sqrt(x**2 + 20)) + 1
preview(expr, viewer='file', filename='output.png')
generates
产生
There are lots of optionsto preview
to change the format of the output (for instance, if you don't like the Euler font you can set euler=False
).
有很多选项可以preview
更改输出的格式(例如,如果您不喜欢 Euler 字体,您可以设置euler=False
)。
preview
also accepts a LaTeX string instead of a SymPy expression if you have that
preview
如果你有,也接受 LaTeX 字符串而不是 SymPy 表达式
preview(r'$$\int_0^1 e^x\,dx$$', viewer='file', filename='test.png', euler=False)
回答by mulllhausen
this answer might not have been available at the time when the question was asked, but i will add it for those seeking a solution as of 2015.
这个答案在提出问题时可能还没有,但我会为那些在 2015 年寻求解决方案的人添加它。
you can use matplotlib.pyplot
to render an equation in a graph with axes, and then remove the axes manually. you can also generate the latex with sympy
:
您可以使用matplotlib.pyplot
轴在图形中渲染方程,然后手动删除轴。您还可以使用以下方法生成乳胶sympy
:
#!/usr/bin/env python2.7
import matplotlib.pyplot as plt
import sympy
x = sympy.symbols('x')
y = 1 + sympy.sin(sympy.sqrt(x**2 + 20))
lat = sympy.latex(y)
#add text
plt.text(0, 0.6, r"$%s$" % lat, fontsize = 50)
#hide axes
fig = plt.gca()
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
plt.draw() #or savefig
plt.show()
tested with sympy 0.7.6
and matplotlib 1.4.3
用 sympy0.7.6
和 matplotlib测试1.4.3
回答by Slabko
This is a bit ugly solution I often use, but I found it easiest to use in many cases.
这是我经常使用的一个有点丑陋的解决方案,但我发现在许多情况下它最容易使用。
import matplotlib.pyplot as plt
import io
from PIL import Image, ImageChops
white = (255, 255, 255, 255)
def latex_to_img(tex):
buf = io.BytesIO()
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.axis('off')
plt.text(0.05, 0.5, f'${tex}$', size=40)
plt.savefig(buf, format='png')
plt.close()
im = Image.open(buf)
bg = Image.new(im.mode, im.size, white)
diff = ImageChops.difference(im, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)
bbox = diff.getbbox()
return im.crop(bbox)
latex_to_img(r'\frac{x}{y^2}').save('img.png')
Keep in mind, it requires pillow and matplotlib.
请记住,它需要枕头和 matplotlib。
pip install matplotlib pillow
回答by D'Nabre
You're going to need to use LaTeX to process to string. The process of rendering LaTex/TeX is very involved (it generally takes a 100+MB package to do the work), you're just not going to be able toss in a little python module to get the work done.
您将需要使用 LaTeX 来处理字符串。渲染 LaTex/TeX 的过程非常复杂(通常需要 100+MB 的包来完成这项工作),你只是无法投入一个小的 python 模块来完成工作。
回答by Bastien Léonard
Maybe you could use an online service such as this one: http://www.codecogs.com/components/equationeditor/equationeditor.php.
也许您可以使用这样的在线服务:http: //www.codecogs.com/components/equationeditor/equationeditor.php。
Following Joel A. Christophel's suggestion, here's a working similar website: http://arachnoid.com/latex/
遵循 Joel A. Christophel 的建议,这里有一个类似的网站:http: //arachnoid.com/latex/