Skip to content

Commit

Permalink
Added ability to remove non-svg LaTeX files (ManimCommunity#3322)
Browse files Browse the repository at this point in the history
* Added ability to remove latex junk (default True)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fixed tests (hopefully), and whitelisted .tex

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* reverted weird changes from merge

* See previous commit message

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fixed logs-too-long test

* Fixed log output

* Fixed typo ;)

* deleted unused variable

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* moved latex deletion to tex_file_writing.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* removed changes in scene files

* Added caching based on LaTeX expression .svg

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Deleted unused function in delete_old_tex

* make if condition more readable

Co-authored-by: Benjamin Hackl <[email protected]>

* cleaned up svg file check

* changed blacklist -> whitelist for file endings

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Reverted docstring change

* Updated delete_non_svg files docstring

* Changed list to a set

* Update manim/_config/utils.py

* Update manim/cli/render/global_options.py

* added one test for the no_latex_cleanup config option

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Benjamin Hackl <[email protected]>
Co-authored-by: Tristan Schulz <[email protected]>
  • Loading branch information
4 people authored Nov 4, 2023
1 parent 8fe1665 commit 3962a12
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
9 changes: 9 additions & 0 deletions manim/_config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ class MyScene(Scene):
"write_to_movie",
"zero_pad",
"force_window",
"no_latex_cleanup",
}

def __init__(self) -> None:
Expand Down Expand Up @@ -580,6 +581,7 @@ def digest_parser(self, parser: configparser.ConfigParser) -> ManimConfig:
"use_projection_stroke_shaders",
"enable_wireframe",
"force_window",
"no_latex_cleanup",
]:
setattr(self, key, parser["CLI"].getboolean(key, fallback=False))

Expand Down Expand Up @@ -756,6 +758,7 @@ def digest_args(self, args: argparse.Namespace) -> ManimConfig:
"enable_wireframe",
"force_window",
"dry_run",
"no_latex_cleanup",
]:
if hasattr(args, key):
attr = getattr(args, key)
Expand Down Expand Up @@ -960,6 +963,12 @@ def digest_file(self, filename: str | os.PathLike) -> ManimConfig:
doc="Set to force window when using the opengl renderer",
)

no_latex_cleanup = property(
lambda self: self._d["no_latex_cleanup"],
lambda self, val: self._set_boolean("no_latex_cleanup", val),
doc="Prevents deletion of .aux, .dvi, and .log files produced by Tex and MathTex.",
)

@property
def verbosity(self):
"""Logger verbosity; "DEBUG", "INFO", "WARNING", "ERROR", or "CRITICAL" (-v)."""
Expand Down
6 changes: 6 additions & 0 deletions manim/cli/render/global_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ def validate_gui_location(ctx, param, value):
help="Renders animations without outputting image or video files and disables the window",
default=False,
),
option(
"--no_latex_cleanup",
is_flag=True,
help="Prevents deletion of .aux, .dvi, and .log files produced by Tex and MathTex.",
default=False,
),
)
31 changes: 29 additions & 2 deletions manim/utils/tex_file_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import re
import unicodedata
from pathlib import Path
from typing import Iterable

from manim.utils.tex import TexTemplate

Expand Down Expand Up @@ -51,19 +52,28 @@ def tex_to_svg_file(
if tex_template is None:
tex_template = config["tex_template"]
tex_file = generate_tex_file(expression, environment, tex_template)

# check if svg already exists
svg_file = tex_file.with_suffix(".svg")
if svg_file.exists():
return svg_file

dvi_file = compile_tex(
tex_file,
tex_template.tex_compiler,
tex_template.output_format,
)
return convert_to_svg(dvi_file, tex_template.output_format)
svg_file = convert_to_svg(dvi_file, tex_template.output_format)
if not config["no_latex_cleanup"]:
delete_nonsvg_files()
return svg_file


def generate_tex_file(
expression: str,
environment: str | None = None,
tex_template: TexTemplate | None = None,
):
) -> Path:
"""Takes a tex expression (and an optional tex environment),
and returns a fully formed tex file ready for compilation.
Expand Down Expand Up @@ -251,6 +261,23 @@ def convert_to_svg(dvi_file: Path, extension: str, page: int = 1):
return result


def delete_nonsvg_files(additional_endings: Iterable[str] = ()) -> None:
"""Deletes every file that does not have a suffix in ``(".svg", ".tex", *additional_endings)``
Parameters:
-----------
additional_endings
Additional endings to whitelist
"""

tex_dir = config.get_dir("tex_dir")
file_suffix_whitelist = {".svg", ".tex", *additional_endings}

for f in tex_dir.iterdir():
if f.suffix not in file_suffix_whitelist:
f.unlink()


def print_all_tex_errors(log_file: Path, tex_compiler: str, tex_file: Path) -> None:
if not log_file.exists():
raise RuntimeError(
Expand Down
14 changes: 14 additions & 0 deletions tests/module/mobject/text/test_texmobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,17 @@ def test_tempconfig_resetting_tex_template():
assert config.tex_template.preamble == "Custom preamble!"

assert config.tex_template.preamble != "Custom preamble!"


def test_tex_garbage_collection(tmpdir, monkeypatch):
monkeypatch.chdir(tmpdir)
Path(tmpdir, "media").mkdir()

with tempconfig({"media_dir": "media"}):
tex_without_log = Tex("Hello World!") # f7bc61042256dea9.tex
assert Path("media", "Tex", "f7bc61042256dea9.tex").exists()
assert not Path("media", "Tex", "f7bc61042256dea9.log").exists()

with tempconfig({"media_dir": "media", "no_latex_cleanup": True}):
tex_with_log = Tex("Hello World, again!") # 3ef79eaaa2d0b15b.tex
assert Path("media", "Tex", "3ef79eaaa2d0b15b.log").exists()

0 comments on commit 3962a12

Please sign in to comment.