Coding_Tools/unifytex
2025-01-12 19:01:17 +01:00

49 lines
1.6 KiB
Python
Executable File

#!/bin/python3
import os
import argparse
def dynamic_includes(folder_path):
"""Generate separate LaTeX files recursively"""
consolidated_dir = os.path.join(os.getcwd(), "consolidated")
os.makedirs(consolidated_dir, exist_ok=True)
for root, dirs, files in os.walk(folder_path):
if not files or os.path.abspath(root) == os.path.abspath(consolidated_dir):
continue
if root == folder_path:
folder_name = os.path.basename(folder_path) or "root"
else:
folder_name = os.path.basename(root)
output_file = os.path.join(consolidated_dir, f"{folder_name}.tex")
with open(output_file, "w") as f:
f.write("% Autogenerated by unifytex (git.confest.im/boyan_k/Coding_Tools/)\n")
for file in sorted(files):
if file.endswith(".tex"):
relative_path = os.path.relpath(
os.path.join(root, file),
os.path.dirname(os.path.abspath(folder_path))
)
f.write(f"\\input{{{relative_path}}}\n")
print(f"Generated {output_file}")
def main():
parser = argparse.ArgumentParser(description="Generate separate LaTeX include files for .tex files recursively")
parser.add_argument("directory", help="The directory to process.")
args = parser.parse_args()
directory = args.directory
if not os.path.isdir(directory):
raise FileNotFoundError(f"Error: The directory '{directory}' does not exist.")
dynamic_includes(directory)
if __name__ == "__main__":
main()