Show More
@@ -65,7 +65,7 b' def print_display_png(o):' | |||||
65 | s = s.strip('$') |
|
65 | s = s.strip('$') | |
66 | # As matplotlib does not support display style, dvipng backend is |
|
66 | # As matplotlib does not support display style, dvipng backend is | |
67 | # used here. |
|
67 | # used here. | |
68 |
png = latex_to_png( |
|
68 | png = latex_to_png(s, backend='dvipng', wrap=True) | |
69 | return png |
|
69 | return png | |
70 |
|
70 | |||
71 |
|
71 |
@@ -31,7 +31,7 b' from IPython.utils.process import find_cmd, FindCmdError' | |||||
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | def latex_to_png(s, encode=False, backend='mpl'): |
|
34 | def latex_to_png(s, encode=False, backend='mpl', wrap=False): | |
35 | """Render a LaTeX string to PNG. |
|
35 | """Render a LaTeX string to PNG. | |
36 |
|
36 | |||
37 | Parameters |
|
37 | Parameters | |
@@ -42,6 +42,8 b" def latex_to_png(s, encode=False, backend='mpl'):" | |||||
42 | Should the PNG data bebase64 encoded to make it JSON'able. |
|
42 | Should the PNG data bebase64 encoded to make it JSON'able. | |
43 | backend : {mpl, dvipng} |
|
43 | backend : {mpl, dvipng} | |
44 | Backend for producing PNG data. |
|
44 | Backend for producing PNG data. | |
|
45 | wrap : bool | |||
|
46 | If true, Automatically wrap `s` as a LaTeX equation. | |||
45 |
|
47 | |||
46 | None is returned when the backend cannot be used. |
|
48 | None is returned when the backend cannot be used. | |
47 |
|
49 | |||
@@ -52,25 +54,27 b" def latex_to_png(s, encode=False, backend='mpl'):" | |||||
52 | f = latex_to_png_dvipng |
|
54 | f = latex_to_png_dvipng | |
53 | else: |
|
55 | else: | |
54 | raise ValueError('No such backend {0}'.format(backend)) |
|
56 | raise ValueError('No such backend {0}'.format(backend)) | |
55 | bin_data = f(s) |
|
57 | bin_data = f(s, wrap) | |
56 | if encode and bin_data: |
|
58 | if encode and bin_data: | |
57 | bin_data = encodestring(bin_data) |
|
59 | bin_data = encodestring(bin_data) | |
58 | return bin_data |
|
60 | return bin_data | |
59 |
|
61 | |||
60 |
|
62 | |||
61 | def latex_to_png_mpl(s): |
|
63 | def latex_to_png_mpl(s, wrap): | |
62 | try: |
|
64 | try: | |
63 | from matplotlib import mathtext |
|
65 | from matplotlib import mathtext | |
64 | except ImportError: |
|
66 | except ImportError: | |
65 | return None |
|
67 | return None | |
66 |
|
68 | |||
|
69 | if wrap: | |||
|
70 | s = '${0}$'.format(s) | |||
67 | mt = mathtext.MathTextParser('bitmap') |
|
71 | mt = mathtext.MathTextParser('bitmap') | |
68 | f = StringIO() |
|
72 | f = StringIO() | |
69 | mt.to_png(f, s, fontsize=12) |
|
73 | mt.to_png(f, s, fontsize=12) | |
70 | return f.getvalue() |
|
74 | return f.getvalue() | |
71 |
|
75 | |||
72 |
|
76 | |||
73 | def latex_to_png_dvipng(s): |
|
77 | def latex_to_png_dvipng(s, wrap): | |
74 | try: |
|
78 | try: | |
75 | find_cmd('latex') |
|
79 | find_cmd('latex') | |
76 | find_cmd('dvipng') |
|
80 | find_cmd('dvipng') | |
@@ -83,9 +87,7 b' def latex_to_png_dvipng(s):' | |||||
83 | outfile = os.path.join(workdir, "tmp.png") |
|
87 | outfile = os.path.join(workdir, "tmp.png") | |
84 |
|
88 | |||
85 | with open(tmpfile, "w") as f: |
|
89 | with open(tmpfile, "w") as f: | |
86 |
f.write( |
|
90 | f.writelines(genelatex(s, wrap)) | |
87 | f.write(s) |
|
|||
88 | f.write(_latex_footer) |
|
|||
89 |
|
91 | |||
90 | subprocess.check_call( |
|
92 | subprocess.check_call( | |
91 | ["latex", "-halt-on-errror", tmpfile], cwd=workdir, |
|
93 | ["latex", "-halt-on-errror", tmpfile], cwd=workdir, | |
@@ -103,17 +105,39 b' def latex_to_png_dvipng(s):' | |||||
103 | return bin_data |
|
105 | return bin_data | |
104 |
|
106 | |||
105 |
|
107 | |||
106 | _latex_header = r''' |
|
108 | def kpsewhich(filename): | |
107 | \documentclass{article} |
|
109 | """Invoke kpsewhich command with an argument `filename`.""" | |
108 | \usepackage{amsmath} |
|
110 | try: | |
109 | \usepackage{amsthm} |
|
111 | find_cmd("kpsewhich") | |
110 | \usepackage{amssymb} |
|
112 | proc = subprocess.Popen( | |
111 | \usepackage{bm} |
|
113 | ["kpsewhich", filename], | |
112 | \pagestyle{empty} |
|
114 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
113 | \begin{document} |
|
115 | (stdout, stderr) = proc.communicate() | |
114 | ''' |
|
116 | return stdout.strip() | |
115 |
|
117 | except FindCmdError: | ||
116 | _latex_footer = r'\end{document}' |
|
118 | pass | |
|
119 | ||||
|
120 | ||||
|
121 | def genelatex(body, wrap): | |||
|
122 | """Generate LaTeX document for dvipng backend.""" | |||
|
123 | breqn = wrap and kpsewhich("breqn.sty") | |||
|
124 | yield r'\documentclass{article}' | |||
|
125 | packages = ['amsmath', 'amsthm', 'amssymb', 'bm'] | |||
|
126 | if breqn: | |||
|
127 | packages.append('breqn') | |||
|
128 | for pack in packages: | |||
|
129 | yield r'\usepackage{{{0}}}'.format(pack) | |||
|
130 | yield r'\pagestyle{empty}' | |||
|
131 | yield r'\begin{document}' | |||
|
132 | if breqn: | |||
|
133 | yield r'\begin{dmath*}' | |||
|
134 | yield body | |||
|
135 | yield r'\end{dmath*}' | |||
|
136 | elif wrap: | |||
|
137 | yield '$${0}$$'.format(body) | |||
|
138 | else: | |||
|
139 | yield body | |||
|
140 | yield r'\end{document}' | |||
117 |
|
141 | |||
118 |
|
142 | |||
119 | _data_uri_template_png = """<img src="data:image/png;base64,%s" alt=%s />""" |
|
143 | _data_uri_template_png = """<img src="data:image/png;base64,%s" alt=%s />""" |
General Comments 0
You need to be logged in to leave comments.
Login now