Personal tex compilation fixes
This commit is contained in:
1 parent
ec6ac9580b
commit
705db1068f
8 files changed
+217
-21
No files matched your search
+39
-3
@@ -149,9 +149,43 @@ def report_latex_warning(output_pdf_path) -> None:
|
||||
print(f"{LATEX_WARNING_MARKER}{Path(output_pdf_path).resolve()}")
|
||||
|
||||
|
||||
def compile_to_pdf(text, output_pdf_path, *, strict=False):
|
||||
def fragment_latex_header(source_path=None):
|
||||
"""Keep source packages/macros while using the configured fragment layout."""
|
||||
if source_path is None:
|
||||
return LATEX_BEFORE
|
||||
source = Path(source_path).read_text(encoding="utf-8")
|
||||
# Ignore commented document commands when locating the preamble.
|
||||
source = re.sub(r"(?m)(?<!\\)%[^\n]*", "", source)
|
||||
begin = re.search(r"\\begin\s*\{document\}", source)
|
||||
document_class = r"\\documentclass(?:\s*\[[^\]]*\])?\s*\{[^}]+\}"
|
||||
configured_class = re.search(document_class, LATEX_BEFORE)
|
||||
configured_begin = re.search(r"\\begin\s*\{document\}", LATEX_BEFORE)
|
||||
if begin is None or configured_class is None or configured_begin is None:
|
||||
return LATEX_BEFORE
|
||||
preamble = source[:begin.start()]
|
||||
if not re.search(document_class, preamble):
|
||||
return LATEX_BEFORE
|
||||
preamble = re.sub(document_class, lambda _: configured_class.group(), preamble, count=1)
|
||||
# geometry overrides standalone's crop coordinates and can produce blank or
|
||||
# clipped PDFs even when LaTeX succeeds. Keep other source packages intact.
|
||||
def without_geometry(match):
|
||||
packages = [name.strip() for name in match.group(2).split(',')
|
||||
if name.strip() != 'geometry']
|
||||
return (r'\usepackage' + (match.group(1) or '') + '{' + ','.join(packages) + '}'
|
||||
if packages else '')
|
||||
preamble = re.sub(r'\\usepackage\s*(\[[^\]]*\])?\s*\{([^}]+)\}',
|
||||
without_geometry, preamble)
|
||||
preamble = re.sub(r'\\(?:geometry|newgeometry)\s*\{[^}]*\}', '', preamble)
|
||||
return preamble + LATEX_BEFORE[configured_begin.start():]
|
||||
|
||||
|
||||
def compile_to_pdf(text, output_pdf_path, *, strict=False, source_path=None):
|
||||
"""Wraps text in standalone header/footer templates and compiles it to PDF."""
|
||||
latex_template = f"{LATEX_BEFORE}{text}{LATEX_AFTER}"
|
||||
latex_template = f"{fragment_latex_header(source_path)}{text}{LATEX_AFTER}"
|
||||
env = os.environ.copy()
|
||||
if source_path is not None:
|
||||
source_dir = str(Path(source_path).resolve().parent)
|
||||
env['TEXINPUTS'] = source_dir + os.pathsep + env.get('TEXINPUTS', '')
|
||||
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
tex_filename = 'text.tex'
|
||||
@@ -170,6 +204,7 @@ def compile_to_pdf(text, output_pdf_path, *, strict=False):
|
||||
result = subprocess.run(
|
||||
['pdflatex', '-interaction=nonstopmode', tex_filename],
|
||||
cwd=temp_dir,
|
||||
env=env,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
@@ -180,6 +215,7 @@ def compile_to_pdf(text, output_pdf_path, *, strict=False):
|
||||
result = subprocess.run(
|
||||
['pdflatex', '-interaction=nonstopmode', tex_filename],
|
||||
cwd=temp_dir,
|
||||
env=env,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
@@ -188,7 +224,7 @@ def compile_to_pdf(text, output_pdf_path, *, strict=False):
|
||||
|
||||
generated_pdf = os.path.join(temp_dir, pdf_filename)
|
||||
pdf_generated = os.path.exists(generated_pdf)
|
||||
if pdf_generated:
|
||||
if pdf_generated and (not strict or result.returncode == 0):
|
||||
shutil.move(generated_pdf, output_pdf_path)
|
||||
|
||||
if result.returncode != 0:
|
||||
|
||||
Reference in new issue
Block a user