Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Added documentation for `index_labels`. (#2465)

Laith Bahodi962c24684a43
diff --git a/docs/source/reference_index/utilities_misc.rst b/docs/source/reference_index/utilities_misc.rst
index e69c26c23..26ec07020 100644
--- a/docs/source/reference_index/utilities_misc.rst
+++ b/docs/source/reference_index/utilities_misc.rst
@@ -15,6 +15,7 @@ Module Index
~utils.commands
~utils.config_ops
~utils.deprecation
+ ~utils.debug
~utils.hashing
~utils.ipython_magic
~utils.images
diff --git a/docs/source/tutorials/using_text.rst b/docs/source/tutorials/using_text.rst
index 7d0acd46a..723438e8c 100644
--- a/docs/source/tutorials/using_text.rst
+++ b/docs/source/tutorials/using_text.rst
@@ -45,7 +45,9 @@ For example:
class SingleLineColor(Scene):
def construct(self):
- text = MarkupText(f'all in red <span fgcolor="{YELLOW}">except this</span>', color=RED)
+ text = MarkupText(
+ f'all in red <span fgcolor="{YELLOW}">except this</span>', color=RED
+ )
self.add(text)
.. _Pango library: https://pango.gnome.org
@@ -105,7 +107,15 @@ Weight specifies the boldness of a font. You can see a list of weights in
import manimpango
g = VGroup()
- weight_list = dict(sorted({weight: manimpango.Weight(weight).value for weight in manimpango.Weight}.items(), key=lambda x: x[1]))
+ weight_list = dict(
+ sorted(
+ {
+ weight: manimpango.Weight(weight).value
+ for weight in manimpango.Weight
+ }.items(),
+ key=lambda x: x[1],
+ )
+ )
for weight in weight_list:
g += Text(weight.name, weight=weight.name, font="Open Sans")
self.add(g.arrange(DOWN).scale(0.5))
@@ -367,7 +377,11 @@ we have to add it manually.
def construct(self):
myTemplate = TexTemplate()
myTemplate.add_to_preamble(r"\usepackage{mathrsfs}")
- tex = Tex(r'$\mathscr{H} \rightarrow \mathbb{H}$}', tex_template=myTemplate, font_size=144)
+ tex = Tex(
+ r"$\mathscr{H} \rightarrow \mathbb{H}$}",
+ tex_template=myTemplate,
+ font_size=144,
+ )
self.add(tex)
Substrings and parts
@@ -429,6 +443,31 @@ will consist of the substrings ``a^2``, ``+``, ``b^2``, ``=``, and ``c^2``.
This makes transformations between similar text fragments easy
to write using :class:`~.TransformMatchingTex`.
+Using ``index_labels`` to work with complicated strings
+=======================================================
+
+You might sometimes be working with a very complicated :class:`~.MathTex` mobject
+that makes it difficult to work with its individual components. This is
+where the debugging function :func:`.index_labels` is very useful.
+
+The method shows the index of a mobject's submobjects, allowing you
+to easily find the components of the mobject you would like to change.
+
+.. manim:: IndexLabelsMathTex
+ :save_last_frame:
+
+ class IndexLabelsMathTex(Scene):
+ def construct(self):
+ text = MathTex(r"\binom{2n}{n+2}", font_size=96)
+
+ # index the first (and only) term of the MathTex mob
+ self.add(index_labels(text[0]))
+
+ text[0][1:3].set_color(YELLOW)
+ text[0][3:6].set_color(RED)
+ self.add(text)
+
+
LaTeX Maths Fonts - The Template Library
========================================
@@ -442,7 +481,11 @@ ready for you to use. These templates will all work in math mode:
class LaTeXMathFonts(Scene):
def construct(self):
- tex = Tex(r'$x^2 + y^2 = z^2$', tex_template=TexFontTemplates.french_cursive, font_size=144)
+ tex = Tex(
+ r"$x^2 + y^2 = z^2$",
+ tex_template=TexFontTemplates.french_cursive,
+ font_size=144,
+ )
self.add(tex)
Manim also has a :class:`~.TexTemplateLibrary` containing the TeX
diff --git a/manim/utils/debug.py b/manim/utils/debug.py
index 7b67bda82..0a1eca7c6 100644
--- a/manim/utils/debug.py
+++ b/manim/utils/debug.py
@@ -6,6 +6,10 @@ from __future__ import annotations
__all__ = ["print_family", "index_labels"]
+from colour import Color
+
+from manim.mobject.mobject import Mobject
+
from ..mobject.numbers import Integer
from ..mobject.types.vectorized_mobject import VGroup
from .color import BLACK
@@ -18,12 +22,62 @@ def print_family(mobject, n_tabs=0):
print_family(submob, n_tabs + 1)
-def index_labels(mobject, label_height=0.15):
+def index_labels(
+ mobject: Mobject,
+ label_height: float = 0.15,
+ background_stroke_width=5,
+ background_stroke_color=BLACK,
+ **kwargs
+):
+ r"""Returns a :class:`~.VGroup` of :class:`~.Integer` mobjects
+ that shows the index of each submobject.
+
+ Useful for working with parts of complicated mobjects.
+
+ Parameters
+ ----------
+ mobject
+ The mobject that will have its submobjects labelled.
+ label_height
+ The height of the labels, by default 0.15.
+ background_stroke_width
+ The stroke width of the outline of the labels, by default 5.
+ background_stroke_color
+ The stroke color of the outline of labels.
+ kwargs
+ Additional parameters to be passed into the :class`~.Integer`
+ mobjects used to construct the labels.
+
+ Examples
+ --------
+ .. manim:: IndexLabelsExample
+ :save_last_frame:
+
+ class IndexLabelsExample(Scene):
+ def construct(self):
+ text = MathTex(
+ "\\frac{d}{dx}f(x)g(x)=",
+ "f(x)\\frac{d}{dx}g(x)",
+ "+",
+ "g(x)\\frac{d}{dx}f(x)",
+ )
+
+ #index the fist term in the MathTex mob
+ indices = index_labels(text[0])
+
+ text[0][1].set_color(PURPLE_B)
+ text[0][8:12].set_color(DARK_BLUE)
+
+ self.add(text, indices)
+ """
+
labels = VGroup()
for n, submob in enumerate(mobject):
- label = Integer(n)
+ label = Integer(n, **kwargs)
+ label.set_stroke(
+ background_stroke_color, background_stroke_width, background=True
+ )
label.height = label_height
label.move_to(submob)
- label.set_stroke(BLACK, 5, background=True)
labels.add(label)
return labels