Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions manim/mobject/text/text_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading