Hi,
i know that it's been a while since this question was asked, but since i
struggled with it a bit (and to have a reference for later), here's the
workaround i've come up with:
1. (assuming that this is the file that produces the Matplotlib figure, eg.
$HOME/sagetexInclude.py):
def f():
from numpy import arange
from matplotlib.pyplot import plot
plot(range(10), arange(1.5, 2.5, .1))
2. tweek the file $SAGE_ROOT/local/bin/sage-env (so that is uses PYTHONPATH)*:
" ...
PYTHONPATH="$SAGE_PATH:$SAGE_ROOT/local/lib/python:$PYTHONPATH" && export
PYTHONPATH
...
"
[*Note: now i see that probably setting SAGE_PATH to the appropriate value
would suffice]
3. in the .tex file do something like:
\begin{sagesilent}
import sagetexInclude as sti
import matplotlib.pyplot as plt
f = plt.figure() # so that we get a separate image for the plot
sti.f() # here we do the PLOTting
plt.save = plt.savefig # SageTeX's plotting apparently relies on *this*!
\end{sagesilent}
\begin{center} \sageplot{plt} \end{center} % plot as a normal Sage object
% this is not really necessary, but whatever
\begin{sagesilent} plt.close() \end{sagesilent}
4. generate latex and run sage on the file as usually (don't forget to set
PYTHONPATH or SAGE_PATH to the directory containing sagetexInclude [i.e. export
PYTHONPATH=$HOME:$PYTHONPATH]):
pdflatex texfile.tex
sage texfile.sage
pdflatex texfile.tex
Hope this helps [someone :) ].
Best regards,
Constantin
You're correct that there's nothing *obvious* one can do. But I think
there's something non-obvious...
When you do \sageplot{p} in SageTeX, it doesn't require much of the
object `p'. It basically needs to be able to do
p.save('path/foo.png')
and get a PNG file saved properly. It's very useful for 'foo.eps' and
'foo.pdf' to also work.
So, you don't really need a Sage Graphics() object; you just need
something that has an appropriate save() method. So it's possible that
something like this would work:
class GoodEnoughGraphics():
def __init__(self, obj):
self.obj = obj
def save(filename):
if filename.endswith('png'):
# do whatever you need to do to save a PNG to `filename'
if filename.endswith('eps'):
# do whatever you need to do to save an EPS to `filename'
# etc
So, if you're working with pure matplotlib graphics, you would write the
above class putting whatever specific code you need in .save(), and then
do something like \sageplot{GoodEnoughGraphics(p)}. This approach should
work for any kind of graphics whatsoever; if you can somehow save them
to the right kind of file, then you can write the above wrapper.
I see that the OP effectively did this; see above where he did "plt.save
= plt.savefig", which gives his object the .save() method that SageTeX
wants.
As a disclaimer: I haven't tested any of this, or even looked at the
SageTeX code to make sure it would work. So caveat emptor. :) Let me
know if this works!
Dan
--
--- Dan Drake
----- http://mathsci.kaist.ac.kr/~drake
-------
If mpl included a save method that had the above interface
(foo.save('/path/to/blah.png'), and so on), then I'm pretty sure it
would work in SageTeX with no modifications.