Show More
@@ -11,7 +11,7 pretty-printed. | |||||
11 |
|
11 | |||
12 | """ |
|
12 | """ | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
# Copyright (C) 2008 |
|
14 | # Copyright (C) 2008 The IPython Development Team | |
15 | # |
|
15 | # | |
16 | # Distributed under the terms of the BSD License. The full license is in |
|
16 | # Distributed under the terms of the BSD License. The full license is in | |
17 | # the file COPYING, distributed as part of this software. |
|
17 | # the file COPYING, distributed as part of this software. | |
@@ -45,7 +45,9 def print_basic_unicode(o, p, cycle): | |||||
45 |
|
45 | |||
46 |
|
46 | |||
47 | def print_png(o): |
|
47 | def print_png(o): | |
48 | """A function to display sympy expression using LaTex -> PNG.""" |
|
48 | """ | |
|
49 | A function to display sympy expression using inline style LaTeX in PNG. | |||
|
50 | """ | |||
49 | s = latex(o, mode='inline') |
|
51 | s = latex(o, mode='inline') | |
50 | # mathtext does not understand certain latex flags, so we try to replace |
|
52 | # mathtext does not understand certain latex flags, so we try to replace | |
51 | # them with suitable subs. |
|
53 | # them with suitable subs. | |
@@ -54,6 +56,19 def print_png(o): | |||||
54 | png = latex_to_png(s) |
|
56 | png = latex_to_png(s) | |
55 | return png |
|
57 | return png | |
56 |
|
58 | |||
|
59 | ||||
|
60 | def print_display_png(o): | |||
|
61 | """ | |||
|
62 | A function to display sympy expression using display style LaTeX in PNG. | |||
|
63 | """ | |||
|
64 | s = latex(o, mode='plain') | |||
|
65 | s = s.strip('$') | |||
|
66 | # As matplotlib does not support display style, dvipng backend is | |||
|
67 | # used here. | |||
|
68 | png = latex_to_png('$$%s$$' % s, backend='dvipng') | |||
|
69 | return png | |||
|
70 | ||||
|
71 | ||||
57 | def can_print_latex(o): |
|
72 | def can_print_latex(o): | |
58 | """ |
|
73 | """ | |
59 | Return True if type o can be printed with LaTeX. |
|
74 | Return True if type o can be printed with LaTeX. | |
@@ -115,6 +130,9 def load_ipython_extension(ip): | |||||
115 | png_formatter.for_type_by_name( |
|
130 | png_formatter.for_type_by_name( | |
116 | 'sympy.core.basic', 'Basic', print_png |
|
131 | 'sympy.core.basic', 'Basic', print_png | |
117 | ) |
|
132 | ) | |
|
133 | png_formatter.for_type_by_name( | |||
|
134 | 'sympy.matrices.matrices', 'Matrix', print_display_png | |||
|
135 | ) | |||
118 | for cls in [dict, int, long, float] + printable_containers: |
|
136 | for cls in [dict, int, long, float] + printable_containers: | |
119 | png_formatter.for_type(cls, print_png) |
|
137 | png_formatter.for_type(cls, print_png) | |
120 |
|
138 |
@@ -6,7 +6,7 Authors: | |||||
6 | * Brian Granger |
|
6 | * Brian Granger | |
7 | """ |
|
7 | """ | |
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 |
# Copyright (C) 2010 |
|
9 | # Copyright (C) 2010 IPython Development Team. | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the Modified BSD License. |
|
11 | # Distributed under the terms of the Modified BSD License. | |
12 | # |
|
12 | # | |
@@ -19,14 +19,20 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 | |||
|
26 | ||||
|
27 | from IPython.utils.process import find_cmd, FindCmdError | |||
22 |
|
28 | |||
23 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
24 | # Tools |
|
30 | # Tools | |
25 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
26 |
|
32 | |||
27 |
|
33 | |||
28 | def latex_to_png(s, encode=False): |
|
34 | def latex_to_png(s, encode=False, backend='mpl'): | |
29 |
"""Render a LaTeX string to PNG |
|
35 | """Render a LaTeX string to PNG. | |
30 |
|
36 | |||
31 | Parameters |
|
37 | Parameters | |
32 | ---------- |
|
38 | ---------- | |
@@ -34,18 +40,82 def latex_to_png(s, encode=False): | |||||
34 | The raw string containing valid inline LaTeX. |
|
40 | The raw string containing valid inline LaTeX. | |
35 | encode : bool, optional |
|
41 | encode : bool, optional | |
36 | 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} | |||
|
44 | Backend for producing PNG data. | |||
|
45 | ||||
|
46 | None is returned when the backend cannot be used. | |||
|
47 | ||||
37 | """ |
|
48 | """ | |
|
49 | if backend == 'mpl': | |||
|
50 | f = latex_to_png_mpl | |||
|
51 | elif backend == 'dvipng': | |||
|
52 | f = latex_to_png_dvipng | |||
|
53 | else: | |||
|
54 | raise ValueError('No such backend {0}'.format(backend)) | |||
|
55 | bin_data = f(s) | |||
|
56 | if encode and bin_data: | |||
|
57 | bin_data = encodestring(bin_data) | |||
|
58 | return bin_data | |||
|
59 | ||||
|
60 | ||||
|
61 | def latex_to_png_mpl(s): | |||
|
62 | try: | |||
38 | from matplotlib import mathtext |
|
63 | from matplotlib import mathtext | |
|
64 | except ImportError: | |||
|
65 | return None | |||
39 |
|
66 | |||
40 | mt = mathtext.MathTextParser('bitmap') |
|
67 | mt = mathtext.MathTextParser('bitmap') | |
41 | f = StringIO() |
|
68 | f = StringIO() | |
42 | mt.to_png(f, s, fontsize=12) |
|
69 | mt.to_png(f, s, fontsize=12) | |
43 |
|
|
70 | return f.getvalue() | |
44 | if encode: |
|
71 | ||
45 | bin_data = encodestring(bin_data) |
|
72 | ||
|
73 | def latex_to_png_dvipng(s): | |||
|
74 | try: | |||
|
75 | find_cmd('latex') | |||
|
76 | find_cmd('dvipng') | |||
|
77 | except FindCmdError: | |||
|
78 | return None | |||
|
79 | try: | |||
|
80 | workdir = tempfile.mkdtemp() | |||
|
81 | tmpfile = os.path.join(workdir, "tmp.tex") | |||
|
82 | dvifile = os.path.join(workdir, "tmp.dvi") | |||
|
83 | outfile = os.path.join(workdir, "tmp.png") | |||
|
84 | ||||
|
85 | with open(tmpfile, "w") as f: | |||
|
86 | f.write(_latex_header) | |||
|
87 | f.write(s) | |||
|
88 | f.write(_latex_footer) | |||
|
89 | ||||
|
90 | subprocess.check_call( | |||
|
91 | ["latex", "-halt-on-errror", tmpfile], cwd=workdir, | |||
|
92 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |||
|
93 | ||||
|
94 | subprocess.check_call( | |||
|
95 | ["dvipng", "-T", "tight", "-x", "1500", "-z", "9", | |||
|
96 | "-bg", "transparent", "-o", outfile, dvifile], cwd=workdir, | |||
|
97 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |||
|
98 | ||||
|
99 | with open(outfile) as f: | |||
|
100 | bin_data = f.read() | |||
|
101 | finally: | |||
|
102 | shutil.rmtree(workdir) | |||
46 | return bin_data |
|
103 | return bin_data | |
47 |
|
104 | |||
48 |
|
105 | |||
|
106 | _latex_header = r''' | |||
|
107 | \documentclass{article} | |||
|
108 | \usepackage{amsmath} | |||
|
109 | \usepackage{amsthm} | |||
|
110 | \usepackage{amssymb} | |||
|
111 | \usepackage{bm} | |||
|
112 | \pagestyle{empty} | |||
|
113 | \begin{document} | |||
|
114 | ''' | |||
|
115 | ||||
|
116 | _latex_footer = r'\end{document}' | |||
|
117 | ||||
|
118 | ||||
49 | _data_uri_template_png = """<img src="data:image/png;base64,%s" alt=%s />""" |
|
119 | _data_uri_template_png = """<img src="data:image/png;base64,%s" alt=%s />""" | |
50 |
|
120 | |||
51 | def latex_to_html(s, alt='image'): |
|
121 | def latex_to_html(s, alt='image'): | |
@@ -59,6 +129,7 def latex_to_html(s, alt='image'): | |||||
59 | The alt text to use for the HTML. |
|
129 | The alt text to use for the HTML. | |
60 | """ |
|
130 | """ | |
61 | base64_data = latex_to_png(s, encode=True) |
|
131 | base64_data = latex_to_png(s, encode=True) | |
|
132 | if base64_data: | |||
62 | return _data_uri_template_png % (base64_data, alt) |
|
133 | return _data_uri_template_png % (base64_data, alt) | |
63 |
|
134 | |||
64 |
|
135 |
General Comments 0
You need to be logged in to leave comments.
Login now