Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Added :meth:`.set_opacity_by_tex` method for setting the opacity of parts of Tex mobjects (#3159)

Matthew Lee117563e1f01a
diff --git a/manim/mobject/text/tex_mobject.py b/manim/mobject/text/tex_mobject.py
index 649b5f8c7..7c3bc5378 100644
--- a/manim/mobject/text/tex_mobject.py
+++ b/manim/mobject/text/tex_mobject.py
@@ -381,6 +381,29 @@ class MathTex(SingleStringMathTex):
part.set_color(color)
return self
+ def set_opacity_by_tex(
+ self, tex: str, opacity: float = 0.5, remaining_opacity: float = None, **kwargs
+ ):
+ """
+ Sets the opacity of the tex specified. If 'remaining_opacity' is specified,
+ then the remaining tex will be set to that opacity.
+
+ Parameters
+ ----------
+ tex
+ The tex to set the opacity of.
+ opacity
+ Default 0.5. The opacity to set the tex to
+ remaining_opacity
+ Default None. The opacity to set the remaining tex to.
+ If None, then the remaining tex will not be changed
+ """
+ if remaining_opacity is not None:
+ self.set_opacity(opacity=remaining_opacity)
+ for part in self.get_parts_by_tex(tex):
+ part.set_opacity(opacity)
+ return self
+
def set_color_by_tex_to_color_map(self, texs_to_color_map, **kwargs):
for texs, color in list(texs_to_color_map.items()):
try:
diff --git a/tests/test_graphical_units/control_data/tex_mobject/set_opacity_by_tex.npz b/tests/test_graphical_units/control_data/tex_mobject/set_opacity_by_tex.npz
new file mode 100644
index 000000000..0d1164611
Binary files /dev/null and b/tests/test_graphical_units/control_data/tex_mobject/set_opacity_by_tex.npz differ
diff --git a/tests/test_graphical_units/test_tex_mobject.py b/tests/test_graphical_units/test_tex_mobject.py
index 82eb72b06..d92162b37 100644
--- a/tests/test_graphical_units/test_tex_mobject.py
+++ b/tests/test_graphical_units/test_tex_mobject.py
@@ -18,3 +18,11 @@ def test_color_inheritance(scene):
VMobject.set_default()
scene.add(vgr)
+
+
+@frames_comparison
+def test_set_opacity_by_tex(scene):
+ """Test that set_opacity_by_tex works correctly."""
+ tex = MathTex("f(x) = y", substrings_to_isolate=["f(x)"])
+ tex.set_opacity_by_tex("f(x)", 0.2, 0.5)
+ scene.add(tex)