@@ -148,15 +148,24 @@ def run(args):
148
148
else:
149
149
src = os.path.splitext(src)[0]
150
150
151
+ # Ensure results are output in the same directory as the source files
152
+ os.chdir(os.path.dirname(src))
153
+ src = os.path.basename(src)
154
+
151
155
usepackage = r'usepackage(\[ .*\] )?{sagetex}'
152
156
uses_sagetex = False
153
157
154
- # if it does not use sagetex, obviously running sage is unnecessary
155
- with open(src + '.tex' ) as texf:
156
- for line in texf:
157
- if line.strip().startswith(r'\usepackage ') and re.search(usepackage, line):
158
- uses_sagetex = True
159
- break
158
+ # If it does not use sagetex, obviously running sage is unnecessary.
159
+ if os.path.isfile(src + '.tex' ):
160
+ with open(src + '.tex' ) as texf:
161
+ for line in texf:
162
+ if line.strip().startswith(r'\usepackage ') and re.search(usepackage, line):
163
+ uses_sagetex = True
164
+ break
165
+ else:
166
+ # The .tex file might not exist if LaTeX output was put to a different
167
+ # directory, so in that case just assume we need to build.
168
+ uses_sagetex = True
160
169
161
170
if not uses_sagetex:
162
171
print(src + ".tex doesn't seem to use SageTeX, exiting." , file=sys.stderr)
@@ -248,6 +257,8 @@ def argparser(p):
248
257
p.add_argument('outputfile', nargs='?', default=None, help="Output file name")
249
258
p.add_argument('-o', '--overwrite' , action="store_true", default=False,
250
259
help="Overwrite output file if it exists")
260
+ p.add_argument('-s', '--sout' , action="store", default=None,
261
+ help="Location of the .sagetex.sout file")
251
262
252
263
def run(args):
253
264
src, dst, overwrite = args.inputfile, args.outputfile, args.overwrite
@@ -258,11 +269,13 @@ def run(args):
258
269
sys.exit(1)
259
270
260
271
src, ext = os.path.splitext(src)
272
+ texfn = src + '.tex'
273
+ soutfn = args.sout if args.sout is not None else src + '.sagetex.sout'
261
274
% \end{macrocode}
262
275
% All the real work gets done in the line below. Sorry it's not more
263
276
% exciting-looking.
264
277
% \begin{macrocode}
265
- desagetexed = DeSageTex(src )
278
+ desagetexed = DeSageTex(texfn, soutfn )
266
279
% \end{macrocode}
267
280
% This part is cool: we need double percent signs at the beginning of
268
281
% the line because Python needs them (so they get turned into single
@@ -332,7 +345,7 @@ def run(args):
332
345
sys.exit(1)
333
346
334
347
src, ext = os.path.splitext(src)
335
- sagecode = SageCodeExtractor(src)
348
+ sagecode = SageCodeExtractor(src + '.tex' )
336
349
header = ("# This file contains Sage code extracted from % s%s.\n"
337
350
"# Processed %s.\n"
338
351
"" % (src, ext, time.strftime('%a %d %b %Y %H:%M:%S', time.localtime())))
@@ -366,6 +379,7 @@ if __name__ == "__main__":
366
379
% over the screen.
367
380
% \begin{macrocode}
368
381
import sys
382
+ import os
369
383
from pyparsing import *
370
384
% \end{macrocode}
371
385
% First, we define this very helpful parser: it finds the matching
@@ -451,11 +465,11 @@ class SoutParser():
451
465
% that the provided |fn| is just a basename.
452
466
% \begin{macrocode}
453
467
class DeSageTex():
454
- def __init__(self, fn ):
468
+ def __init__(self, texfn, soutfn ):
455
469
self.sagen = 0
456
470
self.plotn = 0
457
- self.fn = fn
458
- self.sout = SoutParser(fn + '.sagetex.sout' )
471
+ self.fn = os.path.basename(texfn)
472
+ self.sout = SoutParser(soutfn )
459
473
% \end{macrocode}
460
474
% Parse |\sage| macros. We just need to pull in the result from the
461
475
% |.sout| file and increment the counter---that's what |self.sage| does.
@@ -517,7 +531,7 @@ class DeSageTex():
517
531
% |transformString| on it, since that will just pick out the interesting
518
532
% bits and munge them according to the above definitions.
519
533
% \begin{macrocode}
520
- str = '' .join(open(fn + '.tex' , 'r' ).readlines())
534
+ str = '' .join(open(texfn , 'r' ).readlines())
521
535
self.result = doit.transformString(str)
522
536
% \end{macrocode}
523
537
% That's the end of the class constructor, and it's all we need to do
@@ -551,7 +565,7 @@ class DeSageTex():
551
565
% Sage.
552
566
% \begin{macrocode}
553
567
class SageCodeExtractor():
554
- def __init__(self, fn ):
568
+ def __init__(self, texfn ):
555
569
smacro = sagemacroparser
556
570
smacro.setParseAction(self.macroout)
557
571
@@ -580,7 +594,7 @@ class SageCodeExtractor():
580
594
581
595
doit = smacro | splot | senv | spause | sunpause
582
596
583
- str = '' .join(open(fn + '.tex' , 'r' ).readlines())
597
+ str = '' .join(open(texfn , 'r' ).readlines())
584
598
self.result = ''
585
599
586
600
doit.transformString(str)
0 commit comments