Skip to content

Commit

Permalink
feat: preserve original TeX colors (ManimCommunity#3903)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Sep 1, 2024
1 parent 1097ed9 commit 2536b7f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
28 changes: 19 additions & 9 deletions manim/mobject/text/tex_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from __future__ import annotations

from manim.utils.color import ManimColor
from manim.utils.color import BLACK, ManimColor, ParsableManimColor

__all__ = [
"SingleStringMathTex",
Expand Down Expand Up @@ -62,12 +62,11 @@ def __init__(
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
Expand All @@ -88,6 +87,7 @@ def __init__(
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,
Expand Down Expand Up @@ -210,10 +210,16 @@ def get_tex_string(self):
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):
Expand Down Expand Up @@ -426,6 +432,10 @@ def sort_alphabetically(self):
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
-----
Expand Down
30 changes: 30 additions & 0 deletions tests/test_graphical_units/test_tex_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 2536b7f

Please sign in to comment.