def latex_to_png(s, encode=False):
    from matplotlib import mathtext
    
    mt = mathtext.MathTextParser('bitmap')
    f = StringIO()
    mt.to_png(f, s, fontsize=12)
    bin_data = f.getvalue()
    if encode:
        bin_data = encodestring(bin_data)
    return bin_data
Render a LaTeX string to PNG using matplotlib.mathtext.
dataset/ETHPy150Open ipython/ipython-py3k/IPython/lib/latextools.py/latex_to_png