Skip to content

Commit 6fb60f9

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

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
@@ -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)
@@ -153,6 +162,8 @@ def argparser():
153162
p.add_argument('outputfile', nargs='?', default=None, help="Output file name")
154163
p.add_argument('-o', '--overwrite', action="store_true", default=False,
155164
help="Overwrite output file if it exists")
165+
p.add_argument('-s', '--sout', action="store", default=None,
166+
help="Location of the .sagetex.sout file")
156167
return p
157168
158169
def run(args):
@@ -164,11 +175,13 @@ def run(args):
164175
sys.exit(1)
165176
166177
src, ext = os.path.splitext(src)
178+
texfn = src + '.tex'
179+
soutfn = args.sout if args.sout is not None else src + '.sagetex.sout'
167180
% \end{macrocode}
168181
% All the real work gets done in the line below. Sorry it's not more
169182
% exciting-looking.
170183
% \begin{macrocode}
171-
desagetexed = DeSageTex(src)
184+
desagetexed = DeSageTex(texfn, soutfn)
172185
% \end{macrocode}
173186
% This part is cool: we need double percent signs at the beginning of
174187
% the line because Python needs them (so they get turned into single
@@ -236,7 +249,7 @@ def run(args):
236249
sys.exit(1)
237250
238251
src, ext = os.path.splitext(src)
239-
sagecode = SageCodeExtractor(src)
252+
sagecode = SageCodeExtractor(src + '.tex')
240253
header = ("# This file contains Sage code extracted from %s%s.\n"
241254
"# Processed %s.\n"
242255
"" % (src, ext, time.strftime('%a %d %b %Y %H:%M:%S', time.localtime())))
@@ -269,6 +282,7 @@ if __name__ == "__main__":
269282
% over the screen.
270283
% \begin{macrocode}
271284
import sys
285+
import os
272286
from pyparsing import *
273287
% \end{macrocode}
274288
% First, we define this very helpful parser: it finds the matching
@@ -354,11 +368,11 @@ class SoutParser():
354368
% that the provided |fn| is just a basename.
355369
% \begin{macrocode}
356370
class DeSageTex():
357-
def __init__(self, fn):
371+
def __init__(self, texfn, soutfn):
358372
self.sagen = 0
359373
self.plotn = 0
360-
self.fn = fn
361-
self.sout = SoutParser(fn + '.sagetex.sout')
374+
self.fn = os.path.basename(texfn)
375+
self.sout = SoutParser(soutfn)
362376
% \end{macrocode}
363377
% Parse |\sage| macros. We just need to pull in the result from the
364378
% |.sout| file and increment the counter---that's what |self.sage| does.
@@ -420,7 +434,7 @@ class DeSageTex():
420434
% |transformString| on it, since that will just pick out the interesting
421435
% bits and munge them according to the above definitions.
422436
% \begin{macrocode}
423-
str = ''.join(open(fn + '.tex', 'r').readlines())
437+
str = ''.join(open(texfn, 'r').readlines())
424438
self.result = doit.transformString(str)
425439
% \end{macrocode}
426440
% That's the end of the class constructor, and it's all we need to do
@@ -454,7 +468,7 @@ class DeSageTex():
454468
% Sage.
455469
% \begin{macrocode}
456470
class SageCodeExtractor():
457-
def __init__(self, fn):
471+
def __init__(self, texfn):
458472
smacro = sagemacroparser
459473
smacro.setParseAction(self.macroout)
460474
@@ -483,7 +497,7 @@ class SageCodeExtractor():
483497
484498
doit = smacro | splot | senv | spause | sunpause
485499
486-
str = ''.join(open(fn + '.tex', 'r').readlines())
500+
str = ''.join(open(texfn, 'r').readlines())
487501
self.result = ''
488502
489503
doit.transformString(str)

0 commit comments

Comments
 (0)