Skip to content

Instantly share code, notes, and snippets.

@ha7ilm
Last active July 29, 2023 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ha7ilm/23f94c9f481e25c0a9303d67854ac04a to your computer and use it in GitHub Desktop.
Save ha7ilm/23f94c9f481e25c0a9303d67854ac04a to your computer and use it in GitHub Desktop.
Find all referenced files in .tex file and categorize the files in current directory to referenced and non-referenced files

The AI prompt to generate this program was:

write a python program that finds all the files referenced in a .tex (LaTeX) file in the current directory. then, it outputs two list of files:

  • those referenced from the .tex (including images, PDF, cls, bib, etc.)
  • those not referenced from the .tex
Copyright 2023 Andras Retzler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment