##// END OF EJS Templates
Use breqn.sty in dvipng backend if possible
Takafumi Arakaki -
Show More
@@ -65,7 +65,7 b' def print_display_png(o):'
65 65 s = s.strip('$')
66 66 # As matplotlib does not support display style, dvipng backend is
67 67 # used here.
68 png = latex_to_png('$$%s$$' % s, backend='dvipng')
68 png = latex_to_png(s, backend='dvipng', wrap=True)
69 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 35 """Render a LaTeX string to PNG.
36 36
37 37 Parameters
@@ -42,6 +42,8 b" def latex_to_png(s, encode=False, backend='mpl'):"
42 42 Should the PNG data bebase64 encoded to make it JSON'able.
43 43 backend : {mpl, dvipng}
44 44 Backend for producing PNG data.
45 wrap : bool
46 If true, Automatically wrap `s` as a LaTeX equation.
45 47
46 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 54 f = latex_to_png_dvipng
53 55 else:
54 56 raise ValueError('No such backend {0}'.format(backend))
55 bin_data = f(s)
57 bin_data = f(s, wrap)
56 58 if encode and bin_data:
57 59 bin_data = encodestring(bin_data)
58 60 return bin_data
59 61
60 62
61 def latex_to_png_mpl(s):
63 def latex_to_png_mpl(s, wrap):
62 64 try:
63 65 from matplotlib import mathtext
64 66 except ImportError:
65 67 return None
66
68
69 if wrap:
70 s = '${0}$'.format(s)
67 71 mt = mathtext.MathTextParser('bitmap')
68 72 f = StringIO()
69 73 mt.to_png(f, s, fontsize=12)
70 74 return f.getvalue()
71 75
72 76
73 def latex_to_png_dvipng(s):
77 def latex_to_png_dvipng(s, wrap):
74 78 try:
75 79 find_cmd('latex')
76 80 find_cmd('dvipng')
@@ -83,9 +87,7 b' def latex_to_png_dvipng(s):'
83 87 outfile = os.path.join(workdir, "tmp.png")
84 88
85 89 with open(tmpfile, "w") as f:
86 f.write(_latex_header)
87 f.write(s)
88 f.write(_latex_footer)
90 f.writelines(genelatex(s, wrap))
89 91
90 92 subprocess.check_call(
91 93 ["latex", "-halt-on-errror", tmpfile], cwd=workdir,
@@ -103,17 +105,39 b' def latex_to_png_dvipng(s):'
103 105 return bin_data
104 106
105 107
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}'
108 def kpsewhich(filename):
109 """Invoke kpsewhich command with an argument `filename`."""
110 try:
111 find_cmd("kpsewhich")
112 proc = subprocess.Popen(
113 ["kpsewhich", filename],
114 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
115 (stdout, stderr) = proc.communicate()
116 return stdout.strip()
117 except FindCmdError:
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 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