-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- compile_tex -> tex_to_pdf - most code moved to... - ...new module "latex.py" - gift.py - process_latex - an exception is raised if a formula cannot be compiled... - question.py - HtmlQuestion - process_text - ...the exception is caught - wrap.py - new command-line option "no-checks" - minor improvements - more documentation
- Loading branch information
1 parent
9e6d138
commit 65a8b33
Showing
7 changed files
with
191 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import pathlib | ||
import shutil | ||
import subprocess | ||
import string | ||
from typing import Union, List, Optional | ||
|
||
|
||
def compile_tex( | ||
source_file: Union[str, pathlib.Path], timeout: Optional[int], options: List[str] = ['halt-on-error']) -> int: | ||
""" | ||
Compiles a TeX file. | ||
Parameters | ||
---------- | ||
source_file : str or pathlib.Path | ||
TeX file. | ||
timeout: int | ||
Seconds that are given to compile the source. | ||
options: list of str | ||
Options to be passed to `pdflatex`. | ||
Returns | ||
------- | ||
out: int | ||
The exit status of the call to `pdflatex`. | ||
""" | ||
|
||
source_file = pathlib.Path(source_file) | ||
|
||
path_to_compiler = shutil.which('pdflatex') | ||
|
||
assert path_to_compiler, 'cannot find pdflatex' | ||
|
||
command = [path_to_compiler] + [f'-{o}' for o in options] + [source_file.name] | ||
|
||
run_summary = subprocess.run(command, capture_output=True, cwd=source_file.parent, timeout=timeout) | ||
|
||
return run_summary.returncode | ||
|
||
|
||
latex_template = string.Template(r''' | ||
\documentclass{standalone} | ||
\usepackage{amsmath} | ||
\begin{document} | ||
$$ | ||
$formula | ||
$$ | ||
\end{document} | ||
''') | ||
|
||
|
||
def formula_can_be_compiled(formula: str, auxiliary_file: str = '__latex_check.tex') -> bool: | ||
""" | ||
Checks whether a latex formula can be compiled with the above template, `latex_template`. | ||
Parameters | ||
---------- | ||
formula : str | ||
Latex formula. | ||
auxiliary_file : str | ||
(Auxiliary) TeX file that is created to check the formula. | ||
Returns | ||
------- | ||
out: bool | ||
`True` if the compilation finished with no errors. | ||
""" | ||
|
||
tex_source_code = latex_template.substitute(formula=formula) | ||
|
||
with open(auxiliary_file, 'w') as f: | ||
|
||
f.write(tex_source_code) | ||
|
||
exit_status = compile_tex(auxiliary_file, timeout=10, options=['halt-on-error', 'draftmode']) | ||
|
||
return exit_status == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.