Repository: ManimCommunity/manim
File: manim/utils/tex_file_writing.py (Python)

Code before:


def tex_to_svg_file(
    expression: str,
    environment: str | None = None,
    tex_template: TexTemplate | None = None,
):
    """Takes a tex expression and returns the svg version of the compiled tex

    Parameters
    ----------
    expression
        String containing the TeX expression to be rendered, e.g. ``\\sqrt{2}`` or ``foo``
    environment
        The string containing the environment in which the expression should be typeset, e.g. ``align*``
    tex_template
        Template class used to typesetting. If not set, use default template set via `config["tex_template"]`

    Returns
    -------
    :class:`Path`
        Path to generated SVG file.
    """
    if tex_template is None:
        tex_template = config["tex_template"]
    tex_file, svg = generate_tex_file(expression, environment, tex_template)

    # 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,
    )
    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.


Review comment (suggestion):
I am not sure whether it was necessary to change the output type of `generate_tex_file`: in the current implementation, as far as I am aware, this method always returns the path of a given `.tex` file. We can just use `tex_file.with_suffix(".svg")` to get the path to the -- if it exists -- corresponding svg file. If it exists (which we can and should check here!), `compile_tex` and `convert_to_svg` could be skipped (which is good, as the cleaned state would then be kept).



Code after:
    return hasher.hexdigest()[:16]


def tex_to_svg_file(
    expression: str,
    environment: str | None = None,
    tex_template: TexTemplate | None = None,
):
    """Takes a tex expression and returns the svg version of the compiled tex

    Parameters
    ----------
    expression
        String containing the TeX expression to be rendered, e.g. ``\\sqrt{2}`` or ``foo``
    environment
        The string containing the environment in which the expression should be typeset, e.g. ``align*``
    tex_template
        Template class used to typesetting. If not set, use default template set via `config["tex_template"]`

    Returns
    -------
    :class:`Path`
        Path to generated 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,
    )
    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.