-
-
Notifications
You must be signed in to change notification settings - Fork 404
/
latex.py
300 lines (241 loc) · 9.39 KB
/
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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# sage_setup: distribution = sagemath-environment
r"""
Features for testing the presence of ``latex`` and equivalent programs
"""
# ****************************************************************************
# Copyright (C) 2021 Sebastien Labbe <slabqc@gmail.com>
# 2021 Matthias Koeppe
# 2022 Kwankyu Lee
# 2022 Sebastian Oehms
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
from . import StaticFile, Executable, FeatureTestResult, FeatureNotPresentError
latex_url = 'https://www.latex-project.org/'
latex_spkg = 'texlive'
class LaTeX(Executable):
r"""
A :class:`~sage.features.Feature` describing the presence of ``latex``
EXAMPLES::
sage: from sage.features.latex import latex
sage: latex().is_present() # optional - latex
FeatureTestResult('latex', True)
"""
def __init__(self, name):
r"""
TESTS::
sage: from sage.features.latex import latex
sage: isinstance(latex(), latex)
True
"""
super().__init__(name, executable=name, spkg=latex_spkg, url=latex_url)
def is_functional(self):
r"""
Return whether ``latex`` in the path is functional.
EXAMPLES::
sage: from sage.features.latex import latex
sage: latex().is_functional() # optional - latex
FeatureTestResult('latex', True)
When the feature is not functional, more information on the reason
can be obtained as follows::
sage: result = latex().is_functional() # not tested
sage: print(result.reason) # not tested
Running latex on a sample file
(with command='latex -interaction=nonstopmode tmp_wmpos8ak.tex')
returned non-zero exit status='1' with stderr=''
and stdout='This is pdfTeX,
...
Runaway argument?
{document
! File ended while scanning use of \end.
...
No pages of output.
Transcript written on tmp_wmpos8ak.log.'
"""
lines = []
lines.append(r"\documentclass{article}")
lines.append(r"\begin{document}")
lines.append(r"$\alpha+2$")
lines.append(r"\end{document}")
content = '\n'.join(lines)
# create a simple tex file with the content
from sage.misc.temporary_file import tmp_filename
base_filename_tex = tmp_filename(ext='.tex')
with open(base_filename_tex, 'w') as f:
f.write(content)
import os
base, filename_tex = os.path.split(base_filename_tex)
# running latex
from subprocess import run
cmd = [self.name, '-interaction=nonstopmode', filename_tex]
cmd = ' '.join(cmd)
result = run(cmd, shell=True, cwd=base, capture_output=True, text=True)
# return
if result.returncode == 0:
return FeatureTestResult(self, True)
else:
return FeatureTestResult(self, False, reason="Running latex on "
"a sample file (with command='{}') returned non-zero "
"exit status='{}' with stderr='{}' "
"and stdout='{}'".format(result.args,
result.returncode,
result.stderr.strip(),
result.stdout.strip()))
class latex(LaTeX):
r"""
A :class:`~sage.features.Feature` describing the presence of ``latex``
EXAMPLES::
sage: from sage.features.latex import latex
sage: latex().is_present() # optional - latex
FeatureTestResult('latex', True)
"""
def __init__(self):
r"""
TESTS::
sage: from sage.features.latex import latex
sage: isinstance(latex(), latex)
True
"""
super().__init__("latex")
class pdflatex(LaTeX):
r"""
A :class:`~sage.features.Feature` describing the presence of ``pdflatex``
EXAMPLES::
sage: from sage.features.latex import pdflatex
sage: pdflatex().is_present() # optional - pdflatex
FeatureTestResult('pdflatex', True)
"""
def __init__(self):
r"""
TESTS::
sage: from sage.features.latex import pdflatex
sage: isinstance(pdflatex(), pdflatex)
True
"""
super().__init__("pdflatex")
class xelatex(LaTeX):
r"""
A :class:`~sage.features.Feature` describing the presence of ``xelatex``
EXAMPLES::
sage: from sage.features.latex import xelatex
sage: xelatex().is_present() # optional - xelatex
FeatureTestResult('xelatex', True)
"""
def __init__(self):
r"""
TESTS::
sage: from sage.features.latex import xelatex
sage: isinstance(xelatex(), xelatex)
True
"""
super().__init__("xelatex")
class lualatex(LaTeX):
r"""
A :class:`~sage.features.Feature` describing the presence of ``lualatex``
EXAMPLES::
sage: from sage.features.latex import lualatex
sage: lualatex().is_present() # optional - lualatex
FeatureTestResult('lualatex', True)
"""
def __init__(self):
r"""
TESTS::
sage: from sage.features.latex import lualatex
sage: isinstance(lualatex(), lualatex)
True
"""
super().__init__("lualatex")
class dvips(Executable):
r"""
A :class:`~sage.features.Feature` describing the presence of ``dvips``
EXAMPLES::
sage: from sage.features.latex import dvips
sage: dvips().is_present() # optional - dvips
FeatureTestResult('dvips', True)
"""
def __init__(self):
r"""
TESTS::
sage: from sage.features.latex import dvips
sage: isinstance(dvips(), dvips)
True
"""
Executable.__init__(self, "dvips", executable="dvips",
url="https://tug.org/texinfohtml/dvips.html")
class TeXFile(StaticFile):
r"""
A :class:`sage.features.Feature` describing the presence of a TeX file
EXAMPLES::
sage: from sage.features.latex import TeXFile
sage: TeXFile('x', 'x.tex').is_present() # optional - latex
FeatureTestResult('x', True)
"""
def __init__(self, name, filename, **kwds):
r"""
Initialize.
TESTS::
sage: from sage.features.latex import TeXFile
sage: TeXFile('nonexisting', 'xxxxxx-nonexisting-file.tex').is_present() # optional - latex
FeatureTestResult('nonexisting', False)
"""
StaticFile.__init__(self, name, filename, search_path=[], **kwds)
def absolute_filename(self) -> str:
r"""
The absolute path of the file.
EXAMPLES::
sage: from sage.features.latex import TeXFile
sage: feature = TeXFile('latex_class_article', 'article.cls')
sage: feature.absolute_filename() # optional - latex
'.../latex/base/article.cls'
"""
from subprocess import run, CalledProcessError, PIPE
try:
proc = run(['kpsewhich', self.filename],
capture_output=True, text=True, check=True)
return proc.stdout.strip()
except CalledProcessError:
reason = "{filename!r} not found by kpsewhich".format(filename=self.filename)
raise FeatureNotPresentError(self, reason)
def _is_present(self):
r"""
Test for the presence of the TeX file.
EXAMPLES::
sage: from sage.features.latex import LaTeXPackage, latex
sage: f = LaTeXPackage("tkz-graph")
sage: g = latex()
sage: not f.is_present() or bool(g.is_present()) # indirect doctest
True
"""
return latex().is_present() and super()._is_present()
class LaTeXPackage(TeXFile):
r"""
A :class:`sage.features.Feature` describing the presence of a LaTeX package
(``.sty`` file).
EXAMPLES::
sage: from sage.features.latex import LaTeXPackage
sage: LaTeXPackage('graphics').is_present() # optional - latex
FeatureTestResult('latex_package_graphics', True)
"""
@staticmethod
def __classcall__(cls, package_name, **kwds):
"""
TESTS::
sage: from sage.features.latex import LaTeXPackage
sage: LaTeXPackage('graphics') is LaTeXPackage('graphics')
True
"""
return TeXFile.__classcall__(cls,
f'latex_package_{package_name}'.replace('-', '_'),
f'{package_name}.sty',
**kwds)
def all_features():
return [latex(),
pdflatex(),
xelatex(),
lualatex(),
dvips(),
LaTeXPackage("tkz-graph")]