##// END OF EJS Templates
Latextools: Make latex_to_png_mpl not fail on errors...
Carlos Cordoba -
Show More
@@ -1,199 +1,204 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Tools for handling LaTeX."""
2 """Tools for handling LaTeX."""
3
3
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 from io import BytesIO, open
7 from io import BytesIO, open
8 import os
8 import os
9 import tempfile
9 import tempfile
10 import shutil
10 import shutil
11 import subprocess
11 import subprocess
12
12
13 from IPython.utils.process import find_cmd, FindCmdError
13 from IPython.utils.process import find_cmd, FindCmdError
14 from traitlets.config import get_config
14 from traitlets.config import get_config
15 from traitlets.config.configurable import SingletonConfigurable
15 from traitlets.config.configurable import SingletonConfigurable
16 from traitlets import List, Bool, Unicode
16 from traitlets import List, Bool, Unicode
17 from IPython.utils.py3compat import cast_unicode, cast_unicode_py2 as u, PY3
17 from IPython.utils.py3compat import cast_unicode, cast_unicode_py2 as u, PY3
18
18
19 try: # Py3
19 try: # Py3
20 from base64 import encodebytes
20 from base64 import encodebytes
21 except ImportError: # Py2
21 except ImportError: # Py2
22 from base64 import encodestring as encodebytes
22 from base64 import encodestring as encodebytes
23
23
24
24
25 class LaTeXTool(SingletonConfigurable):
25 class LaTeXTool(SingletonConfigurable):
26 """An object to store configuration of the LaTeX tool."""
26 """An object to store configuration of the LaTeX tool."""
27 def _config_default(self):
27 def _config_default(self):
28 return get_config()
28 return get_config()
29
29
30 backends = List(
30 backends = List(
31 Unicode(), ["matplotlib", "dvipng"],
31 Unicode(), ["matplotlib", "dvipng"],
32 help="Preferred backend to draw LaTeX math equations. "
32 help="Preferred backend to draw LaTeX math equations. "
33 "Backends in the list are checked one by one and the first "
33 "Backends in the list are checked one by one and the first "
34 "usable one is used. Note that `matplotlib` backend "
34 "usable one is used. Note that `matplotlib` backend "
35 "is usable only for inline style equations. To draw "
35 "is usable only for inline style equations. To draw "
36 "display style equations, `dvipng` backend must be specified. ",
36 "display style equations, `dvipng` backend must be specified. ",
37 # It is a List instead of Enum, to make configuration more
37 # It is a List instead of Enum, to make configuration more
38 # flexible. For example, to use matplotlib mainly but dvipng
38 # flexible. For example, to use matplotlib mainly but dvipng
39 # for display style, the default ["matplotlib", "dvipng"] can
39 # for display style, the default ["matplotlib", "dvipng"] can
40 # be used. To NOT use dvipng so that other repr such as
40 # be used. To NOT use dvipng so that other repr such as
41 # unicode pretty printing is used, you can use ["matplotlib"].
41 # unicode pretty printing is used, you can use ["matplotlib"].
42 config=True)
42 config=True)
43
43
44 use_breqn = Bool(
44 use_breqn = Bool(
45 True,
45 True,
46 help="Use breqn.sty to automatically break long equations. "
46 help="Use breqn.sty to automatically break long equations. "
47 "This configuration takes effect only for dvipng backend.",
47 "This configuration takes effect only for dvipng backend.",
48 config=True)
48 config=True)
49
49
50 packages = List(
50 packages = List(
51 ['amsmath', 'amsthm', 'amssymb', 'bm'],
51 ['amsmath', 'amsthm', 'amssymb', 'bm'],
52 help="A list of packages to use for dvipng backend. "
52 help="A list of packages to use for dvipng backend. "
53 "'breqn' will be automatically appended when use_breqn=True.",
53 "'breqn' will be automatically appended when use_breqn=True.",
54 config=True)
54 config=True)
55
55
56 preamble = Unicode(
56 preamble = Unicode(
57 help="Additional preamble to use when generating LaTeX source "
57 help="Additional preamble to use when generating LaTeX source "
58 "for dvipng backend.",
58 "for dvipng backend.",
59 config=True)
59 config=True)
60
60
61
61
62 def latex_to_png(s, encode=False, backend=None, wrap=False):
62 def latex_to_png(s, encode=False, backend=None, wrap=False):
63 """Render a LaTeX string to PNG.
63 """Render a LaTeX string to PNG.
64
64
65 Parameters
65 Parameters
66 ----------
66 ----------
67 s : text
67 s : text
68 The raw string containing valid inline LaTeX.
68 The raw string containing valid inline LaTeX.
69 encode : bool, optional
69 encode : bool, optional
70 Should the PNG data base64 encoded to make it JSON'able.
70 Should the PNG data base64 encoded to make it JSON'able.
71 backend : {matplotlib, dvipng}
71 backend : {matplotlib, dvipng}
72 Backend for producing PNG data.
72 Backend for producing PNG data.
73 wrap : bool
73 wrap : bool
74 If true, Automatically wrap `s` as a LaTeX equation.
74 If true, Automatically wrap `s` as a LaTeX equation.
75
75
76 None is returned when the backend cannot be used.
76 None is returned when the backend cannot be used.
77
77
78 """
78 """
79 s = cast_unicode(s)
79 s = cast_unicode(s)
80 allowed_backends = LaTeXTool.instance().backends
80 allowed_backends = LaTeXTool.instance().backends
81 if backend is None:
81 if backend is None:
82 backend = allowed_backends[0]
82 backend = allowed_backends[0]
83 if backend not in allowed_backends:
83 if backend not in allowed_backends:
84 return None
84 return None
85 if backend == 'matplotlib':
85 if backend == 'matplotlib':
86 f = latex_to_png_mpl
86 f = latex_to_png_mpl
87 elif backend == 'dvipng':
87 elif backend == 'dvipng':
88 f = latex_to_png_dvipng
88 f = latex_to_png_dvipng
89 else:
89 else:
90 raise ValueError('No such backend {0}'.format(backend))
90 raise ValueError('No such backend {0}'.format(backend))
91 bin_data = f(s, wrap)
91 bin_data = f(s, wrap)
92 if encode and bin_data:
92 if encode and bin_data:
93 bin_data = encodebytes(bin_data)
93 bin_data = encodebytes(bin_data)
94 return bin_data
94 return bin_data
95
95
96
96
97 def latex_to_png_mpl(s, wrap):
97 def latex_to_png_mpl(s, wrap):
98 try:
98 try:
99 from matplotlib import mathtext
99 from matplotlib import mathtext
100 except ImportError:
100 except ImportError:
101 return None
101 return None
102
102
103 # mpl mathtext doesn't support display math, force inline
103 # mpl mathtext doesn't support display math, force inline
104 s = s.replace('$$', '$')
104 s = s.replace('$$', '$')
105 if wrap:
105 if wrap:
106 s = u'${0}$'.format(s)
106 s = u'${0}$'.format(s)
107
107
108 mt = mathtext.MathTextParser('bitmap')
108 try:
109 f = BytesIO()
109 mt = mathtext.MathTextParser('bitmap')
110 mt.to_png(f, s, fontsize=12)
110 f = BytesIO()
111 return f.getvalue()
111 mt.to_png(f, s, fontsize=12)
112 return f.getvalue()
113 except:
114 return None
112
115
113
116
114 def latex_to_png_dvipng(s, wrap):
117 def latex_to_png_dvipng(s, wrap):
115 try:
118 try:
116 find_cmd('latex')
119 find_cmd('latex')
117 find_cmd('dvipng')
120 find_cmd('dvipng')
118 except FindCmdError:
121 except FindCmdError:
119 return None
122 return None
120 try:
123 try:
121 workdir = tempfile.mkdtemp()
124 workdir = tempfile.mkdtemp()
122 tmpfile = os.path.join(workdir, "tmp.tex")
125 tmpfile = os.path.join(workdir, "tmp.tex")
123 dvifile = os.path.join(workdir, "tmp.dvi")
126 dvifile = os.path.join(workdir, "tmp.dvi")
124 outfile = os.path.join(workdir, "tmp.png")
127 outfile = os.path.join(workdir, "tmp.png")
125
128
126 with open(tmpfile, "w", encoding='utf8') as f:
129 with open(tmpfile, "w", encoding='utf8') as f:
127 f.writelines(genelatex(s, wrap))
130 f.writelines(genelatex(s, wrap))
128
131
129 with open(os.devnull, 'wb') as devnull:
132 with open(os.devnull, 'wb') as devnull:
130 subprocess.check_call(
133 subprocess.check_call(
131 ["latex", "-halt-on-error", "-interaction", "batchmode", tmpfile],
134 ["latex", "-halt-on-error", "-interaction", "batchmode", tmpfile],
132 cwd=workdir, stdout=devnull, stderr=devnull)
135 cwd=workdir, stdout=devnull, stderr=devnull)
133
136
134 subprocess.check_call(
137 subprocess.check_call(
135 ["dvipng", "-T", "tight", "-x", "1500", "-z", "9",
138 ["dvipng", "-T", "tight", "-x", "1500", "-z", "9",
136 "-bg", "transparent", "-o", outfile, dvifile], cwd=workdir,
139 "-bg", "transparent", "-o", outfile, dvifile], cwd=workdir,
137 stdout=devnull, stderr=devnull)
140 stdout=devnull, stderr=devnull)
138
141
139 with open(outfile, "rb") as f:
142 with open(outfile, "rb") as f:
140 return f.read()
143 return f.read()
144 except:
145 return None
141 finally:
146 finally:
142 shutil.rmtree(workdir)
147 shutil.rmtree(workdir)
143
148
144
149
145 def kpsewhich(filename):
150 def kpsewhich(filename):
146 """Invoke kpsewhich command with an argument `filename`."""
151 """Invoke kpsewhich command with an argument `filename`."""
147 try:
152 try:
148 find_cmd("kpsewhich")
153 find_cmd("kpsewhich")
149 proc = subprocess.Popen(
154 proc = subprocess.Popen(
150 ["kpsewhich", filename],
155 ["kpsewhich", filename],
151 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
156 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
152 (stdout, stderr) = proc.communicate()
157 (stdout, stderr) = proc.communicate()
153 return stdout.strip().decode('utf8', 'replace')
158 return stdout.strip().decode('utf8', 'replace')
154 except FindCmdError:
159 except FindCmdError:
155 pass
160 pass
156
161
157
162
158 def genelatex(body, wrap):
163 def genelatex(body, wrap):
159 """Generate LaTeX document for dvipng backend."""
164 """Generate LaTeX document for dvipng backend."""
160 lt = LaTeXTool.instance()
165 lt = LaTeXTool.instance()
161 breqn = wrap and lt.use_breqn and kpsewhich("breqn.sty")
166 breqn = wrap and lt.use_breqn and kpsewhich("breqn.sty")
162 yield u(r'\documentclass{article}')
167 yield u(r'\documentclass{article}')
163 packages = lt.packages
168 packages = lt.packages
164 if breqn:
169 if breqn:
165 packages = packages + ['breqn']
170 packages = packages + ['breqn']
166 for pack in packages:
171 for pack in packages:
167 yield u(r'\usepackage{{{0}}}'.format(pack))
172 yield u(r'\usepackage{{{0}}}'.format(pack))
168 yield u(r'\pagestyle{empty}')
173 yield u(r'\pagestyle{empty}')
169 if lt.preamble:
174 if lt.preamble:
170 yield lt.preamble
175 yield lt.preamble
171 yield u(r'\begin{document}')
176 yield u(r'\begin{document}')
172 if breqn:
177 if breqn:
173 yield u(r'\begin{dmath*}')
178 yield u(r'\begin{dmath*}')
174 yield body
179 yield body
175 yield u(r'\end{dmath*}')
180 yield u(r'\end{dmath*}')
176 elif wrap:
181 elif wrap:
177 yield u'$${0}$$'.format(body)
182 yield u'$${0}$$'.format(body)
178 else:
183 else:
179 yield body
184 yield body
180 yield u'\end{document}'
185 yield u'\end{document}'
181
186
182
187
183 _data_uri_template_png = u"""<img src="data:image/png;base64,%s" alt=%s />"""
188 _data_uri_template_png = u"""<img src="data:image/png;base64,%s" alt=%s />"""
184
189
185 def latex_to_html(s, alt='image'):
190 def latex_to_html(s, alt='image'):
186 """Render LaTeX to HTML with embedded PNG data using data URIs.
191 """Render LaTeX to HTML with embedded PNG data using data URIs.
187
192
188 Parameters
193 Parameters
189 ----------
194 ----------
190 s : str
195 s : str
191 The raw string containing valid inline LateX.
196 The raw string containing valid inline LateX.
192 alt : str
197 alt : str
193 The alt text to use for the HTML.
198 The alt text to use for the HTML.
194 """
199 """
195 base64_data = latex_to_png(s, encode=True).decode('ascii')
200 base64_data = latex_to_png(s, encode=True).decode('ascii')
196 if base64_data:
201 if base64_data:
197 return _data_uri_template_png % (base64_data, alt)
202 return _data_uri_template_png % (base64_data, alt)
198
203
199
204
General Comments 0
You need to be logged in to leave comments. Login now