from __future__ import annotations import re from manimlib.mobject.svg.string_mobject import StringMobject from manimlib.utils.tex_file_writing import display_during_execution from manimlib.utils.tex_file_writing import tex_content_to_svg_file from typing import TYPE_CHECKING if TYPE_CHECKING: from colour import Color import re from typing import Iterable, Union from manimlib.mobject.types.vectorized_mobject import VGroup ManimColor = Union[str, Color] Span = tuple[int, int] Selector = Union[ str, re.Pattern, tuple[Union[int, None], Union[int, None]], Iterable[Union[ str, re.Pattern, tuple[Union[int, None], Union[int, None]] ]] ] SCALE_FACTOR_PER_FONT_POINT = 0.001 class MTex(StringMobject): CONFIG = { "font_size": 48, "alignment": "\\centering", "tex_environment": "align*", "tex_to_color_map": {}, "template": "", "additional_preamble": "", } def __init__(self, tex_string: str, **kwargs): # Prevent from passing an empty string. if not tex_string.strip(): tex_string = "\\\\" self.tex_string = tex_string super().__init__(tex_string, **kwargs) self.set_color_by_tex_to_color_map(self.tex_to_color_map) self.scale(SCALE_FACTOR_PER_FONT_POINT * self.font_size) @property def hash_seed(self) -> tuple: return ( self.__class__.__name__, self.svg_default, self.path_string_config, self.base_color, self.isolate, self.tex_string, self.alignment, self.tex_environment, self.tex_to_color_map, self.template, self.additional_preamble ) def get_file_path_by_content(self, content: str) -> str: with display_during_execution(f"Writing \"{self.tex_string}\""): file_path = tex_content_to_svg_file( content, self.template, self.additional_preamble ) return file_path # Parsing @staticmethod def get_command_matches(string: str) -> list[re.Match]: # Group together adjacent braces pattern = re.compile(r""" (?P\\(?:[a-zA-Z]+|.)) |(?P