Attachment 'sagetex.py'
Download 1 """
2 %%
3 %% This is file `sagetex.py',
4 %% generated with the docstrip utility.
5 %%
6 %% The original source files were:
7 %%
8 %% sagetexpackage.dtx (with options: `python')
9 %%
10 %% This is a generated file.
11 %%
12 %% Copyright (C) 2008 by Dan Drake <ddrake@member.ams.org>
13 %%
14 %% This program is free software: you can redistribute it and/or modify it
15 %% under the terms of the GNU General Public License as published by the
16 %% Free Software Foundation, either version 2 of the License, or (at your
17 %% option) any later version.
18 %%
19 %% This program is distributed in the hope that it will be useful, but
20 %% WITHOUT ANY WARRANTY; without even the implied warranty of
21 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 %% General Public License for more details.
23 %%
24 %% You should have received a copy of the GNU General Public License along
25 %% with this program. If not, see <http://www.gnu.org/licenses/>
26 %%
27 """
28 import sys
29 if __name__ == "__main__":
30 print("""This file is part of the SageTeX package.
31 It is not meant to be called directly.
32
33 This file will be used by Sage scripts generated from a LaTeX document
34 using the sagetex package. Keep it somewhere where Sage and Python can
35 find it and it will automatically be imported.""")
36 sys.exit()
37 from sage.misc.latex import latex
38 import os
39 import os.path
40 import hashlib
41 import traceback
42 import subprocess
43 import shutil
44 initplot_done = False
45 dirname = None
46 filename = ""
47 def progress(t,linebreak=True):
48 if linebreak:
49 print(t)
50 else:
51 sys.stdout.write(t)
52 def openout(f):
53 global filename
54 filename = f
55 global _file_
56 _file_ = open(f + '.sout.tmp', 'w')
57 s = '% This file was *autogenerated* from the file ' + \
58 os.path.splitext(filename)[0] + '.sage.\n'
59 _file_.write(s)
60 progress('Processing Sage code for %s.tex...' % filename)
61 def initplot(f):
62 global initplot_done
63 if not initplot_done:
64 progress('Initializing plots directory')
65 global dirname
66 dirname = 'sage-plots-for-' + f + '.tex'
67 if os.path.isdir(dirname):
68 shutil.rmtree(dirname)
69 os.mkdir(dirname)
70 initplot_done = True
71 def inline(counter, s):
72 progress('Inline formula %s' % counter)
73 _file_.write('\\newlabel{@sagelabel' + str(counter) + '}{{' + \
74 latex(s) + '}{}{}{}{}}\n')
75 def blockbegin():
76 progress('Code block begin...', False)
77 def blockend():
78 progress('end')
79 def plot(counter, p, format='notprovided', epsmagick=False, **kwargs):
80 global dirname
81 progress('Plot %s' % counter)
82 if format == 'notprovided':
83 formats = ['eps', 'pdf']
84 else:
85 formats = [format]
86 for fmt in formats:
87 plotfilename = os.path.join(dirname, 'plot-%s.%s' % (counter, fmt))
88 #print(' plotting %s with args %s' % (plotfilename, kwargs))
89 p.save(filename=plotfilename, **kwargs)
90 if format != 'notprovided' and epsmagick is True:
91 print('Calling Imagemagick to convert plot-%s.%s to EPS' % \
92 (counter, format))
93 toeps(counter, format)
94 def toeps(counter, ext):
95 global dirname
96 subprocess.check_call(['convert',\
97 '%s/plot-%s.%s' % (dirname, counter, ext), \
98 '%s/plot-%s.eps' % (dirname, counter)])
99 def goboom(line):
100 global filename
101 print('\n**** Error in Sage code on line %s of %s.tex! Traceback\
102 follows.' % (line, filename))
103 traceback.print_exc()
104 print('\n**** Running Sage on %s.sage failed! Fix %s.tex and try\
105 again.' % (filename, filename))
106 os.remove(filename + '.sout.tmp')
107 sys.exit(1)
108 def endofdoc():
109 global filename
110 sagef = open(filename + '.sage', 'r')
111 m = hashlib.md5()
112 for line in sagef:
113 if line[0:15] != ' sagetex.goboom':
114 m.update(line)
115 s = '%' + m.hexdigest() + '% md5sum of .sage file (minus "goboom" \
116 lines) that produced this\n'
117 _file_.write(s)
118 _file_.close()
119 os.rename(filename + '.sout.tmp', filename + '.sout')
120 progress('Sage processing complete. Run LaTeX on %s.tex again.' %\
121 filename)
122 """
123 \endinput
124 %%
125 %% End of file `sagetex.py'.
126 """
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.