Created
May 10, 2012 11:34
-
-
Save schlamar/2652543 to your computer and use it in GitHub Desktop.
Find the root document of a given LaTeX file
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 glob | |
import os | |
import re | |
MAIN_PATTERN = re.compile(r'\\begin{document}') | |
def is_main(tex_file): | |
with open(tex_file) as fobj: | |
for line in fobj: | |
if MAIN_PATTERN.search(line): | |
return True | |
def get_tex_root(tex_file): | |
if is_main(tex_file): | |
return tex_file | |
path, _ = os.path.split(tex_file) | |
path = os.path.abspath(path) | |
while True: | |
for fname in glob.iglob(os.path.join(path, '*.tex')): | |
if fname == tex_file: | |
continue | |
if is_main(fname): | |
return fname | |
parent_path = os.path.abspath(os.path.join(path, '..')) | |
if path == parent_path: | |
raise ValueError('Main tex file not found.') | |
path = parent_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment