From d49723728dea9b617387165692dbbcf25c8db717 Mon Sep 17 00:00:00 2001 From: QuantumSoul <19613657+BinaryQuantumSoul@users.noreply.github.com> Date: Thu, 4 Sep 2025 01:50:33 +0200 Subject: [PATCH 1/2] Regex support for MathTex --- manim/mobject/text/tex_mobject.py | 52 ++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/manim/mobject/text/tex_mobject.py b/manim/mobject/text/tex_mobject.py index 03bc285e79..f32af0304b 100644 --- a/manim/mobject/text/tex_mobject.py +++ b/manim/mobject/text/tex_mobject.py @@ -259,19 +259,18 @@ def __init__( *tex_strings: str, arg_separator: str = " ", substrings_to_isolate: Iterable[str] | None = None, + regexes_to_isolate: Iterable[str] | None = None, tex_to_color_map: dict[str, ParsableManimColor] | None = None, + regex_to_color_map: dict[str, ManimColor] | None = None, tex_environment: str | None = "align*", **kwargs: Any, ): self.tex_template = kwargs.pop("tex_template", config["tex_template"]) self.arg_separator = arg_separator - self.substrings_to_isolate = ( - [] if substrings_to_isolate is None else substrings_to_isolate - ) - if tex_to_color_map is None: - self.tex_to_color_map: dict[str, ParsableManimColor] = {} - else: - self.tex_to_color_map = tex_to_color_map + self.substrings_to_isolate = substrings_to_isolate or [] + self.regexes_to_isolate = regexes_to_isolate or [] + self.tex_to_color_map = tex_to_color_map or {} + self.regex_to_color_map = regex_to_color_map or {} self.tex_environment = tex_environment self.brace_notation_split_occurred = False self.tex_strings = self._break_up_tex_strings(tex_strings) @@ -298,6 +297,7 @@ def __init__( ), ) raise compilation_error + self.set_color_by_regex_to_color_map(self.regex_to_color_map) self.set_color_by_tex_to_color_map(self.tex_to_color_map) if self.organize_left_to_right: @@ -317,13 +317,18 @@ def _break_up_tex_strings(self, tex_strings: Sequence[str]) -> list[str]: # or tex_to_color_map lists. patterns = [] patterns.extend( - [ - f"({re.escape(ss)})" - for ss in it.chain( - self.substrings_to_isolate, - self.tex_to_color_map.keys(), - ) - ], + f"({regex})" + for regex in it.chain( + self.regexes_to_isolate, + self.regex_to_color_map.keys(), + ) + ) + patterns.extend( + f"({re.escape(tex)})" + for tex in it.chain( + self.substrings_to_isolate, + self.tex_to_color_map.keys(), + ) ) pattern = "|".join(patterns) if pattern: @@ -429,6 +434,25 @@ def set_color_by_tex_to_color_map( self.set_color_by_tex(tex, ManimColor(color), **kwargs) return self + def get_parts_by_regex(self, regex): + return VGroup(*(m for m in self.submobjects if re.search(regex, m.get_tex_string()))) + + def set_color_by_regex(self, regex, color): + parts_to_color = self.get_parts_by_regex(regex) + for part in parts_to_color: + part.set_color(color) + return self + + def set_color_by_regex_to_color_map(self, regexes_to_color_map): + for regexes, color in list(regexes_to_color_map.items()): + try: + regexes + "" + self.set_color_by_regex(regexes, color) + except TypeError: + for regex in regexes: + self.set_color_by_regex(regex, color) + return self + def index_of_part(self, part: MathTex) -> int: split_self = self.split() if part not in split_self: From 6ce757dca4b508b7b962d448a701db40d963314d Mon Sep 17 00:00:00 2001 From: QuantumSoul <19613657+BinaryQuantumSoul@users.noreply.github.com> Date: Thu, 4 Sep 2025 01:54:36 +0200 Subject: [PATCH 2/2] Colored MathTex --- manim/mobject/text/tex_mobject.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/manim/mobject/text/tex_mobject.py b/manim/mobject/text/tex_mobject.py index f32af0304b..75da2e334d 100644 --- a/manim/mobject/text/tex_mobject.py +++ b/manim/mobject/text/tex_mobject.py @@ -223,7 +223,7 @@ def init_colors(self, propagate_colors: bool = True) -> Self: return self -class MathTex(SingleStringMathTex): +class MathTex(ColoredSingleStringMathTex): r"""A string compiled with LaTeX in math mode. Examples @@ -261,7 +261,9 @@ def __init__( substrings_to_isolate: Iterable[str] | None = None, regexes_to_isolate: Iterable[str] | None = None, tex_to_color_map: dict[str, ParsableManimColor] | None = None, - regex_to_color_map: dict[str, ManimColor] | None = None, + regex_to_color_map: dict[str, ParsableManimColor] | None = None, + tex_to_tex_color_map: dict[str, ParsableManimColor] | None = None, + regex_to_tex_color_map: dict[str, ParsableManimColor] | None = None, tex_environment: str | None = "align*", **kwargs: Any, ): @@ -271,12 +273,16 @@ def __init__( self.regexes_to_isolate = regexes_to_isolate or [] self.tex_to_color_map = tex_to_color_map or {} self.regex_to_color_map = regex_to_color_map or {} + self.tex_to_tex_color_map = tex_to_tex_color_map or {} + self.regex_to_tex_color_map = regex_to_tex_color_map or {} self.tex_environment = tex_environment self.brace_notation_split_occurred = False self.tex_strings = self._break_up_tex_strings(tex_strings) try: super().__init__( self.arg_separator.join(self.tex_strings), + tex_to_tex_color_map=self.tex_to_tex_color_map, + regex_to_tex_color_map=self.regex_to_tex_color_map, tex_environment=self.tex_environment, tex_template=self.tex_template, **kwargs,