3b1b / manim

Animation engine for explanatory math videos
MIT License
68.37k stars 6.09k forks source link

\sqrt issue #1225

Open johnbear02 opened 4 years ago

johnbear02 commented 4 years ago

Hello I was trying to do an animation using square roots, but when i write the code like this: TexMobject(r"\sqrt{",r"\left(",r"5",r"-",r"{9",r"\over",r"2}",r"\right)",r"^2}",r"=",r"\sqrt{",r"\left(",r"4",r"-",r"{9",r"\over",r"2}",r"\right)",r"^2\tiny.}") And then Writing (or adding) on the scene, it shows the whole equation, except the last character (the "2" exponent).

I watch a video that use a "\tiny ." to solve this, but it doesn't work to me.

I'm using Windows 10, the last version of Manim.

Problema

NeoPlato commented 4 years ago

Don't take this as a full solution! I will update once I get to the bottom of this

The problem is obvious. Somehow Manim is giving us a slap in the face by eliminating the last one or two strings in the tex.

Quick hacky answer: go to _manimlib\mobject\svg\texmobject and comment out this rat

self.break_up_by_substrings()

Should be right under this one:

SingleStringTexMobject.__init__(
            self, self.arg_separator.join(tex_strings), **kwargs
        )

Tell me how that goes. I'll be back with a permanent solution soon

johnbear02 commented 4 years ago

Update: I realized that putting two characters after the sqrt solve the problem, but in a strange form. And later when i write \left | and \right| I have the same problem but i had to write four characters after the big ||, but the ReplacementTransform acts weird, when I do that.

johnbear02 commented 4 years ago

Here's my _manimlib\mobject\svg\texmobject image image

NeoPlato commented 4 years ago

Yeah I'm not entirely sure what that code does but here's a guestimate from me

Assuming you know how Python works (if not Manim will screw you over big time) class objects in themselves are referred to by self, so when you initialize it as a SingleStringTexMobject the item is created and then calling that method modifies self(the TexMobject) itself. Once I know what the thing is doing to the last two items in the list I'll be good to share a solution.

Just comment out the line you underlined. I tested it and it's yay okay

Which means there's a cheeky bug waiting to be discovered

johnbear02 commented 4 years ago

Yea, i know python, but i'm not that good. I know how to read it, but not in a deep way

NeoPlato commented 4 years ago

Same actually.

Look at me joyfully riding the high horse 💀

All you got to do is learn something new every day. Honestly Python is more extensive than Manim and if I had decided to do Python for Python's sake and not just to use this library I probably would've never tried Manim out.

NeoPlato commented 4 years ago

Update on my solution.

I haven't really found anything yet, admittedly due to minimal reflection on the issue but I have stumbled upon something.

That method somehow has to stay because it makes the tex_to_color_map work so if you wanted to use that that may be a problem.

Again, if I come up with anything is a fingers crossed protocol. Hopefully someone else beats me to the chase.

StruggleTownMayor commented 4 years ago

What worked for me was leaving

r"\sqrt"

As one part without the {.

Then attach the curly bracket to the first thing inside the square root.

Still need to test interactions with r"\left" and r"\right" though!

Jiricek72 commented 3 years ago

Hello,

it seems, that manim is expecting at least one character after the { of \sqrt{. So I tried to add a space in Tex. This can be one of these codes:

\, \: ~

The space is barely visible in the output but it worked for me.

from manim import *
config.background_color = WHITE

class Formel(Scene):
    def construct(self):

    u_formel_1b = MathTex(
            "\\hat{u} = \\sqrt{\,",
            "\\hat{u}_{R}^{2}",
            " + ",
            "\\hat{u}_{C}^{2}}}",
            color= BLACK
        )
    u_formel_1b[1].set_color(GREEN)

    self.play(Write(u_formel_1b))
    self.wait(5)
Koppeprojects commented 1 year ago

Hello Jiricek72, the problem or at least a similar problem still exists. In my case rechnung=MathTex(r"D",r"=\sqrt{18}",r"=",r"\sqrt{",r"9 \cdot 2}",r"=...=",r"\sqrt{",r"3^2}",r" \cdot \sqrt{",r"2}",r"=",r"3\sqrt{2}",r"=",r"D").next_to(text3,DOWN).shift(3.2*RIGHT) the variable when printing for i , _ in enumerate(rechnung): print(i,rechnung[i]) had still 13 substrings and all at the right index, but when I did self.play(Write(rechnung[13]), Write(rechnung[12]), .... Write(rechnung[1]), Write(rechnung[0]), run_time=2) then rechnung[13] strangely corresponded in the animation to the thing at index 12 (equal sign). Nicely rechnung[0] corresponded in the animation to the thing at index 0 (D). So there should be one index in the middle that correspond to nothing. As it turned out the little horizontal line above the number in the sqaure root corresponded to the additional index.

I had more than one such square root but the strange index shift occured only once (at least probably not investigated this).

Koppeprojects commented 1 year ago

I want to add that the solution of you Jiricek72 worked for me, till now.