Repository navigation
Expand file tree
/
Copy pathgenerateLatexPngs.py
More file actions
123 lines (110 loc) · 3.57 KB
/
Copy pathgenerateLatexPngs.py
File metadata and controls
123 lines (110 loc) · 3.57 KB
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
import os
import logging
import subprocess
import numpy as np
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
filename="genPNG.log", # Log file name
filemode="w", # Overwrite the log file each time
)
def latexTemplate(equation):
fname = "temp.tex"
with open(fname, "w+") as o:
o.write(
"\\documentclass[convert={density=500}, border=2pt, varwidth=8in]{standalone}\n"
)
o.write("\\usepackage{amsmath}\n")
o.write("\\usepackage{amssymb}\n")
o.write("\\usepackage{bm}\n")
#o.write("\\usepackage{xcolor}\n") # Add xcolor package for color support
o.write("\\begin{document}\n")
#o.write("\\pagecolor{blue}\n") # Set background color (e.g., white)
o.write("\\begin{align*}\n")
# o.write("_{256}P_{8} = \\frac{256!}{(256 - 8)!} = 1.65 \\times 10^{19}\n")
o.write(f"{equation}\n")
o.write("\\end{align*}\n")
o.write("\\end{document}\n")
def latex2png():
fname = "temp"
try:
# Compile LaTeX to PDF
logging.info("Compiling LaTeX to PDF...")
result = subprocess.run(
["pdflatex", "--shell-escape", f"{fname}.tex"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
)
logging.info(result.stdout)
if result.stderr:
logging.info(result.stderr)
# Convert PDF to PNG and trim borders
logging.info("Converting PDF to PNG and trimming borders...")
result = subprocess.run(
[
"magick",
"-density",
"3000",
"-units",
"PixelsPerInch",
f"{fname}.pdf",
"-quality",
"90",
"-trim",
"+repage",
f"{fname}.png",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
)
logging.info(result.stdout)
if result.stderr:
logging.info(result.stderr)
# Clean up auxiliary files
logging.info("Cleaning up auxiliary files...")
subprocess.run(
["rm", f"{fname}.pdf", f"{fname}.aux", f"{fname}.log", f"{fname}.tex"],
check=True,
)
logging.info("Process completed successfully.")
except subprocess.CalledProcessError as e:
logging.error(f"An error occurred: {e}")
def genPNGfromEquation(eq: str):
# create temporary tex file
latexTemplate(eq)
# compile the temp tex file
latex2png()
# change the temp file name
imgNum = 1
# prevent overwritting existing equation image files
outputDir = "png"
if not os.path.exists(outputDir):
os.mkdir(outputDir)
while True:
if os.path.exists(f"{outputDir}/eq{imgNum}.png"):
imgNum += 1
else:
if os.path.exists("temp.png"):
os.rename("temp.png", f"{outputDir}/eq{imgNum}.png")
break
else:
exit("Error: filed to generate a temporary png file.")
break
def main():
equations = []
with open("equations.txt", "r") as out:
for line in out:
line = line.strip()
equations.append(line)
# remove empty strings in the list
equations = [x for x in equations if x]
# generate png equation files
for eq in equations:
genPNGfromEquation(eq)
if __name__ == "__main__":
main()