Skip to content

Commit 3962a12

Browse files
JasonGrace2282pre-commit-ci[bot]behacklMrDiver
authored
Added ability to remove non-svg LaTeX files (#3322)
* 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]>
1 parent 8fe1665 commit 3962a12

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

manim/_config/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ class MyScene(Scene):
309309
"write_to_movie",
310310
"zero_pad",
311311
"force_window",
312+
"no_latex_cleanup",
312313
}
313314

314315
def __init__(self) -> None:
@@ -580,6 +581,7 @@ def digest_parser(self, parser: configparser.ConfigParser) -> ManimConfig:
580581
"use_projection_stroke_shaders",
581582
"enable_wireframe",
582583
"force_window",
584+
"no_latex_cleanup",
583585
]:
584586
setattr(self, key, parser["CLI"].getboolean(key, fallback=False))
585587

@@ -756,6 +758,7 @@ def digest_args(self, args: argparse.Namespace) -> ManimConfig:
756758
"enable_wireframe",
757759
"force_window",
758760
"dry_run",
761+
"no_latex_cleanup",
759762
]:
760763
if hasattr(args, key):
761764
attr = getattr(args, key)
@@ -960,6 +963,12 @@ def digest_file(self, filename: str | os.PathLike) -> ManimConfig:
960963
doc="Set to force window when using the opengl renderer",
961964
)
962965

966+
no_latex_cleanup = property(
967+
lambda self: self._d["no_latex_cleanup"],
968+
lambda self, val: self._set_boolean("no_latex_cleanup", val),
969+
doc="Prevents deletion of .aux, .dvi, and .log files produced by Tex and MathTex.",
970+
)
971+
963972
@property
964973
def verbosity(self):
965974
"""Logger verbosity; "DEBUG", "INFO", "WARNING", "ERROR", or "CRITICAL" (-v)."""

manim/cli/render/global_options.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,10 @@ def validate_gui_location(ctx, param, value):
102102
help="Renders animations without outputting image or video files and disables the window",
103103
default=False,
104104
),
105+
option(
106+
"--no_latex_cleanup",
107+
is_flag=True,
108+
help="Prevents deletion of .aux, .dvi, and .log files produced by Tex and MathTex.",
109+
default=False,
110+
),
105111
)

manim/utils/tex_file_writing.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import re
1414
import unicodedata
1515
from pathlib import Path
16+
from typing import Iterable
1617

1718
from manim.utils.tex import TexTemplate
1819

@@ -51,19 +52,28 @@ def tex_to_svg_file(
5152
if tex_template is None:
5253
tex_template = config["tex_template"]
5354
tex_file = generate_tex_file(expression, environment, tex_template)
55+
56+
# check if svg already exists
57+
svg_file = tex_file.with_suffix(".svg")
58+
if svg_file.exists():
59+
return svg_file
60+
5461
dvi_file = compile_tex(
5562
tex_file,
5663
tex_template.tex_compiler,
5764
tex_template.output_format,
5865
)
59-
return convert_to_svg(dvi_file, tex_template.output_format)
66+
svg_file = convert_to_svg(dvi_file, tex_template.output_format)
67+
if not config["no_latex_cleanup"]:
68+
delete_nonsvg_files()
69+
return svg_file
6070

6171

6272
def generate_tex_file(
6373
expression: str,
6474
environment: str | None = None,
6575
tex_template: TexTemplate | None = None,
66-
):
76+
) -> Path:
6777
"""Takes a tex expression (and an optional tex environment),
6878
and returns a fully formed tex file ready for compilation.
6979
@@ -251,6 +261,23 @@ def convert_to_svg(dvi_file: Path, extension: str, page: int = 1):
251261
return result
252262

253263

264+
def delete_nonsvg_files(additional_endings: Iterable[str] = ()) -> None:
265+
"""Deletes every file that does not have a suffix in ``(".svg", ".tex", *additional_endings)``
266+
267+
Parameters:
268+
-----------
269+
additional_endings
270+
Additional endings to whitelist
271+
"""
272+
273+
tex_dir = config.get_dir("tex_dir")
274+
file_suffix_whitelist = {".svg", ".tex", *additional_endings}
275+
276+
for f in tex_dir.iterdir():
277+
if f.suffix not in file_suffix_whitelist:
278+
f.unlink()
279+
280+
254281
def print_all_tex_errors(log_file: Path, tex_compiler: str, tex_file: Path) -> None:
255282
if not log_file.exists():
256283
raise RuntimeError(

tests/module/mobject/text/test_texmobject.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,17 @@ def test_tempconfig_resetting_tex_template():
212212
assert config.tex_template.preamble == "Custom preamble!"
213213

214214
assert config.tex_template.preamble != "Custom preamble!"
215+
216+
217+
def test_tex_garbage_collection(tmpdir, monkeypatch):
218+
monkeypatch.chdir(tmpdir)
219+
Path(tmpdir, "media").mkdir()
220+
221+
with tempconfig({"media_dir": "media"}):
222+
tex_without_log = Tex("Hello World!") # f7bc61042256dea9.tex
223+
assert Path("media", "Tex", "f7bc61042256dea9.tex").exists()
224+
assert not Path("media", "Tex", "f7bc61042256dea9.log").exists()
225+
226+
with tempconfig({"media_dir": "media", "no_latex_cleanup": True}):
227+
tex_with_log = Tex("Hello World, again!") # 3ef79eaaa2d0b15b.tex
228+
assert Path("media", "Tex", "3ef79eaaa2d0b15b.log").exists()

0 commit comments

Comments
 (0)