Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f04aa79

Browse files
committed
added the ability to insert arbitrary statements into
the latex preamble to customize usetex output. svn path=/trunk/matplotlib/; revision=3276
1 parent b4237c5 commit f04aa79

5 files changed

Lines changed: 70 additions & 11 deletions

File tree

lib/matplotlib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@ def __call__(self, s):
761761
# text props
762762
'text.color' : ['k', validate_color], # black
763763
'text.usetex' : [False, validate_usetex],
764+
'text.latex.preamble': ['', validate_comma_sep_str],
764765
'text.dvipnghack' : [False, validate_bool],
765766
'text.fontstyle' : ['normal', str],
766767
'text.fontangle' : ['normal', str],

lib/matplotlib/backends/backend_ps.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,9 +1232,11 @@ def _print_figure_tex(self, outfile, dpi, facecolor, edgecolor, orientation,
12321232
paper will be used to prevent clipping.'%(papertype, temp_papertype), 'helpful')
12331233

12341234
texmanager = renderer.get_texmanager()
1235+
font_preamble = texmanager.get_font_preamble()
1236+
custom_preamble = texmanager.get_custom_preamble()
12351237

1236-
convert_psfrags(tmpfile, renderer.psfrag,texmanager.get_font_preamble(),
1237-
paperWidth, paperHeight, orientation)
1238+
convert_psfrags(tmpfile, renderer.psfrag, font_preamble,
1239+
custom_preamble, paperWidth, paperHeight, orientation)
12381240

12391241
if rcParams['ps.usedistiller'] == 'ghostscript':
12401242
gs_distill(tmpfile, ext=='.eps', ptype=papertype, bbox=bbox)
@@ -1249,8 +1251,8 @@ def _print_figure_tex(self, outfile, dpi, facecolor, edgecolor, orientation,
12491251
print >>outfile, fh.read()
12501252
else: shutil.move(tmpfile, outfile)
12511253

1252-
def convert_psfrags(tmpfile, psfrags, font_preamble, paperWidth, paperHeight,
1253-
orientation):
1254+
def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
1255+
paperWidth, paperHeight, orientation):
12541256
"""
12551257
When we want to use the LaTeX backend with postscript, we write PSFrag tags
12561258
to a temporary postscript file, each one marking a position for LaTeX to
@@ -1272,6 +1274,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, paperWidth, paperHeight,
12721274

12731275
print >>latexh, r"""\documentclass{article}
12741276
%s
1277+
%s
12751278
\usepackage[dvips, papersize={%sin,%sin}, body={%sin,%sin}, margin={0in,0in}]{geometry}
12761279
\usepackage{psfrag}
12771280
\usepackage[dvips]{graphicx}
@@ -1285,7 +1288,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, paperWidth, paperHeight,
12851288
\includegraphics*[angle=%s]{%s}
12861289
\end{figure}
12871290
\end{document}
1288-
"""% (font_preamble, paperWidth, paperHeight, paperWidth, paperHeight,
1291+
"""% (font_preamble, custom_preamble, paperWidth, paperHeight, paperWidth, paperHeight,
12891292
'\n'.join(psfrags), angle, os.path.split(epsfile)[-1])
12901293
latexh.close()
12911294

lib/matplotlib/mpl-data/matplotlibrc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,28 @@ units : True
121121
# information on text properties
122122

123123
#text.color : black
124-
#text.usetex : False # use latex for all text handling. For more information, see
125-
# http://www.scipy.org/Wiki/Cookbook/Matplotlib/UsingTex
124+
125+
### LaTeX customizations. See http://www.scipy.org/Wiki/Cookbook/Matplotlib/UsingTex
126+
#text.usetex : False # use latex for all text handling. The following fonts
127+
# are supported through the usual rc parameter settings:
128+
# new century schoolbook, bookman, times, palatino,
129+
# zapf chancery, charter, serif, sans-serif, helvetica,
130+
# avant garde, courier, monospace, computer modern roman,
131+
# computer modern sans serif, computer modern typewriter
132+
# If another font is desired which can loaded using the
133+
# LaTeX \usepackage command, please inquire at the
134+
# matplotlib mailing list
135+
#text.latex.preamble : # IMPROPER USE OF THIS FEATURE WILL LEAD TO LATEX FAILURES
136+
# AND IS THEREFORE UNSUPPORTED. PLEASE DO NOT ASK FOR HELP
137+
# IF THIS FEATURE DOES NOT DO WHAT YOU EXPECT IT TO.
138+
# preamble is a comma separated list of LaTeX statements
139+
# that are included in the LaTeX document preamble.
140+
# An example:
141+
# text.latex.preamble : \usepackage{bm},\usepackage{euler}
142+
# The following packages are always loaded with usetex, so
143+
# beware of package collisions: color, geometry, graphicx,
144+
# type1cm, textcomp. Adobe Postscript (PSSNFS) font packages
145+
# may also be loaded, depending on your font settings
126146
#text.dvipnghack : False # some versions of dvipng don't handle
127147
# alpha channel properly. Use True to correct and flush
128148
# ~/.matplotlib/tex.cache before testing

lib/matplotlib/texmanager.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949

5050
def get_dvipng_version():
51-
stdin, stdout = os.popen4('dvipng --version')
51+
stdin, stdout = os.popen4('dvipng -version')
5252
for line in stdout:
5353
if line.startswith('dvipng '):
5454
version = line.split()[-1]
@@ -172,6 +172,18 @@ def get_font_config(self):
172172

173173
def get_font_preamble(self):
174174
return self._font_preamble
175+
176+
def get_custom_preamble(self):
177+
custom_preamble = '\n'.join(rcParams['text.latex.preamble'])
178+
if custom_preamble is not '':
179+
verbose.report("""
180+
***************************************************************************
181+
You have the following UNSUPPORTED LaTeX preamble customizations:
182+
%s
183+
Please do not ask for support with these customizations active.
184+
***************************************************************************
185+
"""%custom_preamble, 'helpful')
186+
return '\n'.join(rcParams['text.latex.preamble'])
175187

176188
def get_shell_cmd(self, *args):
177189
"""
@@ -189,18 +201,21 @@ def make_tex(self, tex, fontsize):
189201
basefile = self.get_basefile(tex, fontsize)
190202
texfile = '%s.tex'%basefile
191203
fh = file(texfile, 'w')
204+
custom_preamble = self.get_custom_preamble()
192205
fontcmd = {'sans-serif' : r'{\sffamily %s}',
193206
'monospace' : r'{\ttfamily %s}'}.get(self.font_family,
194207
r'{\rmfamily %s}')
195208
tex = fontcmd % tex
209+
196210
s = r"""\documentclass{article}
197211
%s
212+
%s
198213
\usepackage[papersize={72in,72in}, body={70in,70in}, margin={1in,1in}]{geometry}
199214
\pagestyle{empty}
200215
\begin{document}
201216
\fontsize{%f}{%f}%s
202217
\end{document}
203-
""" % (self._font_preamble, fontsize, fontsize*1.25, tex)
218+
""" % (self._font_preamble, custom_preamble, fontsize, fontsize*1.25, tex)
204219
fh.write(s)
205220
fh.close()
206221

matplotlibrc.template

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,28 @@ units : True
121121
# information on text properties
122122

123123
#text.color : black
124-
#text.usetex : False # use latex for all text handling. For more information, see
125-
# http://www.scipy.org/Wiki/Cookbook/Matplotlib/UsingTex
124+
125+
### LaTeX customizations. See http://www.scipy.org/Wiki/Cookbook/Matplotlib/UsingTex
126+
#text.usetex : False # use latex for all text handling. The following fonts
127+
# are supported through the usual rc parameter settings:
128+
# new century schoolbook, bookman, times, palatino,
129+
# zapf chancery, charter, serif, sans-serif, helvetica,
130+
# avant garde, courier, monospace, computer modern roman,
131+
# computer modern sans serif, computer modern typewriter
132+
# If another font is desired which can loaded using the
133+
# LaTeX \usepackage command, please inquire at the
134+
# matplotlib mailing list
135+
#text.latex.preamble : # IMPROPER USE OF THIS FEATURE WILL LEAD TO LATEX FAILURES
136+
# AND IS THEREFORE UNSUPPORTED. PLEASE DO NOT ASK FOR HELP
137+
# IF THIS FEATURE DOES NOT DO WHAT YOU EXPECT IT TO.
138+
# preamble is a comma separated list of LaTeX statements
139+
# that are included in the LaTeX document preamble.
140+
# An example:
141+
# text.latex.preamble : \usepackage{bm},\usepackage{euler}
142+
# The following packages are always loaded with usetex, so
143+
# beware of package collisions: color, geometry, graphicx,
144+
# type1cm, textcomp. Adobe Postscript (PSSNFS) font packages
145+
# may also be loaded, depending on your font settings
126146
#text.dvipnghack : False # some versions of dvipng don't handle
127147
# alpha channel properly. Use True to correct and flush
128148
# ~/.matplotlib/tex.cache before testing

0 commit comments

Comments
 (0)