Compare commits

...

2 Commits

Author SHA1 Message Date
Nicolas Kruse b3c2f5e384 Tests update to write no files to tests/out it WRITE_RESULT_FILES = False 2025-05-21 16:50:23 +02:00
Nicolas Kruse dba8f5997e latex backend can be selected now 2025-05-21 16:46:40 +02:00
4 changed files with 22 additions and 7 deletions

View File

@ -673,7 +673,8 @@ class DocumentWriter():
def to_pdf(self, file_path: str,
font_family: Literal[None, 'serif', 'sans-serif'] = None,
table_renderer: TRenderer = 'simple',
latex_template_path: str = '') -> bool:
latex_template_path: str = '',
engine: latex.LatexEngine = 'pdflatex') -> bool:
"""
Export the document to a PDF file using LaTeX.
@ -683,6 +684,7 @@ class DocumentWriter():
latex_template_path: Path to a LaTeX template file. The
expression <!--CONTENT--> will be replaced by the generated content.
If no path is provided a default template is used.
engine: LaTeX engine (pdflatex, lualatex, xelatex or tectonic)
Returns:
True if the PDF file was successfully created
@ -693,7 +695,7 @@ class DocumentWriter():
if font_family == 'sans-serif':
latex_code = latex.inject_latex_command(latex_code, '\\renewcommand{\\familydefault}{\\sfdefault}')
success, errors, warnings = latex.compile(latex_code, file_path)
success, errors, warnings = latex.compile(latex_code, file_path, engine=engine)
if not success:
print('Errors:')

View File

@ -1,5 +1,5 @@
from html.parser import HTMLParser
from typing import Generator, Any
from typing import Generator, Any, Literal, get_args
from pandas.io.formats.style import Styler
import re
import os
@ -9,6 +9,9 @@ import tempfile
from .latex_escaping import unicode_to_latex_dict, latex_escape_dict
LatexEngine = Literal['pdflatex', 'lualatex', 'xelatex', 'tectonic']
def basic_formatter(value: Any) -> str:
return escape_text(str(value))
@ -248,7 +251,7 @@ def from_html(html_code: str) -> str:
return ''.join(parser.latex_code)
def compile(latex_code: str, output_file: str = '', encoding: str = 'utf-8') -> tuple[bool, list[str], list[str]]:
def compile(latex_code: str, output_file: str = '', encoding: str = 'utf-8', engine: LatexEngine = 'pdflatex') -> tuple[bool, list[str], list[str]]:
"""
Compiles LaTeX code to a PDF file.
@ -256,6 +259,7 @@ def compile(latex_code: str, output_file: str = '', encoding: str = 'utf-8') ->
latex_code: The LaTeX code to compile.
output_file: The output file path.
encoding: The encoding of the LaTeX code.
engine: LaTeX engine (pdflatex, lualatex, xelatex or tectonic)
Returns:
A tuple with three elements:
@ -264,8 +268,13 @@ def compile(latex_code: str, output_file: str = '', encoding: str = 'utf-8') ->
- A list of warnings.
"""
assert engine in get_args(LatexEngine), "engine must be pdflatex, lualatex, xelatex or tectonic"
with tempfile.TemporaryDirectory() as tmp_path:
command = ['pdflatex', '-halt-on-error', '--output-directory', tmp_path]
if engine == 'tectonic':
command = ['tectonic', '--outdir', tmp_path, '-']
else:
command = [engine, '--halt-on-error', '--output-directory', tmp_path]
errors: list[str] = []
warnings: list[str] = []

View File

@ -169,6 +169,8 @@ def test_latex_render():
f.write(doc.to_latex())
assert doc.to_pdf('tests/out/test_latex_render1.pdf', font_family='serif')
else:
assert doc.to_pdf('', font_family='serif') # Write only to temp folder
if __name__ == '__main__':

View File

@ -103,6 +103,8 @@ def test_latex_render():
f.write(doc.to_latex())
assert doc.to_pdf('tests/out/test_latex_render2.pdf', font_family='serif')
else:
assert doc.to_pdf('', font_family='serif') # Write only to temp folder
if __name__ == '__main__':