1010import shutil
1111import subprocess
1212from base64 import encodebytes
13+ from textwrap import wrap as splitstring
1314
1415from IPython .utils .process import find_cmd , FindCmdError
1516from traitlets .config import get_config
@@ -55,7 +56,8 @@ def _config_default(self):
5556 ).tag (config = True )
5657
5758
58- def latex_to_png (s , encode = False , backend = None , wrap = False , color = 'Black' ):
59+ def latex_to_png (s , encode = False , backend = None , wrap = False , color = 'Black' ,
60+ scale = 1.0 ):
5961 """Render a LaTeX string to PNG.
6062
6163 Parameters
@@ -69,7 +71,10 @@ def latex_to_png(s, encode=False, backend=None, wrap=False, color='Black'):
6971 wrap : bool
7072 If true, Automatically wrap `s` as a LaTeX equation.
7173 color : string
72- Foreground color name among dvipsnames.
74+ Foreground color name among dvipsnames, e.g. 'Maroon' or on hex RGB
75+ format, e.g. '#AA20FA'.
76+ scale : float
77+ Scale factor for the resulting PNG.
7378
7479 None is returned when the backend cannot be used.
7580
@@ -84,15 +89,25 @@ def latex_to_png(s, encode=False, backend=None, wrap=False, color='Black'):
8489 f = latex_to_png_mpl
8590 elif backend == 'dvipng' :
8691 f = latex_to_png_dvipng
92+ if color .startswith ('#' ):
93+ # Convert hex RGB color to LaTeX RGB color.
94+ if len (color ) == 7 :
95+ try :
96+ color = "RGB {}" .format (" " .join ([str (int (x , 16 )) for x in
97+ splitstring (color [1 :], 2 )]))
98+ except ValueError :
99+ raise ValueError ('Invalid color specification {}.' .format (color ))
100+ else :
101+ raise ValueError ('Invalid color specification {}.' .format (color ))
87102 else :
88103 raise ValueError ('No such backend {0}' .format (backend ))
89- bin_data = f (s , wrap , color )
104+ bin_data = f (s , wrap , color , scale )
90105 if encode and bin_data :
91106 bin_data = encodebytes (bin_data )
92107 return bin_data
93108
94109
95- def latex_to_png_mpl (s , wrap , color = 'Black' ):
110+ def latex_to_png_mpl (s , wrap , color = 'Black' , scale = 1.0 ):
96111 try :
97112 from matplotlib import mathtext
98113 from pyparsing import ParseFatalException
@@ -107,13 +122,14 @@ def latex_to_png_mpl(s, wrap, color='Black'):
107122 try :
108123 mt = mathtext .MathTextParser ('bitmap' )
109124 f = BytesIO ()
110- mt .to_png (f , s , fontsize = 12 , color = color )
125+ dpi = 120 * scale
126+ mt .to_png (f , s , fontsize = 12 , dpi = dpi , color = color )
111127 return f .getvalue ()
112128 except (ValueError , RuntimeError , ParseFatalException ):
113129 return None
114130
115131
116- def latex_to_png_dvipng (s , wrap , color = 'Black' ):
132+ def latex_to_png_dvipng (s , wrap , color = 'Black' , scale = 1.0 ):
117133 try :
118134 find_cmd ('latex' )
119135 find_cmd ('dvipng' )
@@ -133,8 +149,9 @@ def latex_to_png_dvipng(s, wrap, color='Black'):
133149 ["latex" , "-halt-on-error" , "-interaction" , "batchmode" , tmpfile ],
134150 cwd = workdir , stdout = devnull , stderr = devnull )
135151
152+ resolution = round (150 * scale )
136153 subprocess .check_call (
137- ["dvipng" , "-T" , "tight" , "-x " , "1500" , "-z" , "9" ,
154+ ["dvipng" , "-T" , "tight" , "-D " , str ( resolution ) , "-z" , "9" ,
138155 "-bg" , "transparent" , "-o" , outfile , dvifile , "-fg" , color ],
139156 cwd = workdir , stdout = devnull , stderr = devnull )
140157
0 commit comments