简体   繁体   English

为复杂方程 manim 的部分着色

[英]Colorize parts of a complex equation manim

I want to paint variables of MathTex element in different colors, but Manim seems to have problems with comlicated Latex expressions.我想在不同的 colors 中绘制 MathTex 元素的变量,但 Manim 似乎对复杂的 Latex 表达式有问题。

Here is my scene.这是我的场景。

from manim import *
config.frame_width = 260 

class Find_Path(Scene):
    def construct(self):
        obj = MathTex(r"minimize \quad \sum_{start}^{end}\frac{d_{i,i+1}}{v_{i,i+1}}", 
        font_size=1000, substrings_to_isolate="d" and "v")
        obj.set_color_by_tex("d", YELLOW)
        obj.set_color_by_tex("start", GREEN)
        obj.set_color_by_tex("end", GREEN)
        obj.set_color_by_tex("v", RED)
        self.play(Write(obj))
        self.wait(3)

Here is the result.这是结果。

在此处输入图像描述

Specifically, I want to color d_{i,i+1} in YELLOW , v_{i,i+1} in RED , start and end in GREEN .具体来说,我想在 YELLOW 中为d_{i,i+1}着色,在YELLOW中为v_{i,i+1} GREEN RED startend

Any advice?有什么建议吗? Frankly, I do not want to create several MathTex object in different colors and then arrange them.坦率地说,我不想在不同的 colors 中创建多个 MathTex object 然后排列它们。

Late answer, but I encountered a similar issue and ended up here before finding the relevant section in the documentation.迟到的答案,但我遇到了类似的问题,在找到文档中的相关部分之前就到了这里。

Relevant section in documentation: Using index_labels to work with complicated strings文档中的相关部分: Using index_labels to work with complicated strings

An example with your special case:您的特殊情况的示例:

from manim import *

config.frame_width = 8
config.frame_size = (1300, 1000)


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

        # You can split the string in parts
        minimize = r"minimize \quad "
        summ = r"\sum_{start}^{end}"
        frac = r"\frac{d_{i,i+1}}{v_{i,i+1}}"
        tex = MathTex(minimize, summ, frac).shift(2 * UP)

        # Observe first level labels
        tex_ = tex.copy().next_to(tex, DOWN)
        self.add(index_labels(tex_, color=YELLOW))

        # Observe second level labels
        tex__ = tex_.copy().next_to(tex_, DOWN)
        for part in tex__:
            self.add(index_labels(part, color=YELLOW))

        # Finally you can color accordingly
        tex[1][0:3].set_fill(color=GREEN)
        tex[1][4:9].set_fill(color=GREEN)
        tex[2][0:6].set_fill(color=YELLOW)
        tex[2][7:13].set_fill(color=RED)

        self.add(tex, tex_, tex__)

着色文本

Manim does a bunch of tex rewriting under the covers, and it seems that over is preferred to frac because of that rewriting. Manim 在幕后做了一堆 tex 重写,似乎由于重写, overfrac受欢迎。

I was able to apply the colors that you wanted (although I suspect you didn't want the sum symbol colored) with:我能够应用您想要的颜色(尽管我怀疑您不希望总和符号着色):

from manim import *

class Find_Path(Scene):
    def construct(self):
        obj1 = MathTex(r"\text{minimize}", r"\quad \sum_{\text{start}}^{\text{end}}")
        obj2 = MathTex(r"d_{i,i+1}", r"\over", r"v_{i,i+1}")
        obj1.set_color_by_tex("start", GREEN)
        obj1.set_color_by_tex("end", GREEN)
        obj2.move_to(obj1, RIGHT)
        obj2.shift(1.5 * RIGHT)
        obj2[0].set_color(YELLOW)
        obj2[2].set_color(RED)
        self.play(AnimationGroup(Write(obj1), Write(obj2)))
        self.wait(3)

but I had to resort to separate objects.但我不得不求助于单独的对象。 Worse still, I aligned them by hand with a fudge factor.更糟糕的是,我用手将它们与软糖因素对齐。

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM