| 108 | |
| 109 | |
| 110 | def latex_to_png_mpl(s, wrap, color='Black', scale=1.0): |
| 111 | try: |
| 112 | from matplotlib import mathtext |
| 113 | from pyparsing import ParseFatalException |
| 114 | except ImportError: |
| 115 | return None |
| 116 | |
| 117 | # mpl mathtext doesn't support display math, force inline |
| 118 | s = s.replace('$$', '$') |
| 119 | if wrap: |
| 120 | s = u'${0}$'.format(s) |
| 121 | |
| 122 | try: |
| 123 | mt = mathtext.MathTextParser('bitmap') |
| 124 | f = BytesIO() |
| 125 | dpi = 120*scale |
| 126 | mt.to_png(f, s, fontsize=12, dpi=dpi, color=color) |
| 127 | return f.getvalue() |
| 128 | except (ValueError, RuntimeError, ParseFatalException): |
| 129 | return None |
| 130 | |
| 131 | |
| 132 | def latex_to_png_dvipng(s, wrap, color='Black', scale=1.0): |