Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

docs(text): document whitespace-stripped indexing in Text (#4866)

Javier Carreira7bef6c0e4ca1
diff --git a/manim/mobject/text/text_mobject.py b/manim/mobject/text/text_mobject.py
index d48442030..7e9eb88d8 100644
--- a/manim/mobject/text/text_mobject.py
+++ b/manim/mobject/text/text_mobject.py
@@ -305,6 +305,29 @@ 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
+ 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
----------
text