From b242ec3e2b71c4d0420c5f12ac75c71413256157 Mon Sep 17 00:00:00 2001 From: Javier Carreira <140205325+SupernovaIa@users.noreply.github.com> Date: Sat, 4 Jul 2026 22:01:38 +0200 Subject: [PATCH 1/2] docs(text): document whitespace-stripped indexing in Text Text.__init__ strips spaces/newlines from self.text and never creates submobjects for whitespace, so slicing (text[a:b]) and the t2c/t2s/t2w/t2f/t2g '[a:b]' syntax silently index into that stripped copy rather than the original string, with no warning. Add a docstring callout explaining this and pointing to substring-keyed t2c/etc. as the unaffected alternative. Refs #4864 --- manim/mobject/text/text_mobject.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/manim/mobject/text/text_mobject.py b/manim/mobject/text/text_mobject.py index d484420301..0f5fe5e859 100644 --- a/manim/mobject/text/text_mobject.py +++ b/manim/mobject/text/text_mobject.py @@ -305,6 +305,20 @@ class Text(SVGMobject): Text objects behave like a :class:`.VGroup`-like iterable of all characters in the given text. In particular, slicing is possible. + .. warning:: + + Whitespace and newline characters are stripped internally and never + become their own submobject (there is nothing to render for them), so + indices used for slicing (``my_text[3:5]``) or for the slice syntax in + ``t2c``/``t2s``/``t2w``/``t2f``/``t2g`` (e.g. ``t2c={'[3:5]': RED}``) + refer to positions in the text *with whitespace removed*, not to + positions in the original ``text`` argument. For example, in + ``Text("Hello world")``, index ``5`` refers to ``"w"``, not to the + space between the two words. When the substring you want to select is + known in advance, prefer keying ``t2c``/``t2s``/``t2w``/``t2f``/``t2g`` + by that substring directly (e.g. ``t2c={"world": RED}``), which matches + by text search instead of by index and is unaffected by this quirk. + Parameters ---------- text From 5eea67b839529b5cd6e7315a999b37dff2ec0b40 Mon Sep 17 00:00:00 2001 From: Javier Carreira <140205325+SupernovaIa@users.noreply.github.com> Date: Wed, 8 Jul 2026 22:17:59 +0200 Subject: [PATCH 2/2] docs(text): fix t2c/t2s/etc slice-index whitespace claim Reviewer (chopan050) pointed out on PR #4866 that t2c/t2s/t2w/t2f/t2g slice syntax (e.g. t2c={'[3:7]': RED}) indexes into the original text with whitespace included, not the whitespace-stripped text as the previous wording implied -- the opposite of how direct object slicing (my_text[3:5]) behaves. Verified both behaviors empirically and reworded the warning to describe each convention correctly. --- manim/mobject/text/text_mobject.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/manim/mobject/text/text_mobject.py b/manim/mobject/text/text_mobject.py index 0f5fe5e859..7e9eb88d8a 100644 --- a/manim/mobject/text/text_mobject.py +++ b/manim/mobject/text/text_mobject.py @@ -309,15 +309,24 @@ class Text(SVGMobject): Whitespace and newline characters are stripped internally and never become their own submobject (there is nothing to render for them), so - indices used for slicing (``my_text[3:5]``) or for the slice syntax in - ``t2c``/``t2s``/``t2w``/``t2f``/``t2g`` (e.g. ``t2c={'[3:5]': RED}``) - refer to positions in the text *with whitespace removed*, not to - positions in the original ``text`` argument. For example, in - ``Text("Hello world")``, index ``5`` refers to ``"w"``, not to the - space between the two words. When the substring you want to select is - known in advance, prefer keying ``t2c``/``t2s``/``t2w``/``t2f``/``t2g`` - by that substring directly (e.g. ``t2c={"world": RED}``), which matches - by text search instead of by index and is unaffected by this quirk. + slicing indices are interpreted differently depending on where they + are used, and the two conventions disagree with each other: + + - Slicing the object itself (``my_text[3:5]``) indexes into the + rendered characters, i.e. the text *with whitespace removed*. For + ``Text("Hello world")``, index ``5`` refers to ``"w"``, not to the + space between the two words. + - The slice syntax in ``t2c``/``t2s``/``t2w``/``t2f``/``t2g`` + (e.g. ``t2c={'[3:7]': RED}``) indexes into the *original* ``text`` + argument, whitespace included. For ``Text("Hello World")``, + ``t2c={'[3:7]': RED}`` colors ``"l"``, ``"o"``, ``"W"`` (the space + at index 5 falls in range but has nothing to color), whereas + ``my_text[3:7]`` selects the 4 rendered characters ``"loWo"``. + + When the substring you want to select is known in advance, prefer + keying ``t2c``/``t2s``/``t2w``/``t2f``/``t2g`` by that substring + directly (e.g. ``t2c={"world": RED}``), which matches by text search + instead of by index and is unaffected by this quirk. Parameters ----------