|
#Check dependencies of .tex file in current directory. |
|
#https://chat.openai.com/share/a90ed0e5-56c3-4f25-8519-9a8a8efec380 |
|
|
|
import os |
|
import re |
|
import sys |
|
|
|
# Regex pattern to match common file referencing commands in LaTeX |
|
# You might need to add more patterns to catch all possible references |
|
pattern = re.compile(r"\\(?:includegraphics|include|input|bibliography|documentclass)\s*\[?.*?\]?{(.*?)}") |
|
|
|
# Get the list of all files in the current directory |
|
all_files = os.listdir('.') |
|
|
|
# Lists to hold the referenced and non-referenced files |
|
referenced_files = [] |
|
non_referenced_files = [] |
|
|
|
# Ensure there's a .tex file to proceed |
|
if len(sys.argv)>1: |
|
tex_file = sys.argv[1] |
|
print('loading '+tex_file) |
|
|
|
with open(tex_file, 'r') as file: |
|
content = file.read() |
|
|
|
# Find all files referenced in the .tex file |
|
matches = re.findall(pattern, content) |
|
|
|
# Files can be referenced without their extension in LaTeX |
|
# So, we'll check for references with common extensions too |
|
extensions = ['.tex', '.jpg', '.png', '.pdf', '.cls', '.bib'] |
|
|
|
for match in matches: |
|
# If the matched file has an extension, directly add to the referenced_files list |
|
if '.' in match: |
|
if match in all_files: |
|
referenced_files.append(match) |
|
# If not, append each extension and check if the file exists |
|
else: |
|
for ext in extensions: |
|
temp_match = match + ext |
|
if temp_match in all_files: |
|
referenced_files.append(temp_match) |
|
|
|
# Files not referenced from the .tex |
|
non_referenced_files = list(set(all_files) - set(referenced_files)) |
|
else: |
|
print("No .tex given.") |
|
|
|
# Output the results |
|
print('Referenced Files:') |
|
print('-----------------') |
|
for file in referenced_files: |
|
print(file) |
|
|
|
print('\nNon-Referenced Files:') |
|
print('---------------------') |
|
for file in non_referenced_files: |
|
print(file) |
|
|
|
|