Skip to content

Instantly share code, notes, and snippets.

@jkibele
Last active February 20, 2020 16:24
Show Gist options
  • Save jkibele/9de7f6d9b3250c09c688 to your computer and use it in GitHub Desktop.
Save jkibele/9de7f6d9b3250c09c688 to your computer and use it in GitHub Desktop.
Python code to generate a list of labels from latex documents.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@TRomijn
Copy link

TRomijn commented Jul 3, 2018

Thanks for sharing this code!

I changed some parts in the larger part of your code to make it work with Python 3.6

# import regex instead of re
import regex

# reg exp to find full label specifications
patt = regex.compile("\\label{(\w*:\w*)}")
# reg exp to find `addtotoc` labels
app_patt = re.compile("(\w+:\w+)")

lbls = []
for root, dirs, files in os.walk(chdir):
    for fn in files: 
        if fn == 'main.tex':
            # my project has some appendices that are pulled into
            # the project using includepdf and the labels for these
            # are specified using `addtotoc`. This if statement
            # finds those labels in main.tex.
            with open(os.path.join(root, fn),encoding="Latin-1") as f:
                lbls.extend(re.findall(app_patt, f.read()))
        elif fn.endswith(".tex"):
            # there's a template file in my project that I don't
            # want to search for labels.
            with open(os.path.join(root,fn),encoding="Latin-1") as f:
                txt = f.read()
                labels = regex.findall(patt, txt)
                lbls.extend(labels)
typs = []
names = []
for lbl in lbls:
    t, n = lbl.split(':')
    typs.append(t)
    names.append(n)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment