From b1a85378657f3df1ead6646863448b836dd3ac33 Mon Sep 17 00:00:00 2001 From: Taksh Date: Wed, 8 Jul 2026 17:04:02 +0530 Subject: [PATCH 1/2] fix(tex): keep MathTex bounding box stable after shift Empty MathTexPart submobjects inflated the bbox on move; use VectorizedPoint placeholders like the pre-0.20 workaround. Fixes #4643 Co-authored-by: Cursor --- manim/mobject/text/tex_mobject.py | 22 +++++++++----------- tests/module/mobject/text/test_texmobject.py | 12 ++++++++++- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/manim/mobject/text/tex_mobject.py b/manim/mobject/text/tex_mobject.py index 729fbb158b..8e06a8d160 100644 --- a/manim/mobject/text/tex_mobject.py +++ b/manim/mobject/text/tex_mobject.py @@ -34,7 +34,7 @@ from manim.constants import * from manim.mobject.geometry.line import Line from manim.mobject.svg.svg_mobject import SVGMobject -from manim.mobject.types.vectorized_mobject import VGroup, VMobject +from manim.mobject.types.vectorized_mobject import VGroup, VMobject, VectorizedPoint from manim.utils.tex import TexTemplate from manim.utils.tex_file_writing import tex_to_svg_file @@ -522,17 +522,15 @@ def _break_up_by_substrings(self) -> Self: of tex_strings) """ new_submobjects: list[VMobject] = [] - try: - for tex_string, tex_string_id in self._main_matches: - mtp = MathTexPart() - mtp.tex_string = tex_string - mtp.add(*self.id_to_vgroup_dict[tex_string_id].submobjects) - new_submobjects.append(mtp) - except KeyError: - logger.error( - f"MathTex: Could not find SVG group for tex part '{tex_string}' (id: {tex_string_id}). Using fallback to root group." - ) - new_submobjects.append(self.id_to_vgroup_dict["root"]) + for tex_string, tex_string_id in self._main_matches: + mtp = MathTexPart() + mtp.tex_string = tex_string + vgroup = self.id_to_vgroup_dict.get(tex_string_id) + if vgroup is None or len(vgroup.submobjects) == 0: + mtp.add(VectorizedPoint()) + else: + mtp.add(*vgroup.submobjects) + new_submobjects.append(mtp) self.submobjects = new_submobjects return self diff --git a/tests/module/mobject/text/test_texmobject.py b/tests/module/mobject/text/test_texmobject.py index 48297abec6..08c1bc8851 100644 --- a/tests/module/mobject/text/test_texmobject.py +++ b/tests/module/mobject/text/test_texmobject.py @@ -5,7 +5,7 @@ import numpy as np import pytest -from manim import MathTex, SingleStringMathTex, Tex, TexTemplate, tempconfig +from manim import MathTex, RIGHT, SingleStringMathTex, Tex, TexTemplate, UP, tempconfig def test_MathTex(config): @@ -335,3 +335,13 @@ def test_tex_garbage_collection(tmpdir, monkeypatch, config): tex_with_log = Tex("Hello World, again!") # 45b4e7819cc20cb1.tex assert Path("media", "Tex", "45b4e7819cc20cb1.log").exists() + + +def test_mathtex_bounding_box_stable_after_shift(config): + """Regression test for https://github.com/ManimCommunity/manim/issues/4643.""" + parts = [r"\overrightarrow{", "C", "A", "}", "=", r"\bold{", "m", "}"] + math_tex = MathTex(*parts) + width_before, height_before = math_tex.width, math_tex.height + math_tex.shift(2 * RIGHT + UP) + assert math_tex.width == pytest.approx(width_before, rel=0, abs=1e-6) + assert math_tex.height == pytest.approx(height_before, rel=0, abs=1e-6) From 3e6ebb77241fbd61843c08db002878e7b6795dbf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 8 Jul 2026 11:35:08 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- manim/mobject/text/tex_mobject.py | 2 +- tests/module/mobject/text/test_texmobject.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/manim/mobject/text/tex_mobject.py b/manim/mobject/text/tex_mobject.py index 8e06a8d160..c6f6f80244 100644 --- a/manim/mobject/text/tex_mobject.py +++ b/manim/mobject/text/tex_mobject.py @@ -34,7 +34,7 @@ from manim.constants import * from manim.mobject.geometry.line import Line from manim.mobject.svg.svg_mobject import SVGMobject -from manim.mobject.types.vectorized_mobject import VGroup, VMobject, VectorizedPoint +from manim.mobject.types.vectorized_mobject import VectorizedPoint, VGroup, VMobject from manim.utils.tex import TexTemplate from manim.utils.tex_file_writing import tex_to_svg_file diff --git a/tests/module/mobject/text/test_texmobject.py b/tests/module/mobject/text/test_texmobject.py index 08c1bc8851..48cb6e41c7 100644 --- a/tests/module/mobject/text/test_texmobject.py +++ b/tests/module/mobject/text/test_texmobject.py @@ -5,7 +5,7 @@ import numpy as np import pytest -from manim import MathTex, RIGHT, SingleStringMathTex, Tex, TexTemplate, UP, tempconfig +from manim import RIGHT, UP, MathTex, SingleStringMathTex, Tex, TexTemplate, tempconfig def test_MathTex(config):