-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_latex.py
80 lines (65 loc) · 2.18 KB
/
create_latex.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
# Code provided by Tiago de Souza Fernandes from University of Brasilia
# Repo: Tiagosf00/Competitive-Programming
import os
import subprocess
import shutil
def cpy_template():
shutil.copyfile('template.tex', 'notebook.tex')
def remove_aux():
if os.path.exists('notebook.aux'):
os.remove('notebook.aux')
if os.path.exists('notebook.log'):
os.remove('notebook.log')
# if os.path.exists('notebook.toc'):
# os.remove('notebook.toc')
if os.path.exists('texput.log'):
os.remove('texput.log')
def get_dir():
path = 'codes'
items = os.listdir(path)
section = []
items.sort()
items = list(filter(lambda x : x[0] != '.', items))
print(items)
for itemname in items:
subsection = []
files = os.listdir(os.path.join(path, itemname))
files.sort()
for filename in files:
if filename.endswith('.cpp') or filename.endswith('.tex'):
subsection.append(filename)
section.append((itemname, subsection))
print(section)
return section
def create_notebook(section):
path = 'codes'
aux = ''
with open('notebook.tex', 'a') as texfile:
for (item, subsection) in section:
aux += '\\section{%s}\n' % item
for file in subsection:
name, ext = os.path.splitext(file)
if ext == '.cpp':
aux += '\\includes{%s}{%s}\n' % \
(name, os.path.join(path, item, file))
elif ext == '.tex':
aux += '\\subsection{%s}' % name
aux += '\\input{%s}\n' % \
(os.path.join(path, item, file))
aux += '\n\\end{multicols}\n\\end{document}\n'
print(aux)
texfile.write(aux)
cpy_template()
section = get_dir()
create_notebook(section)
cmd = ['pdflatex', '-interaction=nonstopmode', '-halt-on-error',
'notebook.tex']
with open(os.devnull, 'w') as DEVNULL:
try:
subprocess.check_call(cmd, stdout=DEVNULL)
except Exception as e:
print(str(e))
print("Erro na transformação de LaTex para pdf.")
exit(1)
remove_aux()
print("O PDF foi gerado com sucesso!")