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

Conversation

christopher-hampson
Copy link
Contributor

Overview: What does this pull request change?

  • Preserves original colors specified in the tex_string (as set by, for example, \textcolor), while continuing to support colors set by color kwarg and set_color_by_tex method.
  • Added color as an explicit kwarg of SingleStringMathTex with default value WHITE (possible breaking change?)

Motivation and Explanation: Why and how do your changes improve the library?

  • Addresses (closed) issue: #3783
  • Allows more flexibility when specifying colors of parts of TeX text or MathTex formulas.

Links to added or changed documentation pages

Further Information and Comments

  • This appears to work for the cases I have tested but may not be an optimal implementation, so happy to take suggestions on this.
  • I'm also unsure of the reason for the previous way of initializing the default TeX color so this may be a breaking change somewhere that I'm not aware of?
  • Currently I have set the default color as WHITE but this may be better specified by some constant or config value?

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

@JasonGrace2282
Copy link
Member

Hey, thanks for your interest in contributing to Manim!
What benefit does this have compared to just using Manim's built in coloring?

@christopher-hampson
Copy link
Contributor Author

Hey, thanks for your interest in contributing to Manim! What benefit does this have compared to just using Manim's built in coloring?

Hi @JasonGrace2282, the main benefit I see the flexibility in being able to more easily color parts of a Tex mobject, particularly for long tex_strings or equations where indexing characters by position is cumbersome, or where you want to color only one occurrence of a string that appears multiple times in the tex_string (or color different occurrences in different colors), which, I could be mistaken, I don't think is currently possible with set_color_by_tex. e.g.

Tex(r"This \textcolor{red}{example} is an \textcolor{green}{example} of a multi-colored \textcolor{blue}{example}")
MathTex(r"f(x) = \sum_{x=0}^\infty \frac{\textcolor{red}{x}^x}{x!}")

I have also seen a couple of posts, here and elsewhere, either requesting this or initially confused by the current behavior of overriding the original TeX colors, so this may have the benefit of reducing such queries. e.g. #3783, #3577

(I think this may also help with some issues around isolating substrings in expressions such as \frac{}{} which may produce ill-formed TeX when split (e.g. #3650) as well as allowing the colors from TikZ graphics to be preserved as well.)

@christopher-hampson
Copy link
Contributor Author

Currently failing on test test_color_inheritance due to adding color as a named kwarg with default value WHITE.
If it is still desirable that Tex inherits the color from VMobject then the default value could be set to None and then (similar to the original code) if None set to VMobject().color.

I believe this would remove the breaking change.

Copy link
Member

@JasonGrace2282 JasonGrace2282 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice change!
Do you mind adding a short (maybe a sentence or so) note in the doc string of Tex that tells people that you can use \textcolor for coloring substrings?
Otherwise, I think it's all good :)

Copy link
Member

@JasonGrace2282 JasonGrace2282 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay - this seems fine to me!
Left one comment regarding the formatting of the docstring, but it shouldn't be too hard to fix.

@JasonGrace2282 JasonGrace2282 enabled auto-merge (squash) September 1, 2024 15:35
@JasonGrace2282 JasonGrace2282 changed the title Feat: Preserves original TeX colors feat: preserve original TeX colors Sep 1, 2024
@JasonGrace2282 JasonGrace2282 enabled auto-merge (squash) September 1, 2024 15:35
@JasonGrace2282 JasonGrace2282 merged commit 2536b7f into ManimCommunity:main Sep 1, 2024
18 of 19 checks passed
@behackl
Copy link
Member

behackl commented Sep 1, 2024

Just as a reminder for us, in case we ever get around to it: in case we should ever change the implementation of MathTex / Tex to something that is closer to what 3b1b/manim is doing, we'll have to either break this functionality, or be careful about implementing it in a compatible way.

I've recently read about grouping via proper parsing of the DVI file, that might actually be the nicest solution.

@christopher-hampson christopher-hampson deleted the feat_preserve_tex_colors branch September 3, 2024 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants