import os outdir = os.path.join(os.path.dirname(__file__), 'chapters') for fname in sorted(os.listdir(outdir)): if not fname.endswith('.tex'): continue fpath = os.path.join(outdir, fname) with open(fpath, 'r', encoding='utf-8') as fh: content = fh.read() lines = content.split('\n') prev_cmd = '' prev_title = '' for i, line in enumerate(lines): s = line.strip() matched = None for cmd in ['\\chapter{', '\\section{', '\\subsection{', '\\subsubsection{']: if s.startswith(cmd): matched = cmd break if matched: # Check for level jumps (skip of 1 or more intermediate levels) levels = {'\\chapter{': 0, '\\section{': 1, '\\subsection{': 2, '\\subsubsection{': 3} if prev_cmd and matched in levels and prev_cmd in levels: jump = levels[matched] - levels[prev_cmd] if jump > 1: title = s[len(matched):-1] print(f'{fname}:{i+1}: {prev_cmd.replace("{","")} -> {matched.replace("{","")} ({jump} levels): {title[:60]}') prev_cmd = matched