-
Notifications
You must be signed in to change notification settings - Fork 0
/
latex_preparer.py
executable file
·172 lines (126 loc) · 6.27 KB
/
latex_preparer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/python3
# Copyright 2024 Roza Gkliva
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Module Name: latex_preparer.py
Author: Roza Gkliva
Date: 2024-08-24
Description:
This script is used to prepare a latex project for compilation. It copies the figures and bib files to new directories specified by the user
Usage:
./latex_preparer.py [options]
Options:
-h, --help: Show this help message and exit
--main_tex: The main tex file
--project_path: The path to the latex project
--new_folder: The new figures directory
Example:
./latex_preparer.py --main_tex main.tex --project_path /path/to/latex_project --new_folder test_folder
"""
import argparse
import os
import re
import sys
parser = argparse.ArgumentParser(description='Prepare the latex project for compilation')
# parser.add_argument('--main_tex', type=str, help='The main tex file')
parser.add_argument('--project_path', type=str, help='The path to the latex project')
parser.add_argument('--new_folder', type=str, help='The new directory')
args = parser.parse_args()
# print(args.project_path)
figures_extensions = [".png", ".jpg", ".jpeg", ".pdf", ".eps"]
def get_directory_paths():
""" finds where image files are located and where the bib files are located
returns the paths
"""
bib_directory = None
latex_project_directory = args.project_path
for item in os.listdir(latex_project_directory):
# find the directories that contain figures and bib files
if os.path.isdir(os.path.join(latex_project_directory, item)):
# go in the directories and check if image files or bibfiles are there
for root, dirs, files in os.walk(os.path.join(latex_project_directory, item)):
for file in files:
for extension in figures_extensions:
if file.endswith(extension):
figures_directory = os.path.join(latex_project_directory, item)
# print(f"figures: {figures_directory}")
if file.endswith(".bib"):
bib_directory = os.path.join(latex_project_directory, item)
# print(f"figures: {figures_directory}, bib: {bib_directory}")
return figures_directory, bib_directory
def find_used_figures():
""" Scans the main tex file and finds which image files are used
Returns:
which_figures: list of the figures that are used in the tex file
dir_paths: list of the directories that the figures are
"""
project_path = args.project_path
if not os.path.exists(project_path):
print("The project path does not exist")
sys.exit(1)
which_figures = []
dir_paths = []
# scan through the project path and subdirectories to find all .tex files
for root, dirs, files in os.walk(project_path):
for file in files:
if file.endswith('.tex'):
tex_file_path = os.path.join(root, file)
print(f"Scanning tex file: {tex_file_path}")
# open and scan each .tex file
with open(tex_file_path, 'r') as tex_file:
for line in tex_file:
pattern = r'\\includegraphics\[[^\]]*\]\{([^}]+)\}'
match = re.search(pattern, line)
if match:
if line.startswith("%"): # if the matched line is commented out, skip it
continue
print(f"match: {match.group(1)}")
# if match is found, check if the figure is in a directory and remove the directory
figure_path = match.group(1)
# check if figure is in nested directory and save the path
path_parts = figure_path.split("/")
if len(path_parts) > 1:
dir_path = "/".join(path_parts[:-1])
figure_name = figure_path.split("/")[-1]
dir_paths.append(dir_path)
which_figures.append(figure_name)
return which_figures, dir_paths
def copy_figures_to_new_folder(figures_list, figures_dir_list):
new_directory = os.path.join(args.project_path, args.new_folder)
for figure in figures_list:
print(f"----------------")
print(f"which figure: {figure}")
target_directory = os.path.join(new_directory, figures_dir_list[figures_list.index(figure)])
# create the target directory if it does not exist
if not os.path.exists(target_directory):
os.makedirs(target_directory)
target_path = os.path.join(target_directory, figure)
print(f"target path: {target_path}")
source_directory = os.path.join(args.project_path, figures_dir_list[figures_list.index(figure)])
print(f"source directory: {source_directory}")
source_path = os.path.join(source_directory, figure)
if os.path.exists(target_path):
pass
# print(f"The figure {figure} already exists in the figures directory: {target_path}")
else:
# print(f"The figure {figure} does not exist in the figures directory: {target_path}")
print(f"copying: {source_path} to {target_path}")
os.system(f"cp {source_path} {target_path}")
[figures_dir, bibs_dir] = get_directory_paths()
print(f"main figures directory: {figures_dir}")
[figures_list, directories] = find_used_figures()
print(f"figures list: {figures_list}")
print(f"directories: {directories}")
# copy_figures_to_new_folder(figures_list, figures_dir)
# copy_figures_to_new_folder(figures_list, directories)