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

Skip to content

Commit 6f5da53

Browse files
committed
add unicode latex support
svn path=/trunk/matplotlib/; revision=3279
1 parent 1c03449 commit 6f5da53

6 files changed

Lines changed: 54 additions & 6 deletions

File tree

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2007-05-18 Added LaTeX unicode support. Enable with the
2+
'text.latex.unicode' rcParam. This requires the ucs and
3+
inputenc LaTeX packages. - ADS
4+
15
2007-04-23 Fixed some problems with polar -- dded general polygon
26
clipping to clip the lines a nd grids to the polar axes.
37
Added support for set_rmax to easily change the maximum

lib/matplotlib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@ def __call__(self, s):
775775
# text props
776776
'text.color' : ['k', validate_color], # black
777777
'text.usetex' : [False, validate_usetex],
778+
'text.latex.unicode': [False, validate_bool],
778779
'text.latex.preamble': ['', validate_latex_preamble],
779780
'text.dvipnghack' : [False, validate_bool],
780781
'text.fontstyle' : ['normal', str],

lib/matplotlib/backends/backend_ps.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,14 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
12721272
if orientation=='landscape': angle = 90
12731273
else: angle = 0
12741274

1275-
print >>latexh, r"""\documentclass{article}
1275+
if rcParams['text.latex.unicode']:
1276+
unicode_preamble = """\usepackage{ucs}
1277+
\usepackage[utf8x]{inputenc}"""
1278+
else:
1279+
unicode_preamble = ''
1280+
1281+
s = r"""\documentclass{article}
1282+
%s
12761283
%s
12771284
%s
12781285
\usepackage[dvips, papersize={%sin,%sin}, body={%sin,%sin}, margin={0in,0in}]{geometry}
@@ -1288,8 +1295,21 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
12881295
\includegraphics*[angle=%s]{%s}
12891296
\end{figure}
12901297
\end{document}
1291-
"""% (font_preamble, custom_preamble, paperWidth, paperHeight, paperWidth, paperHeight,
1292-
'\n'.join(psfrags), angle, os.path.split(epsfile)[-1])
1298+
"""% (font_preamble, unicode_preamble, custom_preamble, paperWidth, paperHeight,
1299+
paperWidth, paperHeight,
1300+
'\n'.join(psfrags), angle, os.path.split(epsfile)[-1])
1301+
1302+
if rcParams['text.latex.unicode']:
1303+
latexh.write(s.encode('utf8'))
1304+
else:
1305+
try:
1306+
latexh.write(s)
1307+
except UnicodeEncodeError, err:
1308+
verbose.report("You are using unicode and latex, but have "
1309+
"not enabled the matplotlib 'text.latex.unicode' "
1310+
"rcParam.", 'helpful')
1311+
raise
1312+
12931313
latexh.close()
12941314

12951315
# the split drive part of the command is necessary for windows users with

lib/matplotlib/mpl-data/matplotlibrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ units : True
132132
# If another font is desired which can loaded using the
133133
# LaTeX \usepackage command, please inquire at the
134134
# matplotlib mailing list
135+
#text.latex.unicode : False # use "ucs" and "inputenc" LaTeX packages for handling
136+
# unicode strings.
135137
#text.latex.preamble : # IMPROPER USE OF THIS FEATURE WILL LEAD TO LATEX FAILURES
136138
# AND IS THEREFORE UNSUPPORTED. PLEASE DO NOT ASK FOR HELP
137139
# IF THIS FEATURE DOES NOT DO WHAT YOU EXPECT IT TO.

lib/matplotlib/texmanager.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ def __init__(self):
165165
def get_basefile(self, tex, fontsize, dpi=None):
166166
s = tex + self._fontconfig + ('%f'%fontsize) + self.get_custom_preamble()
167167
if dpi: s += ('%s'%dpi)
168-
return os.path.join(self.texcache, md5.md5(s).hexdigest())
168+
bytes = unicode(s).encode('utf-8') # make sure hash is consistent for all strings, regardless of encoding
169+
return os.path.join(self.texcache, md5.md5(bytes).hexdigest())
169170

170171
def get_font_config(self):
171172
return self._fontconfig
@@ -197,17 +198,35 @@ def make_tex(self, tex, fontsize):
197198
'monospace' : r'{\ttfamily %s}'}.get(self.font_family,
198199
r'{\rmfamily %s}')
199200
tex = fontcmd % tex
201+
202+
if rcParams['text.latex.unicode']:
203+
unicode_preamble = """\usepackage{ucs}
204+
\usepackage[utf8x]{inputenc}"""
205+
else:
206+
unicode_preamble = ''
200207

201208
s = r"""\documentclass{article}
202209
%s
203210
%s
211+
%s
204212
\usepackage[papersize={72in,72in}, body={70in,70in}, margin={1in,1in}]{geometry}
205213
\pagestyle{empty}
206214
\begin{document}
207215
\fontsize{%f}{%f}%s
208216
\end{document}
209-
""" % (self._font_preamble, custom_preamble, fontsize, fontsize*1.25, tex)
210-
fh.write(s)
217+
""" % (self._font_preamble, unicode_preamble, custom_preamble,
218+
fontsize, fontsize*1.25, tex)
219+
if rcParams['text.latex.unicode']:
220+
fh.write(s.encode('utf8'))
221+
else:
222+
try:
223+
fh.write(s)
224+
except UnicodeEncodeError, err:
225+
verbose.report("You are using unicode and latex, but have "
226+
"not enabled the matplotlib 'text.latex.unicode' "
227+
"rcParam.", 'helpful')
228+
raise
229+
211230
fh.close()
212231

213232
return texfile

matplotlibrc.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ units : True
132132
# If another font is desired which can loaded using the
133133
# LaTeX \usepackage command, please inquire at the
134134
# matplotlib mailing list
135+
#text.latex.unicode : False # use "ucs" and "inputenc" LaTeX packages for handling
136+
# unicode strings.
135137
#text.latex.preamble : # IMPROPER USE OF THIS FEATURE WILL LEAD TO LATEX FAILURES
136138
# AND IS THEREFORE UNSUPPORTED. PLEASE DO NOT ASK FOR HELP
137139
# IF THIS FEATURE DOES NOT DO WHAT YOU EXPECT IT TO.

0 commit comments

Comments
 (0)