-
Notifications
You must be signed in to change notification settings - Fork 957
/
latex2png.py
177 lines (154 loc) · 6 KB
/
latex2png.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
173
174
175
176
177
# mostly taken from http://code.google.com/p/latexmath2png/
# install preview.sty
import os
import re
import sys
import io
import glob
import tempfile
import shlex
import subprocess
import traceback
from PIL import Image
class Latex:
BASE = r'''
\documentclass[varwidth]{standalone}
\usepackage{fontspec,unicode-math}
\usepackage[active,tightpage,displaymath,textmath]{preview}
\setmathfont{%s}
\begin{document}
\thispagestyle{empty}
%s
\end{document}
'''
def __init__(self, math, dpi=250, font='Latin Modern Math'):
'''takes list of math code. `returns each element as PNG with DPI=`dpi`'''
self.math = math
self.dpi = dpi
self.font = font
self.prefix_line = self.BASE.split("\n").index(
"%s") # used for calculate error formula index
def write(self, return_bytes=False):
# inline = bool(re.match('^\$[^$]*\$$', self.math)) and False
try:
workdir = tempfile.gettempdir()
fd, texfile = tempfile.mkstemp('.tex', 'eq', workdir, True)
# print(self.BASE % (self.font, self.math))
with os.fdopen(fd, 'w+') as f:
document = self.BASE % (self.font, '\n'.join(self.math))
# print(document)
f.write(document)
png, error_index = self.convert_file(
texfile, workdir, return_bytes=return_bytes)
return png, error_index
finally:
if os.path.exists(texfile):
try:
os.remove(texfile)
except PermissionError:
pass
def convert_file(self, infile, workdir, return_bytes=False):
infile = infile.replace('\\', '/')
try:
# Generate the PDF file
# not stop on error line, but return error line index,index start from 1
cmd = 'xelatex -interaction nonstopmode -file-line-error -output-directory %s %s' % (
workdir.replace('\\', '/'), infile)
p = subprocess.Popen(
shlex.split(cmd),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True
)
sout, serr = p.communicate()
# extract error line from sout
error_index, _ = extract(text=sout, expression=r"%s:(\d+)" % os.path.basename(infile))
# extract success rendered equation
if error_index != []:
# offset index start from 0, same as self.math
error_index = [int(_)-self.prefix_line-1 for _ in error_index]
# Convert the PDF file to PNG's
pdffile = infile.replace('.tex', '.pdf')
result, _ = extract(
text=sout, expression="Output written on %s \((\d+)? page" % pdffile)
if int(result[0]) != len(self.math):
raise Exception('xelatex rendering error, generated %d formula\'s page, but the total number of formulas is %d.' % (
int(result[0]), len(self.math)))
pngfile = os.path.join(workdir, infile.replace('.tex', '.png'))
cmd = 'convert -density %i -colorspace gray %s -quality 90 %s' % (
self.dpi,
pdffile,
pngfile,
) # -bg Transparent -z 9
if sys.platform == 'win32':
cmd = 'magick ' + cmd
p = subprocess.Popen(
shlex.split(cmd),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
sout, serr = p.communicate()
if p.returncode != 0:
raise Exception('PDFpng error', serr, cmd, os.path.exists(
pdffile), os.path.exists(infile))
if return_bytes:
if len(self.math) > 1:
png = [open(pngfile.replace('.png', '')+'-%i.png' %
i, 'rb').read() for i in range(len(self.math))]
else:
png = [open(pngfile.replace(
'.png', '')+'.png', 'rb').read()]
else:
# return path
if len(self.math) > 1:
png = [(pngfile.replace('.png', '')+'-%i.png' % i)
for i in range(len(self.math))]
else:
png = [(pngfile.replace('.png', '')+'.png')]
return png, error_index
except Exception as e:
print(e)
finally:
# Cleanup temporaries
basefile = infile.replace('.tex', '')
tempext = ['.aux', '.pdf', '.log']
if return_bytes:
ims = glob.glob(basefile+'*.png')
for im in ims:
os.remove(im)
for te in tempext:
tempfile = basefile + te
if os.path.exists(tempfile):
os.remove(tempfile)
__cache = {}
def tex2png(eq, **kwargs):
if not eq in __cache:
__cache[eq] = Latex(eq, **kwargs).write(return_bytes=True)
return __cache[eq]
def tex2pil(tex, return_error_index=False, **kwargs):
pngs, error_index = Latex(tex, **kwargs).write(return_bytes=True)
images = [Image.open(io.BytesIO(d)) for d in pngs]
return (images, error_index) if return_error_index else images
def extract(text, expression=None):
"""extract text from text by regular expression
Args:
text (str): input text
expression (str, optional): regular expression. Defaults to None.
Returns:
str: extracted text
"""
try:
pattern = re.compile(expression)
results = re.findall(pattern, text)
return results, True if len(results) != 0 else False
except Exception:
traceback.print_exc()
if __name__ == '__main__':
if len(sys.argv) > 1:
src = sys.argv[1]
else:
src = r'\begin{equation}\mathcal{ L}\nonumber\end{equation}'
print('Equation is: %s' % src)
print(Latex(src).write())