-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_pdf_from_latex_files.py
102 lines (78 loc) · 3.21 KB
/
generate_pdf_from_latex_files.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
#!/usr/bin/env python3
"""
The purpose of this script is to validate that
each Latex expression can be rendered as a PDF
However, BHP actually uses overleaf to edit main.tex and validate rendering
"""
import json
import glob
#import random
from subprocess import PIPE # https://docs.python.org/3/library/subprocess.html
import subprocess # https://stackoverflow.com/questions/39187886/what-is-the-difference-between-subprocess-popen-and-subprocess-run/39187984
proc_timeout = 30
def compile_tex_to_dvi(file_name: str) -> None:
process = subprocess.run(
["latex", "-halt-on-error", file_name + ".tex"],
stdout=PIPE,
stderr=PIPE,
timeout=proc_timeout,
)
# latex_stdout, latex_stderr = process.communicate()
# https://stackoverflow.com/questions/41171791/how-to-suppress-or-capture-the-output-of-subprocess-run
latex_stdout = process.stdout.decode("utf-8")
print(latex_stdout)
latex_stderr = process.stderr.decode("utf-8")
print(latex_stderr)
return
def dvi_to_pdf(file_name: str) -> None:
# https://tex.stackexchange.com/questions/73783/dvipdfm-or-dvipdfmx-or-dvipdft
process = subprocess.run(
["dvipdfmx", file_name + ".dvi"],
stdout=PIPE,
stderr=PIPE,
timeout=proc_timeout,
)
dvipdf_stdout = process.stdout.decode("utf-8")
print(dvipdf_stdout)
dvipdf_stderr = process.stderr.decode("utf-8")
print(dvipdf_stderr)
return
def create_latex_file(file_name: str, math_latex: str):
"""
>>> create_latex_file('a_file_name', 'a=b')
"""
with open(file_name + ".tex", "w") as lat_file:
lat_file.write("\\documentclass[12pt]{article}\n")
lat_file.write("\\thispagestyle{empty}\n")
lat_file.write("\\usepackage{amsmath}\n")
lat_file.write("\\begin{document}\n")
#lat_file.write("\\begin{equation}\n")
lat_file.write(math_latex + "\n")
#lat_file.write("\\end{equation}\n")
lat_file.write("\\end{document}\n")
return
def compile_main_to_pdf(folder_with_valid_latex: str)->None:
# copy contents
with open(folder_with_valid_latex+"main.tex","r") as file_handle:
file_content = file_handle.read()
with open("main.tex","w") as file_handle:
file_handle.write(file_content)
compile_tex_to_dvi("main")
dvi_to_pdf("main")
def compile_each_example_to_pdf(folder_with_valid_latex: str)->None:
list_of_latex_expression_files = glob.glob(folder_with_valid_latex+"expression_*.tex")
print("list_of_latex_expression_files=",list_of_latex_expression_files)
for latex_expr_filename in list_of_latex_expression_files:
with open(latex_expr_filename,'r') as file_handle:
file_content = file_handle.read()
print(file_content)
file_name = latex_expr_filename.replace(".tex","")
file_name = file_name.replace(folder_with_valid_latex,"")
print("file_name=",file_name)
create_latex_file(file_name, file_content)
compile_tex_to_dvi(file_name)
dvi_to_pdf(file_name)
if __name__ == "__main__":
folder_with_valid_latex = "/scratch/examples_of_valid_latex/"
compile_main_to_pdf(folder_with_valid_latex)
compile_each_example_to_pdf(folder_with_valid_latex)