Show More
@@ -0,0 +1,7 b'' | |||||
|
1 | Relative filenames in Latex rendering | |||
|
2 | ===================================== | |||
|
3 | ||||
|
4 | The `latex_to_png_dvipng` command internally generates input and output file arguments to `latex` and `dvipis`. These arguments are now generated as relative files to the current working directory instead of absolute file paths. | |||
|
5 | This solves a problem where the current working directory contains characters that are not handled properly by `latex` and `dvips`. | |||
|
6 | There are no changes to the user API. | |||
|
7 |
@@ -1,246 +1,246 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 | from base64 import encodebytes |
|
12 | from base64 import encodebytes | |
13 | import textwrap |
|
13 | import textwrap | |
14 |
|
14 | |||
15 | from pathlib import Path, PurePath |
|
15 | from pathlib import Path, PurePath | |
16 |
|
16 | |||
17 | from IPython.utils.process import find_cmd, FindCmdError |
|
17 | from IPython.utils.process import find_cmd, FindCmdError | |
18 | from traitlets.config import get_config |
|
18 | from traitlets.config import get_config | |
19 | from traitlets.config.configurable import SingletonConfigurable |
|
19 | from traitlets.config.configurable import SingletonConfigurable | |
20 | from traitlets import List, Bool, Unicode |
|
20 | from traitlets import List, Bool, Unicode | |
21 | from IPython.utils.py3compat import cast_unicode |
|
21 | from IPython.utils.py3compat import cast_unicode | |
22 |
|
22 | |||
23 |
|
23 | |||
24 | class LaTeXTool(SingletonConfigurable): |
|
24 | class LaTeXTool(SingletonConfigurable): | |
25 | """An object to store configuration of the LaTeX tool.""" |
|
25 | """An object to store configuration of the LaTeX tool.""" | |
26 | def _config_default(self): |
|
26 | def _config_default(self): | |
27 | return get_config() |
|
27 | return get_config() | |
28 |
|
28 | |||
29 | backends = List( |
|
29 | backends = List( | |
30 | Unicode(), ["matplotlib", "dvipng"], |
|
30 | Unicode(), ["matplotlib", "dvipng"], | |
31 | help="Preferred backend to draw LaTeX math equations. " |
|
31 | help="Preferred backend to draw LaTeX math equations. " | |
32 | "Backends in the list are checked one by one and the first " |
|
32 | "Backends in the list are checked one by one and the first " | |
33 | "usable one is used. Note that `matplotlib` backend " |
|
33 | "usable one is used. Note that `matplotlib` backend " | |
34 | "is usable only for inline style equations. To draw " |
|
34 | "is usable only for inline style equations. To draw " | |
35 | "display style equations, `dvipng` backend must be specified. ", |
|
35 | "display style equations, `dvipng` backend must be specified. ", | |
36 | # It is a List instead of Enum, to make configuration more |
|
36 | # It is a List instead of Enum, to make configuration more | |
37 | # flexible. For example, to use matplotlib mainly but dvipng |
|
37 | # flexible. For example, to use matplotlib mainly but dvipng | |
38 | # for display style, the default ["matplotlib", "dvipng"] can |
|
38 | # for display style, the default ["matplotlib", "dvipng"] can | |
39 | # be used. To NOT use dvipng so that other repr such as |
|
39 | # be used. To NOT use dvipng so that other repr such as | |
40 | # unicode pretty printing is used, you can use ["matplotlib"]. |
|
40 | # unicode pretty printing is used, you can use ["matplotlib"]. | |
41 | ).tag(config=True) |
|
41 | ).tag(config=True) | |
42 |
|
42 | |||
43 | use_breqn = Bool( |
|
43 | use_breqn = Bool( | |
44 | True, |
|
44 | True, | |
45 | help="Use breqn.sty to automatically break long equations. " |
|
45 | help="Use breqn.sty to automatically break long equations. " | |
46 | "This configuration takes effect only for dvipng backend.", |
|
46 | "This configuration takes effect only for dvipng backend.", | |
47 | ).tag(config=True) |
|
47 | ).tag(config=True) | |
48 |
|
48 | |||
49 | packages = List( |
|
49 | packages = List( | |
50 | ['amsmath', 'amsthm', 'amssymb', 'bm'], |
|
50 | ['amsmath', 'amsthm', 'amssymb', 'bm'], | |
51 | help="A list of packages to use for dvipng backend. " |
|
51 | help="A list of packages to use for dvipng backend. " | |
52 | "'breqn' will be automatically appended when use_breqn=True.", |
|
52 | "'breqn' will be automatically appended when use_breqn=True.", | |
53 | ).tag(config=True) |
|
53 | ).tag(config=True) | |
54 |
|
54 | |||
55 | preamble = Unicode( |
|
55 | preamble = Unicode( | |
56 | help="Additional preamble to use when generating LaTeX source " |
|
56 | help="Additional preamble to use when generating LaTeX source " | |
57 | "for dvipng backend.", |
|
57 | "for dvipng backend.", | |
58 | ).tag(config=True) |
|
58 | ).tag(config=True) | |
59 |
|
59 | |||
60 |
|
60 | |||
61 | def latex_to_png(s, encode=False, backend=None, wrap=False, color='Black', |
|
61 | def latex_to_png(s, encode=False, backend=None, wrap=False, color='Black', | |
62 | scale=1.0): |
|
62 | scale=1.0): | |
63 | """Render a LaTeX string to PNG. |
|
63 | """Render a LaTeX string to PNG. | |
64 |
|
64 | |||
65 | Parameters |
|
65 | Parameters | |
66 | ---------- |
|
66 | ---------- | |
67 | s : str |
|
67 | s : str | |
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 | color : string |
|
75 | color : string | |
76 | Foreground color name among dvipsnames, e.g. 'Maroon' or on hex RGB |
|
76 | Foreground color name among dvipsnames, e.g. 'Maroon' or on hex RGB | |
77 | format, e.g. '#AA20FA'. |
|
77 | format, e.g. '#AA20FA'. | |
78 | scale : float |
|
78 | scale : float | |
79 | Scale factor for the resulting PNG. |
|
79 | Scale factor for the resulting PNG. | |
80 | None is returned when the backend cannot be used. |
|
80 | None is returned when the backend cannot be used. | |
81 |
|
81 | |||
82 | """ |
|
82 | """ | |
83 | s = cast_unicode(s) |
|
83 | s = cast_unicode(s) | |
84 | allowed_backends = LaTeXTool.instance().backends |
|
84 | allowed_backends = LaTeXTool.instance().backends | |
85 | if backend is None: |
|
85 | if backend is None: | |
86 | backend = allowed_backends[0] |
|
86 | backend = allowed_backends[0] | |
87 | if backend not in allowed_backends: |
|
87 | if backend not in allowed_backends: | |
88 | return None |
|
88 | return None | |
89 | if backend == 'matplotlib': |
|
89 | if backend == 'matplotlib': | |
90 | f = latex_to_png_mpl |
|
90 | f = latex_to_png_mpl | |
91 | elif backend == 'dvipng': |
|
91 | elif backend == 'dvipng': | |
92 | f = latex_to_png_dvipng |
|
92 | f = latex_to_png_dvipng | |
93 | if color.startswith('#'): |
|
93 | if color.startswith('#'): | |
94 | # Convert hex RGB color to LaTeX RGB color. |
|
94 | # Convert hex RGB color to LaTeX RGB color. | |
95 | if len(color) == 7: |
|
95 | if len(color) == 7: | |
96 | try: |
|
96 | try: | |
97 | color = "RGB {}".format(" ".join([str(int(x, 16)) for x in |
|
97 | color = "RGB {}".format(" ".join([str(int(x, 16)) for x in | |
98 | textwrap.wrap(color[1:], 2)])) |
|
98 | textwrap.wrap(color[1:], 2)])) | |
99 | except ValueError as e: |
|
99 | except ValueError as e: | |
100 | raise ValueError('Invalid color specification {}.'.format(color)) from e |
|
100 | raise ValueError('Invalid color specification {}.'.format(color)) from e | |
101 | else: |
|
101 | else: | |
102 | raise ValueError('Invalid color specification {}.'.format(color)) |
|
102 | raise ValueError('Invalid color specification {}.'.format(color)) | |
103 | else: |
|
103 | else: | |
104 | raise ValueError('No such backend {0}'.format(backend)) |
|
104 | raise ValueError('No such backend {0}'.format(backend)) | |
105 | bin_data = f(s, wrap, color, scale) |
|
105 | bin_data = f(s, wrap, color, scale) | |
106 | if encode and bin_data: |
|
106 | if encode and bin_data: | |
107 | bin_data = encodebytes(bin_data) |
|
107 | bin_data = encodebytes(bin_data) | |
108 | return bin_data |
|
108 | return bin_data | |
109 |
|
109 | |||
110 |
|
110 | |||
111 | def latex_to_png_mpl(s, wrap, color='Black', scale=1.0): |
|
111 | def latex_to_png_mpl(s, wrap, color='Black', scale=1.0): | |
112 | try: |
|
112 | try: | |
113 | from matplotlib import figure, font_manager, mathtext |
|
113 | from matplotlib import figure, font_manager, mathtext | |
114 | from matplotlib.backends import backend_agg |
|
114 | from matplotlib.backends import backend_agg | |
115 | from pyparsing import ParseFatalException |
|
115 | from pyparsing import ParseFatalException | |
116 | except ImportError: |
|
116 | except ImportError: | |
117 | return None |
|
117 | return None | |
118 |
|
118 | |||
119 | # mpl mathtext doesn't support display math, force inline |
|
119 | # mpl mathtext doesn't support display math, force inline | |
120 | s = s.replace('$$', '$') |
|
120 | s = s.replace('$$', '$') | |
121 | if wrap: |
|
121 | if wrap: | |
122 | s = u'${0}$'.format(s) |
|
122 | s = u'${0}$'.format(s) | |
123 |
|
123 | |||
124 | try: |
|
124 | try: | |
125 | prop = font_manager.FontProperties(size=12) |
|
125 | prop = font_manager.FontProperties(size=12) | |
126 | dpi = 120 * scale |
|
126 | dpi = 120 * scale | |
127 | buffer = BytesIO() |
|
127 | buffer = BytesIO() | |
128 |
|
128 | |||
129 | # Adapted from mathtext.math_to_image |
|
129 | # Adapted from mathtext.math_to_image | |
130 | parser = mathtext.MathTextParser("path") |
|
130 | parser = mathtext.MathTextParser("path") | |
131 | width, height, depth, _, _ = parser.parse(s, dpi=72, prop=prop) |
|
131 | width, height, depth, _, _ = parser.parse(s, dpi=72, prop=prop) | |
132 | fig = figure.Figure(figsize=(width / 72, height / 72)) |
|
132 | fig = figure.Figure(figsize=(width / 72, height / 72)) | |
133 | fig.text(0, depth / height, s, fontproperties=prop, color=color) |
|
133 | fig.text(0, depth / height, s, fontproperties=prop, color=color) | |
134 | backend_agg.FigureCanvasAgg(fig) |
|
134 | backend_agg.FigureCanvasAgg(fig) | |
135 | fig.savefig(buffer, dpi=dpi, format="png", transparent=True) |
|
135 | fig.savefig(buffer, dpi=dpi, format="png", transparent=True) | |
136 | return buffer.getvalue() |
|
136 | return buffer.getvalue() | |
137 | except (ValueError, RuntimeError, ParseFatalException): |
|
137 | except (ValueError, RuntimeError, ParseFatalException): | |
138 | return None |
|
138 | return None | |
139 |
|
139 | |||
140 |
|
140 | |||
141 | def latex_to_png_dvipng(s, wrap, color='Black', scale=1.0): |
|
141 | def latex_to_png_dvipng(s, wrap, color='Black', scale=1.0): | |
142 | try: |
|
142 | try: | |
143 | find_cmd('latex') |
|
143 | find_cmd('latex') | |
144 | find_cmd('dvipng') |
|
144 | find_cmd('dvipng') | |
145 | except FindCmdError: |
|
145 | except FindCmdError: | |
146 | return None |
|
146 | return None | |
147 | try: |
|
147 | try: | |
148 | workdir = Path(tempfile.mkdtemp()) |
|
148 | workdir = Path(tempfile.mkdtemp()) | |
149 |
tmpfile = |
|
149 | tmpfile = "tmp.tex" | |
150 |
dvifile = |
|
150 | dvifile = "tmp.dvi" | |
151 |
outfile = |
|
151 | outfile = "tmp.png" | |
152 |
|
152 | |||
153 | with tmpfile.open("w", encoding="utf8") as f: |
|
153 | with workdir.joinpath(tmpfile).open("w", encoding="utf8") as f: | |
154 | f.writelines(genelatex(s, wrap)) |
|
154 | f.writelines(genelatex(s, wrap)) | |
155 |
|
155 | |||
156 | with open(os.devnull, 'wb') as devnull: |
|
156 | with open(os.devnull, 'wb') as devnull: | |
157 | subprocess.check_call( |
|
157 | subprocess.check_call( | |
158 | ["latex", "-halt-on-error", "-interaction", "batchmode", tmpfile], |
|
158 | ["latex", "-halt-on-error", "-interaction", "batchmode", tmpfile], | |
159 | cwd=workdir, stdout=devnull, stderr=devnull) |
|
159 | cwd=workdir, stdout=devnull, stderr=devnull) | |
160 |
|
160 | |||
161 | resolution = round(150*scale) |
|
161 | resolution = round(150*scale) | |
162 | subprocess.check_call( |
|
162 | subprocess.check_call( | |
163 | [ |
|
163 | [ | |
164 | "dvipng", |
|
164 | "dvipng", | |
165 | "-T", |
|
165 | "-T", | |
166 | "tight", |
|
166 | "tight", | |
167 | "-D", |
|
167 | "-D", | |
168 | str(resolution), |
|
168 | str(resolution), | |
169 | "-z", |
|
169 | "-z", | |
170 | "9", |
|
170 | "9", | |
171 | "-bg", |
|
171 | "-bg", | |
172 | "Transparent", |
|
172 | "Transparent", | |
173 | "-o", |
|
173 | "-o", | |
174 | outfile, |
|
174 | outfile, | |
175 | dvifile, |
|
175 | dvifile, | |
176 | "-fg", |
|
176 | "-fg", | |
177 | color, |
|
177 | color, | |
178 | ], |
|
178 | ], | |
179 | cwd=workdir, |
|
179 | cwd=workdir, | |
180 | stdout=devnull, |
|
180 | stdout=devnull, | |
181 | stderr=devnull, |
|
181 | stderr=devnull, | |
182 | ) |
|
182 | ) | |
183 |
|
183 | |||
184 | with outfile.open("rb") as f: |
|
184 | with workdir.joinpath(outfile).open("rb") as f: | |
185 | return f.read() |
|
185 | return f.read() | |
186 | except subprocess.CalledProcessError: |
|
186 | except subprocess.CalledProcessError: | |
187 | return None |
|
187 | return None | |
188 | finally: |
|
188 | finally: | |
189 | shutil.rmtree(workdir) |
|
189 | shutil.rmtree(workdir) | |
190 |
|
190 | |||
191 |
|
191 | |||
192 | def kpsewhich(filename): |
|
192 | def kpsewhich(filename): | |
193 | """Invoke kpsewhich command with an argument `filename`.""" |
|
193 | """Invoke kpsewhich command with an argument `filename`.""" | |
194 | try: |
|
194 | try: | |
195 | find_cmd("kpsewhich") |
|
195 | find_cmd("kpsewhich") | |
196 | proc = subprocess.Popen( |
|
196 | proc = subprocess.Popen( | |
197 | ["kpsewhich", filename], |
|
197 | ["kpsewhich", filename], | |
198 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
198 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
199 | (stdout, stderr) = proc.communicate() |
|
199 | (stdout, stderr) = proc.communicate() | |
200 | return stdout.strip().decode('utf8', 'replace') |
|
200 | return stdout.strip().decode('utf8', 'replace') | |
201 | except FindCmdError: |
|
201 | except FindCmdError: | |
202 | pass |
|
202 | pass | |
203 |
|
203 | |||
204 |
|
204 | |||
205 | def genelatex(body, wrap): |
|
205 | def genelatex(body, wrap): | |
206 | """Generate LaTeX document for dvipng backend.""" |
|
206 | """Generate LaTeX document for dvipng backend.""" | |
207 | lt = LaTeXTool.instance() |
|
207 | lt = LaTeXTool.instance() | |
208 | breqn = wrap and lt.use_breqn and kpsewhich("breqn.sty") |
|
208 | breqn = wrap and lt.use_breqn and kpsewhich("breqn.sty") | |
209 | yield r'\documentclass{article}' |
|
209 | yield r'\documentclass{article}' | |
210 | packages = lt.packages |
|
210 | packages = lt.packages | |
211 | if breqn: |
|
211 | if breqn: | |
212 | packages = packages + ['breqn'] |
|
212 | packages = packages + ['breqn'] | |
213 | for pack in packages: |
|
213 | for pack in packages: | |
214 | yield r'\usepackage{{{0}}}'.format(pack) |
|
214 | yield r'\usepackage{{{0}}}'.format(pack) | |
215 | yield r'\pagestyle{empty}' |
|
215 | yield r'\pagestyle{empty}' | |
216 | if lt.preamble: |
|
216 | if lt.preamble: | |
217 | yield lt.preamble |
|
217 | yield lt.preamble | |
218 | yield r'\begin{document}' |
|
218 | yield r'\begin{document}' | |
219 | if breqn: |
|
219 | if breqn: | |
220 | yield r'\begin{dmath*}' |
|
220 | yield r'\begin{dmath*}' | |
221 | yield body |
|
221 | yield body | |
222 | yield r'\end{dmath*}' |
|
222 | yield r'\end{dmath*}' | |
223 | elif wrap: |
|
223 | elif wrap: | |
224 | yield u'$${0}$$'.format(body) |
|
224 | yield u'$${0}$$'.format(body) | |
225 | else: |
|
225 | else: | |
226 | yield body |
|
226 | yield body | |
227 | yield u'\\end{document}' |
|
227 | yield u'\\end{document}' | |
228 |
|
228 | |||
229 |
|
229 | |||
230 | _data_uri_template_png = u"""<img src="data:image/png;base64,%s" alt=%s />""" |
|
230 | _data_uri_template_png = u"""<img src="data:image/png;base64,%s" alt=%s />""" | |
231 |
|
231 | |||
232 | def latex_to_html(s, alt='image'): |
|
232 | def latex_to_html(s, alt='image'): | |
233 | """Render LaTeX to HTML with embedded PNG data using data URIs. |
|
233 | """Render LaTeX to HTML with embedded PNG data using data URIs. | |
234 |
|
234 | |||
235 | Parameters |
|
235 | Parameters | |
236 | ---------- |
|
236 | ---------- | |
237 | s : str |
|
237 | s : str | |
238 | The raw string containing valid inline LateX. |
|
238 | The raw string containing valid inline LateX. | |
239 | alt : str |
|
239 | alt : str | |
240 | The alt text to use for the HTML. |
|
240 | The alt text to use for the HTML. | |
241 | """ |
|
241 | """ | |
242 | base64_data = latex_to_png(s, encode=True).decode('ascii') |
|
242 | base64_data = latex_to_png(s, encode=True).decode('ascii') | |
243 | if base64_data: |
|
243 | if base64_data: | |
244 | return _data_uri_template_png % (base64_data, alt) |
|
244 | return _data_uri_template_png % (base64_data, alt) | |
245 |
|
245 | |||
246 |
|
246 |
General Comments 0
You need to be logged in to leave comments.
Login now