Skip to content

feat: preserve original TeX colors #3903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading