Re: [matplotlib-devel] unicode support for latex

2007-05-18 Thread Andrew Straw
Hi Darren,

I've made the changes you requested and committed it. (I didn't realize
that mpl-data/matplotlibrc was generated from the template -- I actually
already placed a descriptive line of text in the overwritten file.)

I'm attaching a file for the examples/ directory which I'm having
trouble checking in. I'm rushing out the door for a weekend away at the
moment, so if you can check this in, that'd be great, otherwise I'll try
again when I'm back online. I think it might be an issue with the
non-ASCII characters in the file.

I'll also update the wiki page when I get back.

-Andrew

Darren Dale wrote:
 Hi Andrew,
 
 On Thursday 17 May 2007 10:12:09 pm Andrew Straw wrote:
 OK, here's the patch! :)

 Andrew Straw wrote:
 Hi All (esp. Darren),

 The attached patch adds unicode support for LaTeX. Given the recent
 discussion about adding preambles, I thought I'd run it past here first.
 Anyone opposed if I check this in?
 
 No opposition, but a couple requests. Would you add a commented out line to 
 matplotlibrc.template, with some brief discussion if you think it is 
 appropriate? Also, lets make it a policy that the custom preamble always 
 appears after all other preambles in texmanager amd backend_ps.
 
 Note that I specifically added the rcParam text.latex.unicode to enable
 this and a default False value to turn this off by default. I hope this
 prevents breakage for folks who don't have the ucs and inputenc latex
 packages installed while allowing unicode for those of us that do.
 
 I have never used unicode with latex, will you add a nugget to 
 http://www.scipy.org/Cookbook/Matplotlib/UsingTex?
 
 Darren

#!/usr/bin/env python
# -*- coding: latin-1 -*-

This demo is tex_demo.py modified to have unicode. See that file for
more information.

from matplotlib import rcParams
rcParams['text.usetex']=True
rcParams['text.latex.unicode']=True
from matplotlib.numerix import arange, cos, pi
from pylab import figure, axes, plot, xlabel, ylabel, title, \
 grid, savefig, show

figure(1)
ax = axes([0.1, 0.1, 0.8, 0.7])
t = arange(0.0, 1.0+0.01, 0.01)
s = cos(2*2*pi*t)+2
plot(t, s)

xlabel(r'\textbf{time (s)}')
s = unicode(r'\textit{Velocity (°/sec)}','latin-1')
ylabel(unicode(r'\textit{Velocity (°/sec)}','latin-1'),fontsize=16)
title(r\TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!, 
  fontsize=16, color='r')
grid(True)
savefig('tex_demo')


show()
-
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel


Re: [matplotlib-devel] unicode support for latex

2007-05-17 Thread Andrew Straw
OK, here's the patch! :)

Andrew Straw wrote:
 Hi All (esp. Darren),

 The attached patch adds unicode support for LaTeX. Given the recent
 discussion about adding preambles, I thought I'd run it past here first.
 Anyone opposed if I check this in?

 Note that I specifically added the rcParam text.latex.unicode to enable
 this and a default False value to turn this off by default. I hope this
 prevents breakage for folks who don't have the ucs and inputenc latex
 packages installed while allowing unicode for those of us that do.

 -Andrew

 -
 This SF.net email is sponsored by DB2 Express
 Download DB2 Express C - the FREE version of DB2 express and take
 control of your XML. No limits. Just data. Click to get it now.
 http://sourceforge.net/powerbar/db2/
 ___
 Matplotlib-devel mailing list
 Matplotlib-devel@lists.sourceforge.net
 https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
   

Index: CHANGELOG
===
--- CHANGELOG	(revision 3278)
+++ CHANGELOG	(working copy)
@@ -1,3 +1,7 @@
+2007-05-17 Added LaTeX unicode support. Enable with the
+   'text.latex.unicode' rcParam. This requires the ucs and
+   inputenc LaTeX packages. - ADS
+
 2007-04-23 Fixed some problems with polar -- dded general polygon
clipping to clip the lines a nd grids to the polar axes.
Added support for set_rmax to easily change the maximum
Index: lib/matplotlib/__init__.py
===
--- lib/matplotlib/__init__.py	(revision 3278)
+++ lib/matplotlib/__init__.py	(working copy)
@@ -775,6 +775,7 @@
 # text props
 'text.color': ['k', validate_color], # black
 'text.usetex'   : [False, validate_usetex],
+'text.latex.unicode': [False, validate_bool],
 'text.latex.preamble': ['', validate_latex_preamble],
 'text.dvipnghack': [False, validate_bool],
 'text.fontstyle': ['normal', str],
Index: lib/matplotlib/texmanager.py
===
--- lib/matplotlib/texmanager.py	(revision 3278)
+++ lib/matplotlib/texmanager.py	(working copy)
@@ -165,7 +165,8 @@
 def get_basefile(self, tex, fontsize, dpi=None):
 s = tex + self._fontconfig + ('%f'%fontsize) + self.get_custom_preamble()
 if dpi: s += ('%s'%dpi)
-return os.path.join(self.texcache, md5.md5(s).hexdigest())
+bytes = unicode(s).encode('utf-8') # make sure hash is consistent for all strings, regardless of encoding
+return os.path.join(self.texcache, md5.md5(bytes).hexdigest())
 
 def get_font_config(self):
 return self._fontconfig
@@ -197,17 +198,35 @@
'monospace'  : r'{\ttfamily %s}'}.get(self.font_family, 
  r'{\rmfamily %s}')
 tex = fontcmd % tex
+
+if rcParams['text.latex.unicode']:
+unicode_preamble = \usepackage{ucs}
+\usepackage[utf8x]{inputenc}
+else:
+unicode_preamble = ''
 
 s = r\documentclass{article}
 %s
 %s
+%s
 \usepackage[papersize={72in,72in}, body={70in,70in}, margin={1in,1in}]{geometry}
 \pagestyle{empty}
 \begin{document}
 \fontsize{%f}{%f}%s
 \end{document}
- % (self._font_preamble, custom_preamble, fontsize, fontsize*1.25, tex)
-fh.write(s)
+ % (self._font_preamble, custom_preamble, unicode_preamble,
+   fontsize, fontsize*1.25, tex)
+if rcParams['text.latex.unicode']:
+fh.write(s.encode('utf8'))
+else:
+try:
+fh.write(s)
+except UnicodeEncodeError, err:
+verbose.report(You are using unicode and latex, but have 
+   not enabled the matplotlib 'text.latex.unicode' 
+   rcParam., 'helpful')
+raise
+
 fh.close()
 
 return texfile
Index: lib/matplotlib/backends/backend_ps.py
===
--- lib/matplotlib/backends/backend_ps.py	(revision 3278)
+++ lib/matplotlib/backends/backend_ps.py	(working copy)
@@ -1272,9 +1272,16 @@
 if orientation=='landscape': angle = 90
 else: angle = 0
 
-print latexh, r\documentclass{article}
+if rcParams['text.latex.unicode']:
+unicode_preamble = \usepackage{ucs}
+\usepackage[utf8x]{inputenc}
+else:
+unicode_preamble = ''
+
+s = r\documentclass{article}
 %s
 %s
+%s
 \usepackage[dvips, papersize={%sin,%sin}, body={%sin,%sin}, margin={0in,0in}]{geometry}
 \usepackage{psfrag}
 \usepackage[dvips]{graphicx}
@@ -1288,8 +1295,21 @@
 \includegraphics*[angle=%s]{%s}
 \end{figure}
 \end{document}
-% (font_preamble, custom_preamble, paperWidth, paperHeight, paperWidth,