-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamtex.py
783 lines (699 loc) · 27.3 KB
/
examtex.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
import numpy as np
import re
import sys
import os
import random
def compile_error(err, traceback=None):
"""Does this really need a docstring."""
print(err, file=sys.stderr)
if traceback:
print("\t", traceback)
sys.exit(1)
def latexify(line):
"""Replace LaTeX-sensitive characters with LaTeX counterparts.
Returns modified line."""
line = line.replace("%", "\\%")
# search for double quoted substrings, replace with ``''
match = re.search("\"[^\"]*\"", line)
while match:
repl = "``{}''".format(match.group(0)[1:-1])
line = line[:match.start()] + repl + line[match.end():]
match = re.search("\"[^\"]*\"", line)
# search for single quoted substrings, replace with `'. Capture
# surrounding whitespace
match = re.search("\\s'[^']+'\\s", line)
while match:
repl = "`{}'".format(match.group(0)[2:-2])
line = line[:match.start()+1] + repl + line[match.end()-1:]
match = re.search("\\s'[^']+'\\s", line)
# custom bold and italics syntax
line = re.sub(r"\\i\s*{", r"\\textit{", line)
line = re.sub(r"\\b\s*{", r"\\textbf{", line)
return line
def process_options(content):
options = {}
for line in content:
try:
key, val = line.split("::")
except ValueError:
compile_error("Options must have 'key:: value' structure.", line)
key = key.strip().lower()
val = [x.strip() for x in val.strip().split(";;")]
options[key] = val
return options
class Section:
def __init__(self, lines):
sep = np.flatnonzero([re.match("-----", l) for l in lines])
if sep.size != 0:
sep = sep[0]
self.options = process_options(lines[:sep])
self.lines = lines[sep+1:]
else:
self.options = {}
self.lines = lines
self.content = []
options = self.options
if "name" in options:
options["name"] = options["name"][0]
else:
options["name"] = None
def gobble(lines):
module_ptrn = r"(?i){(image|text|latex)}\s*$"
bang_ptrn = r"(?i)\s*!(newpage|gap|newcol|hrule)"
cont = lines[0]
if re.match(module_ptrn, cont):
module_type = cont.strip().lower()[1:-1]
end = 1
while end < len(lines) and re.match(r"\s", lines[end]):
end += 1
if end == 1:
compile_error("Empty module.", cont)
cont = lines[1:end]
if module_type == "image":
return Image(cont), lines[end:]
elif module_type == "text":
return Text(cont), lines[end:]
elif module_type == "latex":
return Latex(cont), lines[end:]
elif re.match(bang_ptrn, cont):
return Bang(cont), lines[1:]
return None, lines
def to_tex(self):
return ""
def ans_sheet_tex(self):
return ""
class Cover(Section):
def __init__(self, lines):
Section.__init__(self, lines)
lines = self.lines
while lines:
cont, lines = Cover.gobble(lines)
self.content.append(cont)
def gobble(lines):
cont, lines = Section.gobble(lines)
if cont:
return cont, lines
try:
key, val = lines[0].split('::')
except ValueError:
compile_error("Error in Cover. Each line must have exactly\
one double colon.", lines[0])
val = [v.strip() for v in val.split(";;")]
cont = (key.strip().lower(), val)
return cont, lines[1:]
def to_tex(self):
tex = []
for cont in self.content:
if type(cont) == tuple:
key, val = cont
if key == "title":
title = latexify(val[0])
tex.append("\\begin{center}")
tex.append("\t\\par\\noindent\\textbf{{\\Huge {}}}"
.format(title))
tex.append("\\end{center}")
if key == "subtitle":
subtitle = latexify(val[0])
tex.append("\\begin{center}")
tex.append("\t\\par\\noindent\\textbf{{\\large {}}}"
.format(subtitle))
tex.append("\\end{center}")
if key == "id":
tex.append("\\begin{center}")
tex.append("\t\\par")
tex.append("\t\\def\\arraystretch{2}\\tabcolsep=3pt")
tex.append("\t\\begin{tabular}{r r}")
for v in val:
v = latexify(v)
tex.append("\t\t\\textbf{{{}:}}".format(v)
+ "& \\makebox[4in]{\\hrulefill} \\\\")
tex.append("\t\\end{tabular}")
tex.append("\\end{center}")
if key == "author":
tex.append("\\begin{center}")
tex.append("\t\\par")
tex.append("\t\\def\\arraystretch{1}\\tabcolsep=3pt")
tex.append("\t\\begin{tabular}{r l}")
for i in range(len(val)):
v = latexify(val[i])
firstcol = "\\textbf{{Written by:}}" if i == 0 else ""
tex.append("\t\t {} & {} \\\\".format(firstcol, v))
tex.append("\t\\end{tabular}")
tex.append("\\end{center}")
else:
tex.append(cont.to_tex())
return "\n".join(tex)
class MatchTF(Section):
def gobble(lines):
cont, lines = Section.gobble(lines)
if cont:
return cont, lines
try:
ans, ques = lines[0].split('::')
except ValueError:
compile_error("Error in Match/TF. Each line must have exactly\
one double colon.", lines[0])
cont = (ques.strip(), ans.strip())
return cont, lines[1:]
def to_tex(self):
global qcount
tex = ["\\newpage"]
if self.options["name"]:
tex.append("\\section*{{{}}}".format(self.options["name"]))
tex.append("\\begin{wordbank}{3}")
if self.wordbank:
for ans in self.wordbank:
tex.append("\t\\wbelem{{{}}}".format(ans))
tex.append("\\end{wordbank}")
in_questions = False
for cont in self.content:
if type(cont) == tuple:
if not in_questions:
in_questions = True
tex.append("\\begin{questions}")
tex.append("\\setcounter{{question}}{{{}}}".format(qcount))
q, a = cont
if self.wordbank:
a = chr(65 + self.wordbank.index(a))
else:
a = 'T' if a.lower() in ["t", "true"] else 'F'
tex.append("\\question\\match{{{}}}{{{}}}".format(a, q))
qcount += 1
else:
if in_questions:
in_questions = False
tex.append("\\end{questions}")
tex.append(cont.to_tex())
if in_questions:
tex.append("\\end{questions}")
return "\n".join(tex)
def ans_sheet_tex(self):
global qcount
tex = []
solutions = []
for cont in self.content:
if type(cont) == tuple:
q, a = cont
if self.wordbank:
a = chr(65 + self.wordbank.index(a))
else:
a = 'T' if a.lower() in ["t", "true"] else 'F'
solutions.append(a)
tex.append("\\raggedcolumns")
tex.append("\\begin{multicols}{5}")
tex.append("\\begin{enumerate}")
tex.append("\t\\setcounter{{enumi}}{{{}}}".format(qcount))
for sol in solutions:
tex.append("\t\\item \\choiceblank{{{}}}".format(sol))
qcount += 1
tex.append("\\end{enumerate}")
# spacing needs fixing iff the columns are uneven in length.
if len(solutions) % 5 != 0:
tex.append("\\fixcolspacing")
tex.append("\\end{multicols}")
return "\n".join(tex)
class Match(MatchTF):
def __init__(self, lines):
Section.__init__(self, lines)
lines = self.lines
wordbank = set()
while lines:
cont, lines = MatchTF.gobble(lines)
self.content.append(cont)
if type(cont) == tuple:
wordbank.add(cont[1])
self.wordbank = sorted(list(wordbank))
if len(self.wordbank) > 26:
compile_error("Too many choices in word bank.")
class TF(MatchTF):
def __init__(self, lines):
Section.__init__(self, lines)
self.wordbank = None
lines = self.lines
while lines:
cont, lines = MatchTF.gobble(lines)
self.content.append(cont)
class MC(Section):
def __init__(self, lines):
Section.__init__(self, lines)
self.format_options()
lines = self.lines
while lines:
cont, lines = MC.gobble(lines)
self.content.append(cont)
def format_options(self):
options = self.options
if "twocolumn" in options:
twocol = options["twocolumn"][0].lower()
if twocol not in ["true", "false"]:
compile_error("Twocolumn option must be boolean.")
options["twocolumn"] = (twocol == "true")
else:
options["twocolumn"] = False
if "condense" in options:
condense = options["condense"][0].lower()
if condense not in ["true", "false"]:
compile_error("Condense option must be boolean.")
options["condense"] = (condense == "true")
else:
options["condense"] = False
def gobble(lines):
cont, lines = Section.gobble(lines)
if cont:
return cont, lines
question = lines[0].strip()
lines = lines[1:]
end = 0
while end < len(lines) and re.match(r"\s", lines[end]):
end += 1
choices = [l.strip() for l in lines[:end]]
MCQ = MC.MCQuestion(question, choices)
return MCQ, lines[end:]
def to_tex(self):
global qcount
initial_qcount = qcount
tex = ["\\newpage"]
if self.options["name"]:
tex.append("\\section*{{{}}}".format(self.options["name"]))
twocol = self.options["twocolumn"]
if twocol:
tex.append("\\setlength{\\columnsep}{0.40 in}")
tex.append("\\begin{multicols*}{2}")
tex.append("\\renewcommand{\\choiceshook}{\\setlength" +
"{\\leftmargin}{0.40 in}}")
tex.append("\\renewcommand{\\questionshook}{\\setlength" +
"{\\leftmargin}{0.0 in}}")
in_questions = False
for cont in self.content:
if type(cont) == MC.MCQuestion:
if not in_questions:
in_questions = True
tex.append("\\begin{questions}")
tex.append("\\setcounter{{question}}{{{}}}".format(qcount))
qcount += 1
else:
if in_questions:
in_questions = False
tex.append("\\end{questions}")
tex.append(cont.to_tex())
if in_questions:
tex.append("\\end{questions}")
if twocol:
tex.append("\\end{multicols*}")
tex.append("\\renewcommand{\\choiceshook}{}")
tex.append("\\renewcommand{\\questionshook}{}")
if self.options["condense"]:
qcount = initial_qcount
tex.append(self.ans_sheet_tex())
return "\n".join(tex)
def ans_sheet_tex(self):
global qcount
tex = []
solutions = []
for cont in self.content:
if type(cont) == MC.MCQuestion:
solutions.append(cont.get_answer())
tex.append("\\raggedcolumns")
tex.append("\\begin{multicols}{5}")
tex.append("\\begin{enumerate}")
tex.append("\t\\setcounter{{enumi}}{{{}}}".format(qcount))
for sol in solutions:
tex.append("\t\\item \\choiceblank{{{}}}".format(sol))
qcount += 1
tex.append("\\end{enumerate}")
# spacing needs fixing iff the columns are uneven in length.
if len(solutions) % 5 != 0:
tex.append("\\fixcolspacing")
tex.append("\\end{multicols}")
return "\n".join(tex)
class MCQuestion:
def __init__(self, question, choices):
self.question = question
self.choices = []
self.correct_choice = None
for choice in choices:
match = re.search("{C}", choice)
if match:
choice = choice[:match.start()]
self.correct_choice = choice
self.choices.append(choice)
# if correct choice not specified, first given answer is correct
if not self.correct_choice:
self.correct_choice = self.choices[0]
# randomize choices
random.shuffle(self.choices)
def get_answer(self):
"""Returns capital-letter character of correct answer choice."""
ind = self.choices.index(self.correct_choice)
return chr(65 + ind)
def to_tex(self):
tex = ["\\question {}".format(latexify(self.question))]
tex.append("\t\\begin{choices}")
for choice in self.choices:
correct = choice == self.correct_choice
choice_str = "CorrectChoice" if correct else "choice"
tex.append("\t\\{} {}".format(choice_str,
latexify(choice)))
tex.append("\t\\end{choices}")
return "\n".join(tex)
class FRQ(Section):
def __init__(self, lines):
Section.__init__(self, lines)
lines = self.lines
while lines:
cont, lines = FRQ.gobble(lines)
self.content.append(cont)
def gobble(lines):
cont, lines = Section.gobble(lines)
if cont:
return cont, lines
end = 1
while end < len(lines) and re.match(r"\s", lines[end]):
end += 1
frq = FRQ.FRQuestion(lines[:end], 0)
return frq, lines[end:]
def to_tex(self):
global qcount
tex = ["\\newpage"]
if self.options["name"]:
tex.append("\\section*{{{}}}".format(self.options["name"]))
in_questions = False
for cont in self.content:
if type(cont) == FRQ.FRQuestion:
if not in_questions:
in_questions = True
tex.append("\\begin{questions}")
tex.append("\\setcounter{{question}}{{{}}}".format(qcount))
qcount += 1
else:
if in_questions:
in_questions = False
tex.append("\\end{questions}")
tex.append(cont.to_tex())
if in_questions:
tex.append("\\end{questions}")
return "\n".join(tex)
def ans_sheet_tex(self):
global qcount
tex = []
tex.append("\\begin{questions}")
tex.append("\\setcounter{{question}}{{{}}}".format(qcount))
for cont in self.content:
if type(cont) == FRQ.FRQuestion:
tex.append(cont.ans_sheet_tex())
qcount += 1
tex.append("\\end{questions}")
return "\n".join(tex)
class FRQuestion:
partlabels = ['question', 'part', 'subpart', 'subsubpart']
def __init__(self, lines, level):
self.content = []
self.level = level
self.point_val = ""
question = lines[0].strip()
match = re.match(r"{\d*\.?\d+}", question)
if match:
self.point_val = "[{}]".format(match.group(0)[1:-1])
question = question[match.end():].strip()
self.question = question if question != "*" else ""
lines = FRQ.FRQuestion.unindent(lines[1:])
if len(lines) == 0:
compile_error("FRQ question missing answer.", self.question)
if len(lines) == 1 and re.match(r"\s*//", lines[0]):
line = lines[0].strip()[2:].strip()
# solution height
match = re.match(r"{\d*\.?\d+}", line)
if match:
self.ans_height = int(float(match.group(0)[1:-1])*18)
line = line[match.end():]
else:
self.ans_height = 18*np.ceil(len(line)/75)
self.answer = line.strip()
else:
while lines:
frq, lines = FRQ.FRQuestion.gobble(lines, level)
self.content.append(frq)
def unindent(lines):
unindented = []
for line in lines:
line = re.sub("\t", " ", line)
if line[:4] != " ":
compile_error("Bad indent.", line)
unindented.append(line[4:])
return unindented
def gobble(lines, level):
cont, lines = Section.gobble(lines)
if cont:
return cont, lines
if re.match(r"\s", lines[0]):
compile_error("Question overindented.", lines[0])
end = 1
while end < len(lines) and re.match(r"\s", lines[end]):
end += 1
frq = FRQ.FRQuestion(lines[:end], level+1)
return frq, lines[end:]
def to_tex(self):
indent = "\t" * self.level
qlabel = FRQ.FRQuestion.partlabels[self.level]
tex = ["{}\\{}{} {}".format(indent, qlabel, self.point_val,
latexify(self.question))]
if self.content:
if self.level+1 >= len(FRQ.FRQuestion.partlabels):
compile_error("FRQ too nested (subsubsubparts not \
allowed.)", self.question)
indent1 = "\t" * (self.level+1)
qlabel1 = FRQ.FRQuestion.partlabels[self.level+1] + "s"
tex.append("{}\\begin{{{}}}".format(indent1, qlabel1))
for cont in self.content:
tex.append(cont.to_tex())
tex.append("{}\\end{{{}}}".format(indent1, qlabel1))
elif not exam.meta["answer sheet"]:
indent1 = "\t" * (self.level+1)
tex.append(indent1 + "\\par")
tex.append("{}\\begin{{solution}}[{}pt]"
.format(indent1, self.ans_height))
tex.append(indent1 + latexify(self.answer))
tex.append(indent1 + "\\end{solution}")
return "\n".join(tex)
def ans_sheet_tex(self):
indent = "\t" * self.level
qlabel = FRQ.FRQuestion.partlabels[self.level]
tex = []
if self.content:
tex.append("{}\\{}".format(indent, qlabel))
if self.level+1 >= len(FRQ.FRQuestion.partlabels):
compile_error("FRQ too nested (subsubsubparts not \
allowed.)", self.question)
indent1 = "\t" * (self.level+1)
qlabel1 = FRQ.FRQuestion.partlabels[self.level+1] + "s"
tex.append("{}\\begin{{{}}}".format(indent1, qlabel1))
for cont in self.content:
if type(cont) == FRQ.FRQuestion:
tex.append(cont.ans_sheet_tex())
tex.append("{}\\end{{{}}}".format(indent1, qlabel1))
else:
tex.append("{}\\{}\\ifprintanswers\\else\\vphantom{{0}}\\fi"
.format(indent, qlabel))
indent1 = "\t" * (self.level+1)
tex.append(indent1 + "\\par")
tex.append("{}\\begin{{solution}}[{}pt]"
.format(indent1, self.ans_height))
tex.append(indent1 + latexify(self.answer))
tex.append(indent1 + "\\end{solution}")
return "\n".join(tex)
class Module:
def __init__(self, lines):
sep = np.flatnonzero([re.match(r"\s*-----", l) for l in lines])
if sep.size != 0:
sep = sep[0]
self.options = process_options(lines[:sep])
self.lines = lines[sep+1:]
else:
self.options = {}
self.lines = lines
self.content = []
class Image(Module):
def __init__(self, lines):
Module.__init__(self, lines)
self.format_options()
if len(self.lines) == 0:
compile_error("Image missing filepath.")
self.img_path = self.lines[0].strip()
def format_options(self):
options = self.options
if "width" in options:
width = options["width"][0]
if width[-1] == "%":
width = "{}\\textwidth".format(float(width[:-1])/100)
options["width"] = width
else:
options["width"] = "\\textwidth"
def to_tex(self):
img_str = "\t\\includegraphics[width={}]{{{}}}".format(
self.options["width"], self.img_path)
tex = ["\\begin{center}"]
tex.append(img_str)
tex.append("\\end{center}")
return "\n".join(tex)
class Text(Module):
def __init__(self, lines):
Module.__init__(self, lines)
def to_tex(self):
tex = ["\\par\\noindent"]
for l in self.lines:
tex.append(latexify(l.strip()))
tex.append("\\par")
tex = tex[:-1]
return "\n".join(tex)
class Latex(Module):
def __init__(self, lines):
Module.__init__(self, lines)
def to_tex(self):
lines = FRQ.FRQuestion.unindent(self.lines)
return "".join(lines)
class Bang:
def __init__(self, line):
line = line.strip().split()
self.bang = line[0].lower()
self.options = line[1:]
def to_tex(self):
if self.bang == "!newpage":
return "\\newpage"
if self.bang == "!newcol":
return "\\vfill\\null\\columnbreak"
if self.bang == "!hrule":
return "\\hrulefill"
if self.bang == "!gap":
if self.options:
return "\\vspace{{{}}}".format(self.options[0])
return "\\vspace{0.10in}"
class Exam:
def __init__(self, examdata):
self.sections = []
self.meta = {}
sec_pattern = r"(?i)\s*\[(meta|cover|match|tf|mc|frq)\]\s*$"
section_inds = [re.match(sec_pattern, line) for line in examdata]
section_inds = np.flatnonzero(section_inds)
if len(section_inds) == 0:
compile_error("No sections found.")
section_inds = list(section_inds)
section_inds.append(len(examdata))
for i in range(len(section_inds)-1):
ind = section_inds[i]
section_type = examdata[ind].strip()[1:-1].lower()
start = ind + 1
end = section_inds[i+1]
if start >= end:
compile_error("Empty section found.", examdata[ind])
content = examdata[start:end]
if section_type == "meta":
meta = process_options(content)
self.meta.update(meta)
elif section_type == "cover":
self.sections.append(Cover(content))
elif section_type == "match":
self.sections.append(Match(content))
elif section_type == "tf":
self.sections.append(TF(content))
elif section_type == "mc":
self.sections.append(MC(content))
elif section_type == "frq":
self.sections.append(FRQ(content))
self.format_meta()
def format_meta(self):
meta = self.meta
if "answer sheet" in meta:
sheet = meta["answer sheet"][0].lower()
if sheet not in ["true", "false"]:
compile_error("Answer sheet option must be boolean.")
meta["answer sheet"] = (sheet == "true")
else:
meta["answer sheet"] = False
if "image sheet" in meta:
meta["image sheet"] = meta["image sheet"][0]
else:
meta["image sheet"] = None
def meta_tex(self):
tex = [template]
if "packages" in self.meta:
for pkg in self.meta["packages"]:
tex.append("\\usepackage{{{}}}\n".format(pkg))
if "header" in self.meta:
header = self.meta["header"]
if len(header) != 3:
compile_error("Header must have three parts.", header)
l, c, r = map(latexify, header)
c = c + (" - Page \\thepage" if c != "" else "")
r = r + (":\\kern .5 in" if r != "" else "")
header_tex = "\\header{{{}}}{{{}}}{{{}}}\n".format(l, c, r)
tex.append("\\pagestyle{head}\n" + header_tex + "\\headrule")
return "\n".join(tex)
def to_tex(self):
tex = [self.meta_tex()]
tex.append("\n\\begin{document}")
for section in self.sections:
tex.append(section.to_tex())
tex.append("\\end{document}\n")
return "\n".join(tex)
def ans_sheet_tex(self):
tex = [self.meta_tex()]
tex.append("\n\\begin{document}")
tex.append("\\section*{Answer Sheet}")
for section in self.sections:
ans = section.ans_sheet_tex()
tex.append(ans)
if ans != "":
tex.append("\\vspace{0.25in}")
tex.append("\\end{document}\n")
return "\n".join(tex)
def image_sheet_tex(self):
fp = self.meta["image sheet"]
tex = [self.meta_tex()]
tex.append("\n\\begin{document}")
tex.append("\\section*{Image Sheet}")
tex.append("\\includegraphics[height=.96\\textheight]{{{}}}"
.format(fp))
tex.append("\\end{document}\n")
return "\n".join(tex)
def main():
global exam
global qcount
global template
template_dir = os.path.join(os.path.dirname(sys.argv[0]), "template.tex")
with open(template_dir, 'r') as filein:
template = "".join(filein.readlines())
# existence of argument is checked in executable script
filename = sys.argv[1]
with open(filename, 'r') as filein:
lines = filein.readlines()
lines = [l for l in lines if l.strip() != ""]
exam = Exam(lines)
# write to tex files
match = re.search("\\.", filename)
if match:
filename = filename[:match.start()]
hashnum = sum(list(map(ord, list(filename))))
seed = sys.argv[2] if len(sys.argv) > 2 else hashnum
random.seed(seed)
qcount = 0
exam_tex = exam.to_tex()
with open(filename+"-EXAM.tex", 'w+') as fileout:
fileout.write(exam_tex)
key_tex = exam_tex
if exam.meta["answer sheet"]:
qcount = 0
sheet_tex = exam.ans_sheet_tex()
with open(filename+"-ANS_SHEET.tex", 'w+') as fileout:
fileout.write(sheet_tex)
key_tex = sheet_tex
key_tex = re.sub(r"%\\printanswers", r"\\printanswers", key_tex)
with open(filename+"-KEY.tex", 'w+') as fileout:
fileout.write(key_tex)
if exam.meta["image sheet"]:
img_tex = exam.image_sheet_tex()
with open(filename+"-IMG_SHEET.tex", 'w+') as fileout:
fileout.write(img_tex)
qcount = 0
exam = None
template = ""
main()