使用 Python 将乳胶代码转换为图像(或其他可显示格式) [英] Converting latex code to Images (or other displayble format) with Python

查看:14
本文介绍了使用 Python 将乳胶代码转换为图像(或其他可显示格式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在使用的函数,它返回一串乳胶代码.我需要从中生成图像.我见过的大多数这样做的方法都建议通过 subprocess 模块调用外部应用程序,该模块将为我生成图像.

但是,管理层对此并不热衷,因为除了我们自己的软件之外,它还需要外部用户安装额外的软件,就我们的用户群而言,我们认为这不是一项简单的任务.

那么是否有任何 python 库可以完成将乳胶转换为可在 GUI 中显示的格式(例如图像文件)的任务?

解决方案

SymPy 有一个内置的预览功能可以做到这一点.

expr = sin(sqrt(x**2 + 20)) + 1预览(expr,查看器='文件',文件名='output.png')

生成

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 subprocess module 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?

解决方案

SymPy has a builtin preview function that does this.

expr = sin(sqrt(x**2 + 20)) + 1
preview(expr, viewer='file', filename='output.png')

generates

There are lots of options to preview to change the format of the output (for instance, if you don't like the Euler font you can set euler=False).

preview also accepts a LaTeX string instead of a SymPy expression if you have that

preview(r'$$int_0^1 e^x,dx$$', viewer='file', filename='test.png', euler=False)

这篇关于使用 Python 将乳胶代码转换为图像(或其他可显示格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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