Created
September 16, 2020 15:59
-
-
Save inducer/c9f1c428e14667e61f814f47fcc6cae5 to your computer and use it in GitHub Desktop.
Purge Comments from LaTeX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
import sys | |
import re | |
with open(sys.argv[1], "rt") as infile: | |
tex = infile.read() | |
tex, _ = re.subn("%.*\n", "", tex) | |
tex, _ = re.subn(r"\\begin\{comment\}.*\\end\{comment\}", "", tex, flags=re.DOTALL) | |
def remove_macro_with_argument(macro, s): | |
i = 0 | |
while True: | |
start = s.find(macro, i) | |
if start == -1: | |
break | |
i = start + len(macro) | |
if s[i] == "#": | |
# That's the definition, keep going. | |
continue | |
assert s[i] == "{" | |
level = 1 | |
i += 1 | |
while i < len(s): | |
c = s[i] | |
i += 1 | |
if c == "{": | |
level += 1 | |
elif c == "}": | |
level -= 1 | |
if level == 0: | |
break | |
s = s[:start] + s[i:] | |
i = start | |
return s | |
tex = remove_macro_with_argument(r"\iftechreport", tex) | |
print(tex) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment