##// END OF EJS Templates
Enable changing the font color for LaTeX rendering
Oscar Gustafsson -
Show More
@@ -22,7 +22,7 b' class LaTeXTool(SingletonConfigurable):'
22 """An object to store configuration of the LaTeX tool."""
22 """An object to store configuration of the LaTeX tool."""
23 def _config_default(self):
23 def _config_default(self):
24 return get_config()
24 return get_config()
25
25
26 backends = List(
26 backends = List(
27 Unicode(), ["matplotlib", "dvipng"],
27 Unicode(), ["matplotlib", "dvipng"],
28 help="Preferred backend to draw LaTeX math equations. "
28 help="Preferred backend to draw LaTeX math equations. "
@@ -55,7 +55,7 b' class LaTeXTool(SingletonConfigurable):'
55 ).tag(config=True)
55 ).tag(config=True)
56
56
57
57
58 def latex_to_png(s, encode=False, backend=None, wrap=False):
58 def latex_to_png(s, encode=False, backend=None, wrap=False, color='Black'):
59 """Render a LaTeX string to PNG.
59 """Render a LaTeX string to PNG.
60
60
61 Parameters
61 Parameters
@@ -68,6 +68,8 b' def latex_to_png(s, encode=False, backend=None, wrap=False):'
68 Backend for producing PNG data.
68 Backend for producing PNG data.
69 wrap : bool
69 wrap : bool
70 If true, Automatically wrap `s` as a LaTeX equation.
70 If true, Automatically wrap `s` as a LaTeX equation.
71 color : string
72 Foreground color name among dvipsnames.
71
73
72 None is returned when the backend cannot be used.
74 None is returned when the backend cannot be used.
73
75
@@ -84,13 +86,13 b' def latex_to_png(s, encode=False, backend=None, wrap=False):'
84 f = latex_to_png_dvipng
86 f = latex_to_png_dvipng
85 else:
87 else:
86 raise ValueError('No such backend {0}'.format(backend))
88 raise ValueError('No such backend {0}'.format(backend))
87 bin_data = f(s, wrap)
89 bin_data = f(s, wrap, color)
88 if encode and bin_data:
90 if encode and bin_data:
89 bin_data = encodebytes(bin_data)
91 bin_data = encodebytes(bin_data)
90 return bin_data
92 return bin_data
91
93
92
94
93 def latex_to_png_mpl(s, wrap):
95 def latex_to_png_mpl(s, wrap, color='Black'):
94 try:
96 try:
95 from matplotlib import mathtext
97 from matplotlib import mathtext
96 from pyparsing import ParseFatalException
98 from pyparsing import ParseFatalException
@@ -105,13 +107,13 b' def latex_to_png_mpl(s, wrap):'
105 try:
107 try:
106 mt = mathtext.MathTextParser('bitmap')
108 mt = mathtext.MathTextParser('bitmap')
107 f = BytesIO()
109 f = BytesIO()
108 mt.to_png(f, s, fontsize=12)
110 mt.to_png(f, s, fontsize=12, color=color)
109 return f.getvalue()
111 return f.getvalue()
110 except (ValueError, RuntimeError, ParseFatalException):
112 except (ValueError, RuntimeError, ParseFatalException):
111 return None
113 return None
112
114
113
115
114 def latex_to_png_dvipng(s, wrap):
116 def latex_to_png_dvipng(s, wrap, color='Black'):
115 try:
117 try:
116 find_cmd('latex')
118 find_cmd('latex')
117 find_cmd('dvipng')
119 find_cmd('dvipng')
@@ -133,8 +135,8 b' def latex_to_png_dvipng(s, wrap):'
133
135
134 subprocess.check_call(
136 subprocess.check_call(
135 ["dvipng", "-T", "tight", "-x", "1500", "-z", "9",
137 ["dvipng", "-T", "tight", "-x", "1500", "-z", "9",
136 "-bg", "transparent", "-o", outfile, dvifile], cwd=workdir,
138 "-bg", "transparent", "-o", outfile, dvifile, "-fg", color],
137 stdout=devnull, stderr=devnull)
139 cwd=workdir, stdout=devnull, stderr=devnull)
138
140
139 with open(outfile, "rb") as f:
141 with open(outfile, "rb") as f:
140 return f.read()
142 return f.read()
@@ -62,7 +62,7 b' def test_latex_to_png_mpl_runs():'
62 @skipif_not_matplotlib
62 @skipif_not_matplotlib
63 def test_latex_to_html():
63 def test_latex_to_html():
64 img = latextools.latex_to_html("$x^2$")
64 img = latextools.latex_to_html("$x^2$")
65 nt.assert_in("data:image/png;base64,iVBOR", img)
65 nt.assert_in("data:image/png;base64,iVBOR", img)
66
66
67
67
68 def test_genelatex_no_wrap():
68 def test_genelatex_no_wrap():
@@ -132,3 +132,26 b' def test_genelatex_wrap_without_breqn():'
132 \begin{document}
132 \begin{document}
133 $$x^2$$
133 $$x^2$$
134 \end{document}''')
134 \end{document}''')
135
136
137 def test_latex_to_png_color():
138 """
139 Test color settings for latex_to_png.
140 """
141 latex_string = "$x^2$"
142 default_value = latextools.latex_to_png(latex_string, wrap=False)
143 dvipng_default = latextools.latex_to_png_dvipng(latex_string, False)
144 dvipng_black = latextools.latex_to_png_dvipng(latex_string, False, 'Black')
145 nt.assert_equal(dvipng_default, dvipng_black)
146 mpl_default = latextools.latex_to_png_mpl(latex_string, False)
147 mpl_black = latextools.latex_to_png_mpl(latex_string, False, 'Black')
148 nt.assert_equal(mpl_default, mpl_black)
149 nt.assert_in(default_value, [dvipng_black, mpl_black])
150
151 # Test that dvips name colors can be used without error
152 dvipng_maroon = latextools.latex_to_png_dvipng(latex_string, False, 'Maroon')
153 # And that it doesn't return the black one
154 nt.assert_not_equal(dvipng_black, dvipng_maroon)
155
156 mpl_maroon = latextools.latex_to_png_mpl(latex_string, False, 'Maroon')
157 nt.assert_not_equal(mpl_black, mpl_maroon)
General Comments 0
You need to be logged in to leave comments. Login now