From f91fc4e1dd915a1afd5a8913a8eba83c5024f285 2019-07-30 15:27:20 From: Oscar Gustafsson Date: 2019-07-30 15:27:20 Subject: [PATCH] Enable changing the font color for LaTeX rendering --- diff --git a/IPython/lib/latextools.py b/IPython/lib/latextools.py index b56b360..0acd16d 100644 --- a/IPython/lib/latextools.py +++ b/IPython/lib/latextools.py @@ -22,7 +22,7 @@ class LaTeXTool(SingletonConfigurable): """An object to store configuration of the LaTeX tool.""" def _config_default(self): return get_config() - + backends = List( Unicode(), ["matplotlib", "dvipng"], help="Preferred backend to draw LaTeX math equations. " @@ -55,7 +55,7 @@ class LaTeXTool(SingletonConfigurable): ).tag(config=True) -def latex_to_png(s, encode=False, backend=None, wrap=False): +def latex_to_png(s, encode=False, backend=None, wrap=False, color='Black'): """Render a LaTeX string to PNG. Parameters @@ -68,6 +68,8 @@ def latex_to_png(s, encode=False, backend=None, wrap=False): Backend for producing PNG data. wrap : bool If true, Automatically wrap `s` as a LaTeX equation. + color : string + Foreground color name among dvipsnames. None is returned when the backend cannot be used. @@ -84,13 +86,13 @@ def latex_to_png(s, encode=False, backend=None, wrap=False): f = latex_to_png_dvipng else: raise ValueError('No such backend {0}'.format(backend)) - bin_data = f(s, wrap) + bin_data = f(s, wrap, color) if encode and bin_data: bin_data = encodebytes(bin_data) return bin_data -def latex_to_png_mpl(s, wrap): +def latex_to_png_mpl(s, wrap, color='Black'): try: from matplotlib import mathtext from pyparsing import ParseFatalException @@ -105,13 +107,13 @@ def latex_to_png_mpl(s, wrap): try: mt = mathtext.MathTextParser('bitmap') f = BytesIO() - mt.to_png(f, s, fontsize=12) + mt.to_png(f, s, fontsize=12, color=color) return f.getvalue() except (ValueError, RuntimeError, ParseFatalException): return None -def latex_to_png_dvipng(s, wrap): +def latex_to_png_dvipng(s, wrap, color='Black'): try: find_cmd('latex') find_cmd('dvipng') @@ -133,8 +135,8 @@ def latex_to_png_dvipng(s, wrap): subprocess.check_call( ["dvipng", "-T", "tight", "-x", "1500", "-z", "9", - "-bg", "transparent", "-o", outfile, dvifile], cwd=workdir, - stdout=devnull, stderr=devnull) + "-bg", "transparent", "-o", outfile, dvifile, "-fg", color], + cwd=workdir, stdout=devnull, stderr=devnull) with open(outfile, "rb") as f: return f.read() diff --git a/IPython/lib/tests/test_latextools.py b/IPython/lib/tests/test_latextools.py index 2772784..35e5309 100644 --- a/IPython/lib/tests/test_latextools.py +++ b/IPython/lib/tests/test_latextools.py @@ -62,7 +62,7 @@ def test_latex_to_png_mpl_runs(): @skipif_not_matplotlib def test_latex_to_html(): img = latextools.latex_to_html("$x^2$") - nt.assert_in("data:image/png;base64,iVBOR", img) + nt.assert_in("data:image/png;base64,iVBOR", img) def test_genelatex_no_wrap(): @@ -132,3 +132,26 @@ def test_genelatex_wrap_without_breqn(): \begin{document} $$x^2$$ \end{document}''') + + +def test_latex_to_png_color(): + """ + Test color settings for latex_to_png. + """ + latex_string = "$x^2$" + default_value = latextools.latex_to_png(latex_string, wrap=False) + dvipng_default = latextools.latex_to_png_dvipng(latex_string, False) + dvipng_black = latextools.latex_to_png_dvipng(latex_string, False, 'Black') + nt.assert_equal(dvipng_default, dvipng_black) + mpl_default = latextools.latex_to_png_mpl(latex_string, False) + mpl_black = latextools.latex_to_png_mpl(latex_string, False, 'Black') + nt.assert_equal(mpl_default, mpl_black) + nt.assert_in(default_value, [dvipng_black, mpl_black]) + + # Test that dvips name colors can be used without error + dvipng_maroon = latextools.latex_to_png_dvipng(latex_string, False, 'Maroon') + # And that it doesn't return the black one + nt.assert_not_equal(dvipng_black, dvipng_maroon) + + mpl_maroon = latextools.latex_to_png_mpl(latex_string, False, 'Maroon') + nt.assert_not_equal(mpl_black, mpl_maroon)