Manim 社区:通过文本为方程式着色

问题描述 投票:0回答:1

我有以下 Python 代码来在 Manim 社区中显示矩阵:

from manim import *

class Example(Scene):
    def construct(self):
        A = MathTex(
            r"A="
            r"\begin{pmatrix}"
            r"a_{11} & a_{12} & \dots  & a_{1n}\\"
            r"a_{21} & a_{22} & \dots  & a_{2n}\\"
            r"\vdots & \vdots & \ddots & \vdots\\"
            r"a_{n1} & a_{n2} & \dots  & a_{nn}"
            r"\end{pmatrix}"
        ).center()

        self.play(Write(A))

我想给每个“a”相同的颜色(例如红色)。

我知道我可以显示索引,然后按索引单独为每个“a”着色,但是对于更大的方程,这会变得越来越潮。

有没有一种方法可以更智能地完成这些事情,例如通过文本添加颜色,或者通过迭代方程?

python manim
1个回答
0
投票

set_color_by_tex
中存在
MathTex
,但您需要仔细阅读子字符串和部分文档,其中有说明

注意 set_color_by_tex() 为包含 Tex 的整个子字符串着色,而不仅仅是特定符号或 Tex 表达式

因此,当应用于

MathTex
时,整个方程都是彩色的。由于这不是您想要的,您可以使用
substrings_to_isolate
参数来
MathTex

(几乎)工作示例如下所示

class Example(Scene):
    def construct(self):
        A = MathTex(
            r"A=a_{11} & a_{12} & \dots  & a_{1n}\\a_{21} & a_{22} & \dots  & a_{2n}\\\vdots & \vdots & \ddots & \vdots\\a_{n1} & a_{n2} & \dots  & a_{nn}",
            substrings_to_isolate="a"
        ).center()
        
        A.set_color_by_tex("a", RED)

        self.play(Write(A))

如您所见,我删除了

\begin{pmatrix}
\end{pmatrix}
以使其正常工作。 也许你可以在他们的 Github 上跟进,看看在矩阵 MathTex 上应用
substrings_to_isolate
是否可行

© www.soinside.com 2019 - 2024. All rights reserved.