Skip to content

Coloring Fractions in LaTeX produces invalid LaTeX #3650

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

Closed
krebsnebel4441 opened this issue Mar 20, 2024 · 5 comments
Closed

Coloring Fractions in LaTeX produces invalid LaTeX #3650

krebsnebel4441 opened this issue Mar 20, 2024 · 5 comments

Comments

@krebsnebel4441
Copy link

krebsnebel4441 commented Mar 20, 2024

Hello,
when trying to colorize an equation wrapped in a MathTex I ran into an issue regarding splitting the string at a \frac{}{} resulting in manim to produce invalid latex eventually causing the rendering to fail.

System information

  • OS: Alpine Linux 3.19.1
  • Python: 3.11.8
  • location of manim installation: regular venv created with python -m venv
  • manim: 0.18.0 installed via pip

Steps to reproduce

The problematic codes looks a follows sin = MathTex( r"\frac{GK_{\alpha}}{H}", substrings_to_isolate=[r"GK_{\alpha}"] )
my goal was to visuallize sin a at a rectangular triangle and for visualization to color the strings GK_{\alpha} and H in some specific colors as described in https://docs.manim.community/en/stable/guides/using_text.html#correctlatexsubstringcoloring

unfortunately manim generates flawed latex, specifically this code


\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}

\begin{align*}
\frac{}
\end{align*}

\end{document}

which latex rejects causing the rendering to fail.

Likely cause of the problem

in __init()__ of MathTex in manim/mobject/text/tex_mobject.py at line 270 the given tex string is split up into multiple substrings based on the value of substrings_to_isolate with the function _break_up_tex_strings(). According to my debugger in this case this yields ['\\sin(\\alpha) = \\frac{', 'GK_{\\alpha}', '}{H}'] (I ran the debugger using a little bit different snippet of latex but it es essentially th same. The flawed latex will then look like this: \sin(\alpha) = \frac{}
This value is stored in self.tex_strings which is later used in the function called in line 278 in the same file _break_up_by_substrings()
As far as I understand that function it basically creates one SingleStringMathTex() per substring resulting in the constructor of SingleStringMathTex() to write that flawed latex to a file and to try to compiel it.

(Possible solutions)

First of all. I'm really new to manim, so I really don't know how fix that, but when debugging my way to the error I also saw the function _get_modified_expressions on line 128 being called to prepare the latex for SingleLineMathTex . It itself calls a function named _modify_special_strings() which is as far as I understand that responsible for checking certain special strings , e.g. \sqrt{}. Without knowing much about manim structure I think this could be a place to address this problem.

I hope I could provide a helpful description of my problem. For further questions I'm available.

@krebsnebel4441
Copy link
Author

I was able to solve that problem for me using \over instead of \frac.
This now posses the question, how important this is now, as rather feel like it's more common to use \frac also in the literature.
And speaking as someone who just got started with Manim I can tell how annoying and also somewhat daunting it is to stumble into problems by just
applying the stuff you're somewhat thaugth to apply.
So I personnally would therefore say that it would make Manim more (especially new-) user-friendly to solve that problem.

@uwezi
Copy link
Contributor

uwezi commented Mar 20, 2024

Great that you were able to solve this on your own. Yes, its a well known but perhaps well-hidden fact: the individual parts after splitting need to still be correct LaTeX syntax, which is not the case when you split a \frac{}{}.
Come over and join the community on Discord, where you will find many alternative suggestions on how to better handle your LaTeX expressions: https://docs.manim.community/en/stable/faq/general.html?highlight=discord#where-can-i-find-more-resources-for-learning-manim

@krebsnebel4441
Copy link
Author

Do you think it is worth noting these problems in the documentation, in order to stop beginners to stumble into the same pitfall?

@uwezi
Copy link
Contributor

uwezi commented Mar 21, 2024

It's open source - you are welcome to collaborate!

@behackl
Copy link
Member

behackl commented Apr 20, 2024

Closing essentially as a duplicate of #2884

@behackl behackl closed this as not planned Won't fix, can't repro, duplicate, stale Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New
Development

No branches or pull requests

3 participants