簡體   English   中英

為復雜方程 manim 的部分着色

[英]Colorize parts of a complex equation manim

我想在不同的 colors 中繪制 MathTex 元素的變量,但 Manim 似乎對復雜的 Latex 表達式有問題。

這是我的場景。

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)

這是結果。

在此處輸入圖像描述

具體來說,我想在 YELLOW 中為d_{i,i+1}着色,在YELLOW中為v_{i,i+1} GREEN RED startend

有什么建議嗎? 坦率地說,我不想在不同的 colors 中創建多個 MathTex object 然后排列它們。

遲到的答案,但我遇到了類似的問題,在找到文檔中的相關部分之前就到了這里。

文檔中的相關部分: Using index_labels to work with complicated strings

您的特殊情況的示例:

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 在幕后做了一堆 tex 重寫,似乎由於重寫, overfrac受歡迎。

我能夠應用您想要的顏色(盡管我懷疑您不希望總和符號着色):

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)

但我不得不求助於單獨的對象。 更糟糕的是,我用手將它們與軟糖因素對齊。

結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM