Created
July 21, 2024 20:00
-
-
Save cristicretu/21d5dbce5dee9dfdece42321b834dd22 to your computer and use it in GitHub Desktop.
Label checker latex
This file contains hidden or 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 os | |
| import re | |
| def analyze_tex_labels(directory='.'): | |
| label_pattern = re.compile(r'\\label{q(\d+)}') | |
| numbers = set() | |
| for root, dirs, files in os.walk(directory): | |
| for filename in files: | |
| if filename.endswith('.tex'): | |
| file_path = os.path.join(root, filename) | |
| try: | |
| with open(file_path, 'r', encoding='utf-8') as file: | |
| content = file.read() | |
| matches = label_pattern.findall(content) | |
| numbers.update(map(int, matches)) | |
| except Exception as e: | |
| print(f"Error reading file {file_path}: {e}") | |
| if not numbers: | |
| print("No labels found.") | |
| return | |
| largest_number = max(numbers) | |
| print(f"The largest number is: {largest_number}") | |
| gaps = [] | |
| for i in range(1, largest_number): | |
| if i not in numbers: | |
| gaps.append(i) | |
| if gaps: | |
| print("The following gaps were found:") | |
| print(", ".join(map(str, gaps))) | |
| else: | |
| print("No gaps found in the sequence.") | |
| print(f"Total unique numbers found: {len(numbers)}") | |
| analyze_tex_labels() | |
| def check_answer_labels(directory='.'): | |
| answer_pattern = re.compile(r'\\answer{.*?}') | |
| label_pattern = re.compile(r'\\label{.*?}') | |
| issues_found = False | |
| for root, dirs, files in os.walk(directory): | |
| for filename in files: | |
| if filename.endswith('.tex'): | |
| file_path = os.path.join(root, filename) | |
| try: | |
| with open(file_path, 'r', encoding='utf-8') as file: | |
| lines = file.readlines() | |
| for line_number, line in enumerate(lines, 1): | |
| answer_match = answer_pattern.search(line) | |
| if answer_match: | |
| if not label_pattern.search(line[answer_match.end():]): | |
| next_line = lines[line_number] if line_number < len(lines) else "" | |
| if not label_pattern.search(next_line): | |
| print(f"File: {file_path}") | |
| print(f"Line {line_number}: Answer without label") | |
| print(f"Content: {line.strip()}") | |
| print() | |
| issues_found = True | |
| except Exception as e: | |
| print(f"Error reading file {file_path}: {e}") | |
| print() | |
| if not issues_found: | |
| print("No issues found. All \\answer{} commands are followed by \\label{}.") | |
| check_answer_labels() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment