Skip to content

Commit

Permalink
Added "tex_bin_dir" config option for specifying a custom TeX binarie…
Browse files Browse the repository at this point in the history
…s folder.
  • Loading branch information
levimcgomes committed Sep 2, 2023
1 parent eac99e8 commit 5625c48
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
13 changes: 13 additions & 0 deletions manim/_config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ class MyScene(Scene):
"save_pngs",
"scene_names",
"show_in_file_browser",
"tex_bin_dir",
"tex_dir",
"tex_template",
"tex_template_file",
Expand Down Expand Up @@ -614,6 +615,7 @@ def digest_parser(self, parser: configparser.ConfigParser) -> ManimConfig:
"background_color",
"renderer",
"window_position",
"tex_bin_dir",
]:
setattr(self, key, parser["CLI"].get(key, fallback="", raw=True))

Expand Down Expand Up @@ -835,6 +837,10 @@ def digest_args(self, args: argparse.Namespace) -> ManimConfig:
if getattr(args, "gui_location") is not None:
self.gui_location = args.gui_location

# Handle --tex_bin_dir flag.
if hasattr(args, "tex_bin_dir") and getattr(args, "tex_bin_dir") is not None:
self.tex_bin_dir = args.tex_bin_dir

return self

def digest_file(self, filename: str | os.PathLike) -> ManimConfig:
Expand Down Expand Up @@ -1468,6 +1474,7 @@ def get_dir(self, key: str, **kwargs: str) -> Path:
"input_file",
"output_file",
"partial_movie_dir",
"tex_bin_dir",
]
if key not in dirs:
raise KeyError(
Expand Down Expand Up @@ -1566,6 +1573,12 @@ def _set_dir(self, key: str, val: str | Path):
doc="Output file name (-o).",
)

tex_bin_dir = property(
lambda self: self._d["tex_bin_dir"],
lambda self, val: self._set_dir("tex_bin_dir", val),
doc="Path to custom TeX binaries folder.",
)

scene_names = property(
lambda self: self._d["scene_names"],
lambda self, val: self._d.__setitem__("scene_names", val),
Expand Down
11 changes: 9 additions & 2 deletions manim/cli/checkhealth/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

import os
import pathlib

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'pathlib' is not used.
import shutil
import subprocess
from typing import Callable
Expand Down Expand Up @@ -154,7 +155,10 @@ def is_ffmpeg_working():
),
)
def is_latex_available():
path_to_latex = shutil.which("latex")
if config.get_dir("tex_bin_dir") is not None:
path_to_latex = config.get_dir("tex_bin_dir") / "latex.exe"
else:
path_to_latex = shutil.which("latex")
return path_to_latex is not None and os.access(path_to_latex, os.X_OK)


Expand All @@ -169,5 +173,8 @@ def is_latex_available():
skip_on_failed=[is_latex_available],
)
def is_dvisvgm_available():
path_to_dvisvgm = shutil.which("dvisvgm")
if config.get_dir("tex_bin_dir") is not None:
path_to_dvisvgm = config.get_dir("tex_bin_dir") / "dvisvgm.exe"
else:
path_to_dvisvgm = shutil.which("dvisvgm")
return path_to_dvisvgm is not None and os.access(path_to_dvisvgm, os.X_OK)
24 changes: 21 additions & 3 deletions manim/utils/tex_file_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,17 @@ def tex_compilation_command(
:class:`str`
Compilation command according to given parameters
"""
# If the tex_bin_dir is set, prefix all tex executables with it
# Otherwise, default to using the ones on PATH
tex_bin_prefix = ""
if config.get_dir("tex_bin_dir") is not None:
tex_bin_prefix = config.get_dir("tex_bin_dir")

if tex_compiler in {"latex", "pdflatex", "luatex", "lualatex"}:
commands = [
tex_compiler,
tex_compiler
if tex_bin_prefix == ""
else (tex_bin_prefix / tex_compiler).as_posix(),
"-interaction=batchmode",
f'-output-format="{output_format[1:]}"',
"-halt-on-error",
Expand All @@ -142,7 +150,9 @@ def tex_compilation_command(
else:
raise ValueError("xelatex output is either pdf or xdv")
commands = [
"xelatex",
"xelatex"
if tex_bin_prefix == ""
else (tex_bin_prefix / "xelatex").as_posix(),
outflag,
"-interaction=batchmode",
"-halt-on-error",
Expand Down Expand Up @@ -223,10 +233,18 @@ def convert_to_svg(dvi_file: Path, extension: str, page: int = 1):
:class:`Path`
Path to generated SVG file.
"""
# If the tex_bin_dir is set, prefix all tex executables with it
# Otherwise, default to using the ones on PATH
tex_bin_prefix = ""
if config.get_dir("tex_bin_dir") is not None:
tex_bin_prefix = config.get_dir("tex_bin_dir")

result = dvi_file.with_suffix(".svg")
if not result.exists():
commands = [
"dvisvgm",
"dvisvgm"
if tex_bin_prefix == ""
else (tex_bin_prefix / "dvisvgm").as_posix(),
"--pdf" if extension == ".pdf" else "",
"-p " + str(page),
f'"{dvi_file.as_posix()}"',
Expand Down

0 comments on commit 5625c48

Please sign in to comment.