Converting latex code to Images (or other displayb

2019-01-18 20:20发布

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?

标签: python latex
7条回答
We Are One
2楼-- · 2019-01-18 20:56

Maybe you could use an online service such as this one: http://www.codecogs.com/components/equationeditor/equationeditor.php.


Following Joel A. Christophel's suggestion, here's a working similar website: http://arachnoid.com/latex/

查看更多
仙女界的扛把子
3楼-- · 2019-01-18 20:58

SymPy has a builtin preview function that does this.

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

generates

enter image description here

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)

enter image description here

查看更多
Emotional °昔
4楼-- · 2019-01-18 21:01

Take a look at mathtex.

查看更多
\"骚年 ilove
5楼-- · 2019-01-18 21:08

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.

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:

#!/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

查看更多
等我变得足够好
6楼-- · 2019-01-18 21:11

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.

pip install matplotlib pillow
查看更多
劳资没心,怎么记你
7楼-- · 2019-01-18 21:16

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.

查看更多
登录 后发表回答