-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlatexmk-solutions.py
More file actions
executable file
·46 lines (36 loc) · 1.17 KB
/
latexmk-solutions.py
File metadata and controls
executable file
·46 lines (36 loc) · 1.17 KB
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
#!/usr/bin/env python3
from pathlib import Path
import re
import subprocess
PATTERN = re.compile(
r"^\\documentclass\[?([^\]]*)\]?\{acAssignment\}", re.MULTILINE)
for tex_file in Path(".").iterdir():
if (
not tex_file.name.endswith(".tex") or
tex_file.name.endswith("-solution.tex") or
tex_file.name == "acAssignment.tex"
):
continue
tex_file_solution = (
tex_file.parent / (tex_file.name.replace(".tex", "-solution.tex"))
)
contents = tex_file.read_text(encoding="UTF-8")
match = PATTERN.search(contents)
if not match:
continue
options = [
option.strip()
for option in match.group(1).split(",")
if option.strip()
]
options.append("solution")
options_str = ", ".join(options)
documentclass_str = rf"\\documentclass[{options_str}]{{acAssignment}}"
contents_solution = PATTERN.sub(documentclass_str, contents)
if (
tex_file_solution.exists() and
tex_file_solution.read_text(encoding="UTF-8") == contents_solution
):
continue
tex_file_solution.write_text(contents_solution, encoding="UTF-8")
subprocess.call(["latexmk"])