From b7eefca98f40b9fc658a5a52295161f7e620baa0 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Fri, 27 Oct 2023 09:28:23 +0200 Subject: [PATCH] Fix incorrect submobject count of multi-part Tex/MathTex mobjects by stopping them from adding empty submobjects (#3423) * do not add a VectorizedPoint as a submobject if SingleStringMathTex renders to empty SVG * test new behavior * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update tests/module/mobject/text/test_texmobject.py * Update tests/module/mobject/text/test_texmobject.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- manim/mobject/text/tex_mobject.py | 4 ---- tests/module/mobject/text/test_texmobject.py | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/manim/mobject/text/tex_mobject.py b/manim/mobject/text/tex_mobject.py index 71a31a72c4..dbdf6e3a50 100644 --- a/manim/mobject/text/tex_mobject.py +++ b/manim/mobject/text/tex_mobject.py @@ -344,10 +344,6 @@ def _break_up_by_substrings(self): curr_index + num_submobs + len("".join(self.arg_separator.split())) ) if num_submobs == 0: - # For cases like empty tex_strings, we want the corresponding - # part of the whole MathTex to be a VectorizedPoint - # positioned in the right part of the MathTex - sub_tex_mob.submobjects = [VectorizedPoint()] last_submob_index = min(curr_index, len(self.submobjects) - 1) sub_tex_mob.move_to(self.submobjects[last_submob_index], RIGHT) else: diff --git a/tests/module/mobject/text/test_texmobject.py b/tests/module/mobject/text/test_texmobject.py index c45681fad2..9103345d18 100644 --- a/tests/module/mobject/text/test_texmobject.py +++ b/tests/module/mobject/text/test_texmobject.py @@ -2,6 +2,7 @@ from pathlib import Path +import numpy as np import pytest from manim import MathTex, SingleStringMathTex, Tex, TexTemplate, config, tempconfig @@ -96,6 +97,23 @@ def test_tex_white_space_and_non_whitespace_args(): assert len(tex[3]) == len("".join(str_part_4.split())) +def test_multi_part_tex_with_empty_parts(): + """Check that if a Tex or MathTex Mobject with multiple + string arguments is created where some of the parts render + as empty SVGs, then the number of family members with points + should still be the same as the snipped in one singular part. + """ + tex_parts = ["(-1)", "^{", "0}"] + one_part_fomula = MathTex("".join(tex_parts)) + multi_part_formula = MathTex(*tex_parts) + + for one_part_glyph, multi_part_glyph in zip( + one_part_fomula.family_members_with_points(), + multi_part_formula.family_members_with_points(), + ): + np.testing.assert_allclose(one_part_glyph.points, multi_part_glyph.points) + + def test_tex_size(): """Check that the size of a :class:`Tex` string is not changed.""" text = Tex("what").center()