Skip to content

Commit 7bef6c0

Browse files
docs(text): document whitespace-stripped indexing in Text (#4866)
* 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 * 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. --------- Co-authored-by: Francisco Manríquez Novoa <49853152+chopan050@users.noreply.github.com>
1 parent 4d25c03 commit 7bef6c0

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

manim/mobject/text/text_mobject.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,29 @@ class Text(SVGMobject):
305305
Text objects behave like a :class:`.VGroup`-like iterable of all characters
306306
in the given text. In particular, slicing is possible.
307307
308+
.. warning::
309+
310+
Whitespace and newline characters are stripped internally and never
311+
become their own submobject (there is nothing to render for them), so
312+
slicing indices are interpreted differently depending on where they
313+
are used, and the two conventions disagree with each other:
314+
315+
- Slicing the object itself (``my_text[3:5]``) indexes into the
316+
rendered characters, i.e. the text *with whitespace removed*. For
317+
``Text("Hello world")``, index ``5`` refers to ``"w"``, not to the
318+
space between the two words.
319+
- The slice syntax in ``t2c``/``t2s``/``t2w``/``t2f``/``t2g``
320+
(e.g. ``t2c={'[3:7]': RED}``) indexes into the *original* ``text``
321+
argument, whitespace included. For ``Text("Hello World")``,
322+
``t2c={'[3:7]': RED}`` colors ``"l"``, ``"o"``, ``"W"`` (the space
323+
at index 5 falls in range but has nothing to color), whereas
324+
``my_text[3:7]`` selects the 4 rendered characters ``"loWo"``.
325+
326+
When the substring you want to select is known in advance, prefer
327+
keying ``t2c``/``t2s``/``t2w``/``t2f``/``t2g`` by that substring
328+
directly (e.g. ``t2c={"world": RED}``), which matches by text search
329+
instead of by index and is unaffected by this quirk.
330+
308331
Parameters
309332
----------
310333
text

0 commit comments

Comments
 (0)