@@ -64,15 +64,24 @@ def run(args):
6464 else:
6565 src = os.path.splitext(src)[0]
6666
67+ # Ensure results are output in the same directory as the source files
68+ os.chdir(os.path.dirname(src))
69+ src = os.path.basename(src)
70+
6771 usepackage = r'usepackage(\[ .*\] )?{sagetex}'
6872 uses_sagetex = False
6973
70- # if it does not use sagetex, obviously running sage is unnecessary
71- with open(src + '.tex' ) as texf:
72- for line in texf:
73- if line.strip().startswith(r'\usepackage ') and re.search(usepackage, line):
74- uses_sagetex = True
75- break
74+ # If it does not use sagetex, obviously running sage is unnecessary.
75+ if os.path.isfile(src + '.tex' ):
76+ with open(src + '.tex' ) as texf:
77+ for line in texf:
78+ if line.strip().startswith(r'\usepackage ') and re.search(usepackage, line):
79+ uses_sagetex = True
80+ break
81+ else:
82+ # The .tex file might not exist if LaTeX output was put to a different
83+ # directory, so in that case just assume we need to build.
84+ uses_sagetex = True
7685
7786 if not uses_sagetex:
7887 print(src + ".tex doesn't seem to use SageTeX, exiting." , file=sys.stderr)
@@ -150,6 +159,8 @@ def argparser():
150159 p.add_argument('outputfile', nargs='?', default=None, help="Output file name")
151160 p.add_argument('-o', '--overwrite' , action="store_true", default=False,
152161 help="Overwrite output file if it exists")
162+ p.add_argument('-s', '--sout' , action="store", default=None,
163+ help="Location of the .sagetex.sout file")
153164 return p
154165
155166def run(args):
@@ -161,11 +172,13 @@ def run(args):
161172 sys.exit(1)
162173
163174 src, ext = os.path.splitext(src)
175+ texfn = src + '.tex'
176+ soutfn = args.sout if args.sout is not None else src + '.sagetex.sout'
164177% \end{macrocode}
165178% All the real work gets done in the line below. Sorry it's not more
166179% exciting-looking.
167180% \begin{macrocode}
168- desagetexed = DeSageTex(src )
181+ desagetexed = DeSageTex(texfn, soutfn )
169182% \end{macrocode}
170183% This part is cool: we need double percent signs at the beginning of
171184% the line because Python needs them (so they get turned into single
@@ -233,7 +246,7 @@ def run(args):
233246 sys.exit(1)
234247
235248 src, ext = os.path.splitext(src)
236- sagecode = SageCodeExtractor(src)
249+ sagecode = SageCodeExtractor(src + '.tex' )
237250 header = ("# This file contains Sage code extracted from % s%s.\n"
238251 "# Processed %s.\n"
239252 "" % (src, ext, time.strftime('%a %d %b %Y %H:%M:%S', time.localtime())))
@@ -266,6 +279,7 @@ if __name__ == "__main__":
266279% over the screen.
267280% \begin{macrocode}
268281import sys
282+ import os
269283from pyparsing import *
270284% \end{macrocode}
271285% First, we define this very helpful parser: it finds the matching
@@ -351,11 +365,11 @@ class SoutParser():
351365% that the provided |fn| is just a basename.
352366% \begin{macrocode}
353367class DeSageTex():
354- def __init__(self, fn ):
368+ def __init__(self, texfn, soutfn ):
355369 self.sagen = 0
356370 self.plotn = 0
357- self.fn = fn
358- self.sout = SoutParser(fn + '.sagetex.sout' )
371+ self.fn = os.path.basename(texfn)
372+ self.sout = SoutParser(soutfn )
359373% \end{macrocode}
360374% Parse |\sage| macros. We just need to pull in the result from the
361375% |.sout| file and increment the counter---that's what |self.sage| does.
@@ -417,7 +431,7 @@ class DeSageTex():
417431% |transformString| on it, since that will just pick out the interesting
418432% bits and munge them according to the above definitions.
419433% \begin{macrocode}
420- str = '' .join(open(fn + '.tex' , 'r' ).readlines())
434+ str = '' .join(open(texfn , 'r' ).readlines())
421435 self.result = doit.transformString(str)
422436% \end{macrocode}
423437% That's the end of the class constructor, and it's all we need to do
@@ -451,7 +465,7 @@ class DeSageTex():
451465% Sage.
452466% \begin{macrocode}
453467class SageCodeExtractor():
454- def __init__(self, fn ):
468+ def __init__(self, texfn ):
455469 smacro = sagemacroparser
456470 smacro.setParseAction(self.macroout)
457471
@@ -480,7 +494,7 @@ class SageCodeExtractor():
480494
481495 doit = smacro | splot | senv | spause | sunpause
482496
483- str = '' .join(open(fn + '.tex' , 'r' ).readlines())
497+ str = '' .join(open(texfn , 'r' ).readlines())
484498 self.result = ''
485499
486500 doit.transformString(str)
0 commit comments