Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to remove non-svg LaTeX files #3322

Merged
merged 39 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c635535
Added ability to remove latex junk (default True)
JasonGrace2282 Aug 9, 2023
c801683
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 9, 2023
c4e529e
Merge branch 'main' into latex_junk_remover
JasonGrace2282 Aug 9, 2023
7f1c735
Fixed tests (hopefully), and whitelisted .tex
JasonGrace2282 Aug 9, 2023
f9a0281
Merge branch 'latex_junk_remover' of https://github.com/JasonGrace228…
JasonGrace2282 Aug 9, 2023
e2b8489
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 9, 2023
e29473b
reverted weird changes from merge
JasonGrace2282 Aug 9, 2023
b23c30d
See previous commit message
JasonGrace2282 Aug 9, 2023
5c39ce1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 9, 2023
5750360
Fixed logs-too-long test
JasonGrace2282 Aug 9, 2023
227c269
Fixed log output
JasonGrace2282 Aug 9, 2023
7958f5f
Fixed typo ;)
JasonGrace2282 Aug 9, 2023
e4b2e01
deleted unused variable
JasonGrace2282 Aug 9, 2023
7d1cccb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 9, 2023
03560b8
moved latex deletion to tex_file_writing.py
JasonGrace2282 Aug 11, 2023
7f044cb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2023
f888c51
removed changes in scene files
JasonGrace2282 Aug 11, 2023
1f48107
Merge branch 'latex_junk_remover' of https://github.com/JasonGrace228…
JasonGrace2282 Aug 11, 2023
133ae4d
Added caching based on LaTeX expression .svg
JasonGrace2282 Aug 11, 2023
5a14356
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2023
009d551
Deleted unused function in delete_old_tex
JasonGrace2282 Aug 11, 2023
bf9f12c
make if condition more readable
JasonGrace2282 Aug 11, 2023
d6bab49
cleaned up svg file check
JasonGrace2282 Aug 11, 2023
d7aa545
Merge branch 'latex_junk_remover' of https://github.com/JasonGrace228…
JasonGrace2282 Aug 11, 2023
9a0240e
changed blacklist -> whitelist for file endings
JasonGrace2282 Aug 12, 2023
205b804
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2023
e323c08
Reverted docstring change
JasonGrace2282 Aug 12, 2023
486ba62
Merge branch 'latex_junk_remover' of https://github.com/JasonGrace228…
JasonGrace2282 Aug 12, 2023
6be5927
Updated delete_non_svg files docstring
JasonGrace2282 Aug 12, 2023
40257f7
Changed list to a set
JasonGrace2282 Aug 12, 2023
7860e4a
Merge branch 'main' into latex_junk_remover
JasonGrace2282 Sep 1, 2023
b91e5b8
Merge branch 'main' into latex_junk_remover
JasonGrace2282 Sep 4, 2023
7bc4442
Merge branch 'ManimCommunity:main' into latex_junk_remover
JasonGrace2282 Sep 23, 2023
e29c6d5
Merge branch 'main' into latex_junk_remover
JasonGrace2282 Oct 29, 2023
613bea2
Update manim/_config/utils.py
MrDiver Nov 4, 2023
fd81de7
Update manim/cli/render/global_options.py
MrDiver Nov 4, 2023
a0d71bf
Merge branch 'main' into latex_junk_remover
MrDiver Nov 4, 2023
a91cf07
added one test for the no_latex_cleanup config option
behackl Nov 4, 2023
572e68a
Merge branch 'main' into latex_junk_remover
behackl Nov 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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="Deletes .aux, .dvi, and .log files produced by Tex and MathTex.",
MrDiver marked this conversation as resolved.
Show resolved Hide resolved
)

@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="Deletes .aux, .dvi, and .log files produced by Tex and MathTex.",
MrDiver marked this conversation as resolved.
Show resolved Hide resolved
default=False,
),
)
46 changes: 41 additions & 5 deletions manim/utils/tex_file_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,31 @@ def tex_to_svg_file(
"""
if tex_template is None:
tex_template = config["tex_template"]
tex_file = generate_tex_file(expression, environment, tex_template)
tex_file, svg = generate_tex_file(expression, environment, tex_template)
JasonGrace2282 marked this conversation as resolved.
Show resolved Hide resolved

# check if svg already exists
if isinstance(svg, Path):
return svg

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,
):
) -> tuple[Path, Path | None]:
"""Takes a tex expression (and an optional tex environment),
and returns a fully formed tex file ready for compilation.
Along the way, checks if an svg file for the expression already exists.

Parameters
----------
Expand All @@ -80,6 +89,9 @@ def generate_tex_file(
-------
:class:`Path`
Path to generated TeX file

:class:`Path` | `None`
Path to svg file if it exists, else None
"""
if tex_template is None:
tex_template = config["tex_template"]
Expand All @@ -92,14 +104,21 @@ def generate_tex_file(
if not tex_dir.exists():
tex_dir.mkdir()

result = tex_dir / (tex_hash(output) + ".tex")
output_hash = tex_hash(output)
# check if svg for expression already exists
svg_path = tex_dir / (output_hash + ".svg")
if svg_path.exists():
# svg for expression found
return "_", svg_path

result = tex_dir / (output_hash + ".tex")
if not result.exists():
logger.info(
"Writing %(expression)s to %(path)s",
{"expression": expression, "path": f"{result}"},
)
result.write_text(output, encoding="utf-8")
return result
return result, None


def tex_compilation_command(
Expand Down Expand Up @@ -251,6 +270,23 @@ def convert_to_svg(dvi_file: Path, extension: str, page: int = 1):
return result


def delete_nonsvg_files(additional_endings: tuple[str] = ()) -> None:
"""Deletes the .dvi, .aux, and .log files produced when using `Tex` or `MathTex`
JasonGrace2282 marked this conversation as resolved.
Show resolved Hide resolved

Parameters:
-----------
additional_endings
Additional Endings to remove in Tex folder
"""

tex_files_directory = config.get_dir("tex_dir")

file_endings = (".dvi", ".aux", ".log", *additional_endings)
for file_name in tex_files_directory.iterdir():
if any(file_name.suffix == s for s in file_endings):
JasonGrace2282 marked this conversation as resolved.
Show resolved Hide resolved
file_name.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