61 lines
1.5 KiB
Python

from pathlib import Path
import subprocess
def build(
path: Path,
*,
solutions: bool = True,
mainfile: str = "main.tex",
jobname: str = "main",
build_dir: Path = Path("build"),
test = False,
env: dict = {},
) -> Path:
"""
Build a directory using XeTeX.
Args:
path (Path): directory to build
solutions (bool, optional): Create a handout with solutions? Defaults to True.
mainfile (str, optional): Path to main TeX file, relative to path. Defaults to "main.tex".
jobname (str, optional): TeX job name. Defaults to "main".
build_dir (Path, optional): Where to place build files. Defaults to Path("build").
test (bool, optional): If true, dry run. Defaults to False.
env (dict): Dictionary of environment variables.
Returns:
Path: Absolute path to output pdf.
"""
# Make temporary file for TeX build arg hack
if solutions:
with (build_dir/"tmp.tex").open("w") as f:
f.write("\\def\\argYesSolutions{1}\\input{" + mainfile + "}")
else:
with (build_dir/"tmp.tex").open("w") as f:
f.write("\\def\\argNoSolutions{1}\\input{" + mainfile + "}")
cmd = subprocess.run([
"latexmk",
"-interaction=nonstopmode",
"-file-line-error",
f"-outdir={build_dir}",
"-xelatex",
f"-jobname={jobname}",
build_dir/"tmp.tex"
],
cwd = path,
env = env,
stdout = None if test else subprocess.DEVNULL,
stderr = None if test else subprocess.DEVNULL
)
assert cmd.returncode == 0
# Remove tmp file
(build_dir / "tmp.tex").unlink()
return build_dir / f"{jobname}.pdf"