Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

feat: preserve original TeX colors (#3903)

Christopher Hampson2536b7fafbee
diff --git a/manim/mobject/text/tex_mobject.py b/manim/mobject/text/tex_mobject.py
index e8c09bbac..26334a60d 100644
--- a/manim/mobject/text/tex_mobject.py
+++ b/manim/mobject/text/tex_mobject.py
@@ -12,7 +12,7 @@ r"""Mobjects representing text rendered using LaTeX.
from __future__ import annotations
-from manim.utils.color import ManimColor
+from manim.utils.color import BLACK, ManimColor, ParsableManimColor
__all__ = [
"SingleStringMathTex",
@@ -62,12 +62,11 @@ class SingleStringMathTex(SVGMobject):
tex_environment: str = "align*",
tex_template: TexTemplate | None = None,
font_size: float = DEFAULT_FONT_SIZE,
+ color: ParsableManimColor | None = None,
**kwargs,
):
- if kwargs.get("color") is None:
- # makes it so that color isn't explicitly passed for these mobs,
- # and can instead inherit from the parent
- kwargs["color"] = VMobject().color
+ if color is None:
+ color = VMobject().color
self._font_size = font_size
self.organize_left_to_right = organize_left_to_right
@@ -88,6 +87,7 @@ class SingleStringMathTex(SVGMobject):
should_center=should_center,
stroke_width=stroke_width,
height=height,
+ color=color,
path_string_config={
"should_subdivide_sharp_curves": True,
"should_remove_null_curves": True,
@@ -210,10 +210,16 @@ class SingleStringMathTex(SVGMobject):
return self.tex_string
def init_colors(self, propagate_colors=True):
- if config.renderer == RendererType.OPENGL:
- super().init_colors()
- elif config.renderer == RendererType.CAIRO:
- super().init_colors(propagate_colors=propagate_colors)
+ for submobject in self.submobjects:
+ # needed to preserve original (non-black)
+ # TeX colors of individual submobjects
+ if submobject.color != BLACK:
+ continue
+ submobject.color = self.color
+ if config.renderer == RendererType.OPENGL:
+ submobject.init_colors()
+ elif config.renderer == RendererType.CAIRO:
+ submobject.init_colors(propagate_colors=propagate_colors)
class MathTex(SingleStringMathTex):
@@ -426,6 +432,10 @@ class MathTex(SingleStringMathTex):
class Tex(MathTex):
r"""A string compiled with LaTeX in normal mode.
+ The color can be set using
+ the ``color`` argument. Any parts of the ``tex_string`` that are colored by the
+ TeX commands ``\color`` or ``\textcolor`` will retain their original color.
+
Tests
-----
diff --git a/tests/test_graphical_units/test_tex_mobject.py b/tests/test_graphical_units/test_tex_mobject.py
index 3286352a8..aabc8e01f 100644
--- a/tests/test_graphical_units/test_tex_mobject.py
+++ b/tests/test_graphical_units/test_tex_mobject.py
@@ -24,3 +24,33 @@ def test_set_opacity_by_tex(scene):
tex = MathTex("f(x) = y", substrings_to_isolate=["f(x)"])
tex.set_opacity_by_tex("f(x)", 0.2, 0.5)
scene.add(tex)
+
+
+def test_preserve_tex_color():
+ """Test that Tex preserves original tex colors."""
+ template = TexTemplate(preamble=r"\usepackage{xcolor}")
+ Tex.set_default(tex_template=template)
+
+ txt = Tex(r"\textcolor{red}{Hello} World")
+ assert len(txt[0].submobjects) == 10
+ assert all(char.fill_color.to_hex() == "#FF0000" for char in txt[0][:5]) # "Hello"
+ assert all(
+ char.fill_color.to_hex() == WHITE.to_hex() for char in txt[0][-5:]
+ ) # "World"
+
+ txt = Tex(r"\textcolor{red}{Hello} World", color=BLUE)
+ assert len(txt[0].submobjects) == 10
+ assert all(char.fill_color.to_hex() == "#FF0000" for char in txt[0][:5]) # "Hello"
+ assert all(
+ char.fill_color.to_hex() == BLUE.to_hex() for char in txt[0][-5:]
+ ) # "World"
+
+ Tex.set_default(color=GREEN)
+ txt = Tex(r"\textcolor{red}{Hello} World")
+ assert len(txt[0].submobjects) == 10
+ assert all(char.fill_color.to_hex() == "#FF0000" for char in txt[0][:5]) # "Hello"
+ assert all(
+ char.fill_color.to_hex() == GREEN.to_hex() for char in txt[0][-5:]
+ ) # "World"
+
+ Tex.set_default()