-
Notifications
You must be signed in to change notification settings - Fork 41
/
texBuild.py
210 lines (183 loc) · 6.21 KB
/
texBuild.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/opt/conda/bin/python
welcome = '''
################
### texBuild ###
################
Helper file to automate the compilation of both .otl (through otl2latex) and
.tex files
### For .tex files ###
1. Checks weather you have a bibliography included in the .tex file
2. Compiles the appropriate number of times and includes bibtex in the
compilation
### For .otl files ###
Helper file to expedite the use of otl2latex for building presentations in
Beamer. It performs the following tasks:
1. Checks for the presence of four spaces instead of actual tabs in the file.
This will generate a warning and stop the script.
2. Allows the user to subset the presentation by inserting the strings
'@start@' and '@end@' (on their own lines) at the beginning and end of a block
of the OTL the user wants to compile. This allows for quickly compiling small
portions of the presentation without having to build the entire thing. The
presentation built is called temp.otl, temp.tex and temp.pdf.
3. Compiles the presentation appropriately (i.e. executes pdflatex the correct
number of times) and deletes out all the temporary files created by Beamer.
It only leaves behind the .otl, .tex and .pdf.
Usage:
texbuild file.otl
texbuild file.tex [-minted]
If -minted added, then runs the appropriate command to compile LaTeX
documents that make use of the minted packaged for code-syntax
highlighting
Authors:
David C. Folch <david.folch@asu.edu>
Daniel Arribas-Bel <daniel.arribas.bel@gmail.com>
'''
import os
import sys
nargs=len(sys.argv)
if nargs>1:
file = sys.argv[1]
fileType = file.split('.')[-1]
name = file[:-4]
def subset(test):
'''
create temp.otl which is a subset of the full presentation;
insert '@start@' into main OTL file to begin subset and
'@end@' to end subset
'''
temp = []
for i in test:
if i != '\n':
temp.append(i)
else:
temp.append('\n')
break
start = None
stop = None
for i in range(len(test)):
if '@start@' in test[i]:
start = i+1
if '@end@' in test[i]:
stop = i
break
temp.extend(test[start:stop])
outFile = open('temp.otl','w')
for i in temp:
outFile.write(i)
outFile.close()
def compileOTL(name):
'''
--converts file from otl to Latex
--then runs pdflatex appropriate number of times
--deletes out intermediate files created by Beamer
--finally, opens the PDF file
'''
os.system('python set_folder_path_here/otl2latexR18.py -p ' + name + '.otl ' + name + '.tex')
os.system('pdflatex ' + name)
os.system('pdflatex ' + name)
os.system('pdflatex ' + name)
os.system('rm ' + name + '.toc')
os.system('rm ' + name + '.aux')
os.system('rm ' + name + '.log')
os.system('rm ' + name + '.snm')
os.system('rm ' + name + '.nav')
os.system('rm ' + name + '.out')
os.system('rm ' + name + '.lot')
os.system('rm ' + name + '.lof')
os.system('open ' + name + '.pdf')
def bibGetter(file):
bibFile=None
texLines=open(file)
texLines=texLines.readlines()
for line in texLines:
if '\\bibliography{' in line:
beg=None
end=None
for i in range(len(line)):
if line[i]=='{':
beg=i+1
if line[i]=='}':
end=i
bibFile=line[beg:end]
return bibFile
def bibCheck(file):
bibYes=0
texLines=open(file)
texLines=texLines.readlines()
for line in texLines:
if '\\bibliography{' in line:
bibYes=1
return bibYes
def compileTEX(name):
'''
--runs pdflatex
--runs bibtex
--runs twive pdflatex
--deletes out side-files created when compiling the tex file
'''
os.system('pdflatex ' + name)
if bibCheck(file):
os.system('bibtex ' + name)
os.system('pdflatex ' + name)
os.system('pdflatex ' + name)
os.system('rm ' + name + '.aux')
os.system('rm ' + name + '.bbl')
os.system('rm ' + name + '.blg')
os.system('rm ' + name + '.log')
os.system('rm ' + name + '.out')
os.system('rm ' + name + '.toc')
os.system('rm ' + name + '.spl')
os.system('rm ' + name + '.lot')
os.system('rm ' + name + '.lof')
os.system('rm ' + name + '.nav')
os.system('rm ' + name + '.snm')
os.system('open ' + name + '.pdf')
def compileTEXminted(name):
os.system('pdflatex -shell-escape ' + name)
os.system('pdflatex -shell-escape ' + name)
os.system('rm ' + name + '.aux')
os.system('rm ' + name + '.bbl')
os.system('rm ' + name + '.blg')
os.system('rm ' + name + '.log')
os.system('rm ' + name + '.out')
os.system('rm ' + name + '.toc')
os.system('open ' + name + '.pdf')
if nargs==1:
print(welcome)
else:
if fileType=='otl':
test = open(file)
test = test.readlines()
for i in test:
# check for four-spaces instead of \t in the OTL file
if ' ' in i:
print('\n')
print('****************************************')
print('Fix the tabs')
print('****************************************')
print('\n')
break
else:
# check to see if the user wants to subset to presentation
for i in test:
if '@start@' in i:
subset(test)
compileOTL('temp')
break
# run the full presentation as the default
else:
compileOTL(name)
if fileType=='tex':
if sys.argv[-1] == '-minted':
compileTEXminted(name)
print('Run with compileTEXminted')
else:
compileTEX(name)
print('Run with compileTEX')
if nargs>1:
if fileType=='tex':
if bibCheck(file):
print('The bibliography used may be found in ',bibGetter(file),'\n')
else:
print('\n There is no bibliography to use with bibTeX \n')
print('\n Your ',fileType,' file has been processed successfully!!!\n')