Skip to content

Commit b07fd4e

Browse files
committed
Make scripts work when Latex output is not in the same directory as .tex
1 parent 6726269 commit b07fd4e

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

scripts.dtx

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,24 @@ def run(args):
148148
else:
149149
src = os.path.splitext(src)[0]
150150

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+
151155
usepackage = r'usepackage(\[.*\])?{sagetex}'
152156
uses_sagetex = False
153157

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
160169

161170
if not uses_sagetex:
162171
print(src + ".tex doesn't seem to use SageTeX, exiting.", file=sys.stderr)
@@ -248,6 +257,8 @@ def argparser(p):
248257
p.add_argument('outputfile', nargs='?', default=None, help="Output file name")
249258
p.add_argument('-o', '--overwrite', action="store_true", default=False,
250259
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")
251262
252263
def run(args):
253264
src, dst, overwrite = args.inputfile, args.outputfile, args.overwrite
@@ -258,11 +269,13 @@ def run(args):
258269
sys.exit(1)
259270
260271
src, ext = os.path.splitext(src)
272+
texfn = src + '.tex'
273+
soutfn = args.sout if args.sout is not None else src + '.sagetex.sout'
261274
% \end{macrocode}
262275
% All the real work gets done in the line below. Sorry it's not more
263276
% exciting-looking.
264277
% \begin{macrocode}
265-
desagetexed = DeSageTex(src)
278+
desagetexed = DeSageTex(texfn, soutfn)
266279
% \end{macrocode}
267280
% This part is cool: we need double percent signs at the beginning of
268281
% the line because Python needs them (so they get turned into single
@@ -332,7 +345,7 @@ def run(args):
332345
sys.exit(1)
333346
334347
src, ext = os.path.splitext(src)
335-
sagecode = SageCodeExtractor(src)
348+
sagecode = SageCodeExtractor(src + '.tex')
336349
header = ("# This file contains Sage code extracted from %s%s.\n"
337350
"# Processed %s.\n"
338351
"" % (src, ext, time.strftime('%a %d %b %Y %H:%M:%S', time.localtime())))
@@ -366,6 +379,7 @@ if __name__ == "__main__":
366379
% over the screen.
367380
% \begin{macrocode}
368381
import sys
382+
import os
369383
from pyparsing import *
370384
% \end{macrocode}
371385
% First, we define this very helpful parser: it finds the matching
@@ -451,11 +465,11 @@ class SoutParser():
451465
% that the provided |fn| is just a basename.
452466
% \begin{macrocode}
453467
class DeSageTex():
454-
def __init__(self, fn):
468+
def __init__(self, texfn, soutfn):
455469
self.sagen = 0
456470
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)
459473
% \end{macrocode}
460474
% Parse |\sage| macros. We just need to pull in the result from the
461475
% |.sout| file and increment the counter---that's what |self.sage| does.
@@ -517,7 +531,7 @@ class DeSageTex():
517531
% |transformString| on it, since that will just pick out the interesting
518532
% bits and munge them according to the above definitions.
519533
% \begin{macrocode}
520-
str = ''.join(open(fn + '.tex', 'r').readlines())
534+
str = ''.join(open(texfn, 'r').readlines())
521535
self.result = doit.transformString(str)
522536
% \end{macrocode}
523537
% That's the end of the class constructor, and it's all we need to do
@@ -551,7 +565,7 @@ class DeSageTex():
551565
% Sage.
552566
% \begin{macrocode}
553567
class SageCodeExtractor():
554-
def __init__(self, fn):
568+
def __init__(self, texfn):
555569
smacro = sagemacroparser
556570
smacro.setParseAction(self.macroout)
557571
@@ -580,7 +594,7 @@ class SageCodeExtractor():
580594
581595
doit = smacro | splot | senv | spause | sunpause
582596
583-
str = ''.join(open(fn + '.tex', 'r').readlines())
597+
str = ''.join(open(texfn, 'r').readlines())
584598
self.result = ''
585599
586600
doit.transformString(str)

0 commit comments

Comments
 (0)