Last active
November 23, 2016 12:13
-
-
Save mayjs/5c80685f4036a12c761a6a947a9c93ad to your computer and use it in GitHub Desktop.
Extract LaTeX labels from .aux files generated by pdflatex
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
import argparse | |
import sys | |
import re | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Extract LaTeX labels") | |
parser.add_argument("file", metavar="FILE", type=argparse.FileType("r")) | |
parser.add_argument("-o", "--output", metavar="OUTPUT_FILE", default=sys.stdout, type=argparse.FileType("w")) | |
args = parser.parse_args() | |
res = [] | |
for line in args.file: | |
line = line.strip() | |
if line.startswith("\\newlabel"): | |
m = re.search("\\\\newlabel\\{(.+)\\}\\{\\{.*\\}\\{.*\\}\\{(.*)\\}\\{.*\\}\\{.*\\}\\}", line) | |
x, y = m.group(1).strip(), m.group(2).strip() | |
if y.endswith("\\relax"): | |
y = y[0:-len("\\relax")] | |
res.append((x,y)) | |
res.sort(key=lambda x: x[0]) | |
distY = max(len(y) for (_,y) in res) | |
distX = max(len(x) for (x,_) in res) | |
formatStr = "{:<" + str(distX) + "}\t{:<" + str(distY+1) + "}" | |
for (x,y) in res: | |
print(formatStr.format(x,y), file = args.output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment