Show More
@@ -19,14 +19,18 b' Authors:' | |||||
19 |
|
19 | |||
20 | from StringIO import StringIO |
|
20 | from StringIO import StringIO | |
21 | from base64 import encodestring |
|
21 | from base64 import encodestring | |
|
22 | import os | |||
|
23 | import tempfile | |||
|
24 | import shutil | |||
|
25 | import subprocess | |||
22 |
|
26 | |||
23 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
24 | # Tools |
|
28 | # Tools | |
25 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
26 |
|
30 | |||
27 |
|
31 | |||
28 | def latex_to_png(s, encode=False): |
|
32 | def latex_to_png(s, encode=False, backend='mpl'): | |
29 |
"""Render a LaTeX string to PNG |
|
33 | """Render a LaTeX string to PNG. | |
30 |
|
34 | |||
31 | Parameters |
|
35 | Parameters | |
32 | ---------- |
|
36 | ---------- | |
@@ -34,18 +38,71 b' def latex_to_png(s, encode=False):' | |||||
34 | The raw string containing valid inline LaTeX. |
|
38 | The raw string containing valid inline LaTeX. | |
35 | encode : bool, optional |
|
39 | encode : bool, optional | |
36 | Should the PNG data bebase64 encoded to make it JSON'able. |
|
40 | Should the PNG data bebase64 encoded to make it JSON'able. | |
|
41 | backend : {mpl, dvipng} | |||
|
42 | Backend for producing PNG data. | |||
|
43 | ||||
37 | """ |
|
44 | """ | |
|
45 | if backend == 'mpl': | |||
|
46 | f = latex_to_png_mpl | |||
|
47 | elif backend == 'dvipng': | |||
|
48 | f = latex_to_png_dvipng | |||
|
49 | else: | |||
|
50 | raise ValueError('No such backend {0}'.format(backend)) | |||
|
51 | bin_data = f(s) | |||
|
52 | if encode: | |||
|
53 | bin_data = encodestring(bin_data) | |||
|
54 | return bin_data | |||
|
55 | ||||
|
56 | ||||
|
57 | def latex_to_png_mpl(s): | |||
38 | from matplotlib import mathtext |
|
58 | from matplotlib import mathtext | |
39 |
|
59 | |||
40 | mt = mathtext.MathTextParser('bitmap') |
|
60 | mt = mathtext.MathTextParser('bitmap') | |
41 | f = StringIO() |
|
61 | f = StringIO() | |
42 | mt.to_png(f, s, fontsize=12) |
|
62 | mt.to_png(f, s, fontsize=12) | |
43 |
|
|
63 | return f.getvalue() | |
44 | if encode: |
|
64 | ||
45 | bin_data = encodestring(bin_data) |
|
65 | ||
|
66 | def latex_to_png_dvipng(s): | |||
|
67 | try: | |||
|
68 | workdir = tempfile.mkdtemp() | |||
|
69 | tmpfile = os.path.join(workdir, "tmp.tex") | |||
|
70 | dvifile = os.path.join(workdir, "tmp.dvi") | |||
|
71 | outfile = os.path.join(workdir, "tmp.png") | |||
|
72 | ||||
|
73 | with open(tmpfile, "w") as f: | |||
|
74 | f.write(_latex_header) | |||
|
75 | f.write(s) | |||
|
76 | f.write(_latex_footer) | |||
|
77 | ||||
|
78 | subprocess.check_call( | |||
|
79 | ["latex", "-halt-on-errror", tmpfile], cwd=workdir) | |||
|
80 | ||||
|
81 | subprocess.check_call( | |||
|
82 | ["dvipng", "-T", "tight", "-x", "1500", "-z", "9", | |||
|
83 | "-bg", "transparent", "-o", outfile, dvifile], cwd=workdir) | |||
|
84 | ||||
|
85 | with open(outfile) as f: | |||
|
86 | bin_data = f.read() | |||
|
87 | finally: | |||
|
88 | shutil.rmtree(workdir) | |||
46 | return bin_data |
|
89 | return bin_data | |
47 |
|
90 | |||
48 |
|
91 | |||
|
92 | ||||
|
93 | _latex_header = r''' | |||
|
94 | \documentclass{article} | |||
|
95 | \usepackage{amsmath} | |||
|
96 | \usepackage{amsthm} | |||
|
97 | \usepackage{amssymb} | |||
|
98 | \usepackage{bm} | |||
|
99 | \pagestyle{empty} | |||
|
100 | \begin{document} | |||
|
101 | ''' | |||
|
102 | ||||
|
103 | _latex_footer = r'\end{document}' | |||
|
104 | ||||
|
105 | ||||
49 | _data_uri_template_png = """<img src="data:image/png;base64,%s" alt=%s />""" |
|
106 | _data_uri_template_png = """<img src="data:image/png;base64,%s" alt=%s />""" | |
50 |
|
107 | |||
51 | def latex_to_html(s, alt='image'): |
|
108 | def latex_to_html(s, alt='image'): |
General Comments 0
You need to be logged in to leave comments.
Login now