Skip to content

Commit 2536b7f

Browse files
christopher-hampsonJasonGrace2282pre-commit-ci[bot]
authored
feat: preserve original TeX colors (#3903)
* feat: preserve original Tex colors * added test for preserving tex color * fix: fixed breaking change to tex color inheritance * updated docstring and comment * Update manim/mobject/text/tex_mobject.py Co-authored-by: Aarush Deshpande <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Aarush Deshpande <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 1097ed9 commit 2536b7f

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

manim/mobject/text/tex_mobject.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from __future__ import annotations
1414

15-
from manim.utils.color import ManimColor
15+
from manim.utils.color import BLACK, ManimColor, ParsableManimColor
1616

1717
__all__ = [
1818
"SingleStringMathTex",
@@ -62,12 +62,11 @@ def __init__(
6262
tex_environment: str = "align*",
6363
tex_template: TexTemplate | None = None,
6464
font_size: float = DEFAULT_FONT_SIZE,
65+
color: ParsableManimColor | None = None,
6566
**kwargs,
6667
):
67-
if kwargs.get("color") is None:
68-
# makes it so that color isn't explicitly passed for these mobs,
69-
# and can instead inherit from the parent
70-
kwargs["color"] = VMobject().color
68+
if color is None:
69+
color = VMobject().color
7170

7271
self._font_size = font_size
7372
self.organize_left_to_right = organize_left_to_right
@@ -88,6 +87,7 @@ def __init__(
8887
should_center=should_center,
8988
stroke_width=stroke_width,
9089
height=height,
90+
color=color,
9191
path_string_config={
9292
"should_subdivide_sharp_curves": True,
9393
"should_remove_null_curves": True,
@@ -210,10 +210,16 @@ def get_tex_string(self):
210210
return self.tex_string
211211

212212
def init_colors(self, propagate_colors=True):
213-
if config.renderer == RendererType.OPENGL:
214-
super().init_colors()
215-
elif config.renderer == RendererType.CAIRO:
216-
super().init_colors(propagate_colors=propagate_colors)
213+
for submobject in self.submobjects:
214+
# needed to preserve original (non-black)
215+
# TeX colors of individual submobjects
216+
if submobject.color != BLACK:
217+
continue
218+
submobject.color = self.color
219+
if config.renderer == RendererType.OPENGL:
220+
submobject.init_colors()
221+
elif config.renderer == RendererType.CAIRO:
222+
submobject.init_colors(propagate_colors=propagate_colors)
217223

218224

219225
class MathTex(SingleStringMathTex):
@@ -426,6 +432,10 @@ def sort_alphabetically(self):
426432
class Tex(MathTex):
427433
r"""A string compiled with LaTeX in normal mode.
428434
435+
The color can be set using
436+
the ``color`` argument. Any parts of the ``tex_string`` that are colored by the
437+
TeX commands ``\color`` or ``\textcolor`` will retain their original color.
438+
429439
Tests
430440
-----
431441

tests/test_graphical_units/test_tex_mobject.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,33 @@ def test_set_opacity_by_tex(scene):
2424
tex = MathTex("f(x) = y", substrings_to_isolate=["f(x)"])
2525
tex.set_opacity_by_tex("f(x)", 0.2, 0.5)
2626
scene.add(tex)
27+
28+
29+
def test_preserve_tex_color():
30+
"""Test that Tex preserves original tex colors."""
31+
template = TexTemplate(preamble=r"\usepackage{xcolor}")
32+
Tex.set_default(tex_template=template)
33+
34+
txt = Tex(r"\textcolor{red}{Hello} World")
35+
assert len(txt[0].submobjects) == 10
36+
assert all(char.fill_color.to_hex() == "#FF0000" for char in txt[0][:5]) # "Hello"
37+
assert all(
38+
char.fill_color.to_hex() == WHITE.to_hex() for char in txt[0][-5:]
39+
) # "World"
40+
41+
txt = Tex(r"\textcolor{red}{Hello} World", color=BLUE)
42+
assert len(txt[0].submobjects) == 10
43+
assert all(char.fill_color.to_hex() == "#FF0000" for char in txt[0][:5]) # "Hello"
44+
assert all(
45+
char.fill_color.to_hex() == BLUE.to_hex() for char in txt[0][-5:]
46+
) # "World"
47+
48+
Tex.set_default(color=GREEN)
49+
txt = Tex(r"\textcolor{red}{Hello} World")
50+
assert len(txt[0].submobjects) == 10
51+
assert all(char.fill_color.to_hex() == "#FF0000" for char in txt[0][:5]) # "Hello"
52+
assert all(
53+
char.fill_color.to_hex() == GREEN.to_hex() for char in txt[0][-5:]
54+
) # "World"
55+
56+
Tex.set_default()

0 commit comments

Comments
 (0)