Skip to content
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

Allow disabling color inheritance for TeX mobjects #3643

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions docs/source/guides/using_text.rst
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,52 @@ we have to add it manually.
)
self.add(tex)

You can use the ``xcolor`` package to apply colors within TeX, but to do this
you have to explicitly state that you want to preserve these colors (as the
default is to override them with whatever the default :class:`~.VMobject`
color is):

.. manim:: XColor
:save_last_frame:

class XColor(Scene):
def construct(self):
myTemplate = TexTemplate()
myTemplate.add_to_preamble(r"\usepackage{xcolor}")
tex = Tex(
r"\textcolor{white}{Hello} \textcolor{yellow}{\LaTeX}",
tex_template=myTemplate,
font_size=144,
preserve_colors=True,
)
self.add(tex)

You can use :func:`manim.mobject.mobject.Mobject.set_default` to make this the
default:

.. manim:: DefaultTexTemplate
:save_last_frame:

class DefaultTexTemplate(Scene):
def construct(self):
myTemplate = TexTemplate()
myTemplate.add_to_preamble(r"\usepackage{xcolor}")
myTemplate.add_to_document(r"\color{white}")

Tex.set_default(
tex_template=myTemplate,
preserve_colors=True
)

tex = Tex(
r"Hello \textcolor{yellow}{\LaTeX}",
font_size=144,
)
self.add(tex)

# Restore original defaults, to avoid breaking documentation
Tex.set_default()

Substrings and parts
====================

Expand Down
33 changes: 32 additions & 1 deletion manim/mobject/text/tex_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@
class SingleStringMathTex(SVGMobject):
"""Elementary building block for rendering text with LaTeX.

Parameters
----------
tex_string
The TeX source string to render.
stroke_width
The stroke width of the mobject. If ``None`` (the default),
the stroke width values set in the generated SVG file are used.
should_center
Whether or not the mobject should be centered after
being imported.
height
organize_left_to_right
tex_environment
The TeX environment to wrap the string in.
tex_template
The TeX template to use when rendering the string.
font_size
preserve_colors
If ``False`` (the default), the mobject is recolored based on
the default :class:`~.VMobject` color.
If ``True``, the colors in the TeX document are used.

.. warning::
By default, TeX strings are coloured black, meaning they
will be invisible against a black background.
If you want to use this option, it is recommended to set
a default color in TeX.
kwargs
Further arguments passed to the parent class.

Tests
-----
Check that creating a :class:`~.SingleStringMathTex` object works::
Expand All @@ -62,9 +92,10 @@ def __init__(
tex_environment: str = "align*",
tex_template: TexTemplate | None = None,
font_size: float = DEFAULT_FONT_SIZE,
preserve_colors: bool = False,
**kwargs,
):
if kwargs.get("color") is None:
if kwargs.get("color") is None and not preserve_colors:
# makes it so that color isn't explicitly passed for these mobs,
# and can instead inherit from the parent
kwargs["color"] = VMobject().color
Expand Down
19 changes: 15 additions & 4 deletions tests/test_graphical_units/test_tex_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@
@frames_comparison
def test_color_inheritance(scene):
"""Test that Text and MarkupText correctly inherit colour from
their parent class."""
their parent class when the preserve_colors argument is unset."""

VMobject.set_default(color=RED)
tex = Tex("test color inheritance")
mathtex = MathTex("test color inheritance")
vgr = VGroup(tex, mathtex).arrange()

template = config.tex_template.copy()
template.add_to_preamble(r"\usepackage{xcolor}")

text = r"test color \textcolor{green}{inheritance}"

tex_inherit = Tex(text, tex_template=template)
mathtex_inherit = MathTex(text, tex_template=template)
tex_preserve = Tex(text, tex_template=template, preserve_colors=True)
mathtex_preserve = MathTex(text, tex_template=template, preserve_colors=True)

vgr = VGroup(tex_inherit, mathtex_inherit, tex_preserve, mathtex_preserve).arrange(
DOWN
)
VMobject.set_default()

scene.add(vgr)
Expand Down