##// END OF EJS Templates
Added mdboom's math_to_image from matplotlib.
Brian Granger -
Show More
@@ -1,62 +1,110
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Tools for handling LaTeX.
2 """Tools for handling LaTeX.
3
3
4 Authors:
4 Authors:
5
5
6 * Brian Granger
6 * Brian Granger
7 """
7 """
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (c) 2010, IPython Development Team.
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 #
13 # The full license is in the file COPYING.txt, distributed with this software.
13 # The full license is in the file COPYING.txt, distributed with this software.
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 # Imports
17 # Imports
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19
19
20 from StringIO import StringIO
20 from StringIO import StringIO
21 from base64 import encodestring
21 from base64 import encodestring
22
22
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24 # Tools
24 # Tools
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
26
27
27
28 def latex_to_png(s, encode=False):
28 def latex_to_png(s, encode=False):
29 """Render a LaTeX string to PNG using matplotlib.mathtext.
29 """Render a LaTeX string to PNG using matplotlib.mathtext.
30
30
31 Parameters
31 Parameters
32 ----------
32 ----------
33 s : str
33 s : str
34 The raw string containing valid inline LaTeX.
34 The raw string containing valid inline LaTeX.
35 encode : bool, optional
35 encode : bool, optional
36 Should the PNG data bebase64 encoded to make it JSON'able.
36 Should the PNG data bebase64 encoded to make it JSON'able.
37 """
37 """
38 from matplotlib import mathtext
38 from matplotlib import mathtext
39
39
40 mt = mathtext.MathTextParser('bitmap')
40 mt = mathtext.MathTextParser('bitmap')
41 f = StringIO()
41 f = StringIO()
42 mt.to_png(f, s, fontsize=12)
42 mt.to_png(f, s, fontsize=12)
43 bin_data = f.getvalue()
43 bin_data = f.getvalue()
44 if encode:
44 if encode:
45 bin_data = encodestring(bin_data)
45 bin_data = encodestring(bin_data)
46 return bin_data
46 return bin_data
47
47
48
48 _data_uri_template_png = """<img src="data:image/png;base64,%s" alt=%s />"""
49 _data_uri_template_png = """<img src="data:image/png;base64,%s" alt=%s />"""
49
50
50 def latex_to_html(s, alt='image'):
51 def latex_to_html(s, alt='image'):
51 """Render LaTeX to HTML with embedded PNG data using data URIs.
52 """Render LaTeX to HTML with embedded PNG data using data URIs.
52
53
53 Parameters
54 Parameters
54 ----------
55 ----------
55 s : str
56 s : str
56 The raw string containing valid inline LateX.
57 The raw string containing valid inline LateX.
57 alt : str
58 alt : str
58 The alt text to use for the HTML.
59 The alt text to use for the HTML.
59 """
60 """
60 base64_data = latex_to_png(s, encode=True)
61 base64_data = latex_to_png(s, encode=True)
61 return _data_uri_template_png % (base64_data, alt)
62 return _data_uri_template_png % (base64_data, alt)
62
63
64
65 # From matplotlib, thanks to mdboom. Once this is in matplotlib releases, we
66 # will remove.
67 def math_to_image(s, filename_or_obj, prop=None, dpi=None, format=None):
68 """
69 Given a math expression, renders it in a closely-clipped bounding
70 box to an image file.
71
72 *s*
73 A math expression. The math portion should be enclosed in
74 dollar signs.
75
76 *filename_or_obj*
77 A filepath or writable file-like object to write the image data
78 to.
79
80 *prop*
81 If provided, a FontProperties() object describing the size and
82 style of the text.
83
84 *dpi*
85 Override the output dpi, otherwise use the default associated
86 with the output format.
87
88 *format*
89 The output format, eg. 'svg', 'pdf', 'ps' or 'png'. If not
90 provided, will be deduced from the filename.
91 """
92 from matplotlib import figure
93 # backend_agg supports all of the core output formats
94 from matplotlib.backends import backend_agg
95 from matplotlib.font_manager import FontProperties
96 from matplotlib.mathtext import MathTextParser
97
98 if prop is None:
99 prop = FontProperties()
100
101 parser = MathTextParser('path')
102 width, height, depth, _, _ = parser.parse(s, dpi=72, prop=prop)
103
104 fig = figure.Figure(figsize=(width / 72.0, height / 72.0))
105 fig.text(0, depth/height, s, fontproperties=prop)
106 backend_agg.FigureCanvasAgg(fig)
107 fig.savefig(filename_or_obj, dpi=dpi, format=format)
108
109 return depth
110
General Comments 0
You need to be logged in to leave comments. Login now