##// END OF EJS Templates
Enable changing the font color for LaTeX rendering
Oscar Gustafsson -
Show More
@@ -55,7 +55,7 b' class LaTeXTool(SingletonConfigurable):'
55 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 59 """Render a LaTeX string to PNG.
60 60
61 61 Parameters
@@ -68,6 +68,8 b' def latex_to_png(s, encode=False, backend=None, wrap=False):'
68 68 Backend for producing PNG data.
69 69 wrap : bool
70 70 If true, Automatically wrap `s` as a LaTeX equation.
71 color : string
72 Foreground color name among dvipsnames.
71 73
72 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 86 f = latex_to_png_dvipng
85 87 else:
86 88 raise ValueError('No such backend {0}'.format(backend))
87 bin_data = f(s, wrap)
89 bin_data = f(s, wrap, color)
88 90 if encode and bin_data:
89 91 bin_data = encodebytes(bin_data)
90 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 96 try:
95 97 from matplotlib import mathtext
96 98 from pyparsing import ParseFatalException
@@ -105,13 +107,13 b' def latex_to_png_mpl(s, wrap):'
105 107 try:
106 108 mt = mathtext.MathTextParser('bitmap')
107 109 f = BytesIO()
108 mt.to_png(f, s, fontsize=12)
110 mt.to_png(f, s, fontsize=12, color=color)
109 111 return f.getvalue()
110 112 except (ValueError, RuntimeError, ParseFatalException):
111 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 117 try:
116 118 find_cmd('latex')
117 119 find_cmd('dvipng')
@@ -133,8 +135,8 b' def latex_to_png_dvipng(s, wrap):'
133 135
134 136 subprocess.check_call(
135 137 ["dvipng", "-T", "tight", "-x", "1500", "-z", "9",
136 "-bg", "transparent", "-o", outfile, dvifile], cwd=workdir,
137 stdout=devnull, stderr=devnull)
138 "-bg", "transparent", "-o", outfile, dvifile, "-fg", color],
139 cwd=workdir, stdout=devnull, stderr=devnull)
138 140
139 141 with open(outfile, "rb") as f:
140 142 return f.read()
@@ -132,3 +132,26 b' def test_genelatex_wrap_without_breqn():'
132 132 \begin{document}
133 133 $$x^2$$
134 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