Skip to content

Instantly share code, notes, and snippets.

@mayjs
Last active November 23, 2016 12:13
Show Gist options
  • Save mayjs/5c80685f4036a12c761a6a947a9c93ad to your computer and use it in GitHub Desktop.
Save mayjs/5c80685f4036a12c761a6a947a9c93ad to your computer and use it in GitHub Desktop.
Extract LaTeX labels from .aux files generated by pdflatex
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