如何灵活设置公式中各个部分的颜色?
<p>在制作数学动画时,我们经常需要突出显示公式中的某一部分。</p><p>比如勾股定理 $ a^2 + b^2 = c^2 $,我们可能想把 $ a^2 $ 标成<strong>黄色</strong>,$ b^2 $ 标成<strong>绿色</strong>,$ c^2 $ 标成<strong>红色</strong>,以便观众可以直观地理解对应关系。</p>
<p>在 <code>ManimCE</code> 中,实现这一目标主要有两种思路:<strong>“Manim 原生拆分法”</strong> 和 <strong>“LaTeX 原生着色法”</strong>。</p>
<p>今天我们就结合代码,聊聊这两种方法怎么用,以及什么时候该用哪一种。</p>
<h1 id="1-方法一manim-原生拆分法">1. 方法一:Manim 原生拆分法</h1>
<p>这是 <code>Manim</code> 中最常用、也是最灵活的方法。它的核心思想是:<strong>在创建公式时,将公式拆分成多个字符串片段,每个片段成为一个独立的对象。</strong></p>
<h2 id="11-代码演示">1.1. 代码演示</h2>
<pre><code class="language-python">from manim import *
class LatexColor(Scene):
def construct(self):
# 1. 将公式拆分成多个字符串参数
# 这样 MathTex 会把它们视为独立的子对象
tex01 = MathTex("a^2", "+", "b^2", "=", "c^2")
# 2. 通过索引访问各个部分并设置颜色
# tex01 对应 "a^2"
# tex01 对应 "b^2"
# tex01 对应 "c^2"
tex01.set_color(YELLOW)
tex01.set_color(GREEN)
tex01.set_color(RED)
self.play(Write(tex01))
self.wait()
</code></pre>
<p><img src="https://img2024.cnblogs.com/blog/83005/202604/83005-20260422102054763-1201276177.png" alt="" loading="lazy"></p>
<h2 id="12-原理解析">1.2. 原理解析</h2>
<p>当你写 <code>MathTex("a^2", "+", "b^2", "=", "c^2")</code> 时,<code>Manim</code> 实际上创建了一个包含 5 个子对象的组(<code>VGroup</code>)。</p>
<ul>
<li><code>tex01</code> 是 $ a^2 $</li>
<li><code>tex01</code> 是 $ + $</li>
<li><code>tex01</code> 是 $ b^2 $</li>
<li>...以此类推。</li>
</ul>
<p>因为它们是独立的对象,你不仅可以改颜色,还可以单独让它们移动、缩放或单独播放写入动画。</p>
<h2 id="13-优缺点">1.3. 优缺点</h2>
<ul>
<li>✅ <strong>优点</strong>:后续动画控制极其方便。你可以让 $ a^2 $ 先出现,再加号出现,再 $ b^2 $ 出现。</li>
<li>❌ <strong>缺点</strong>:如果公式非常复杂,拆分字符串会很麻烦,而且需要数索引(比如第 3 部分到底是哪个符号),容易数错。</li>
</ul>
<h1 id="2-方法二latex-原生着色法">2. 方法二:LaTeX 原生着色法</h1>
<p>如果你熟悉 <code>LaTeX</code>,你可能知道 <code>LaTeX</code> 本身支持颜色命令 <code>\color{}</code>。</p>
<p><code>Manim</code> 也支持这种方式,但需要额外配置一下模板。</p>
<h2 id="21-代码演示">2.1. 代码演示</h2>
<pre><code class="language-python">from manim import *
class LatexColor(Scene):
def construct(self):
# 1. 创建自定义模板
my_template = TexTemplate()
# 2. 添加 xcolor 宏包支持(Manim 默认不全包含所有 LaTeX 包)
my_template.add_to_preamble(r"\usepackage{xcolor}")
# 3. 在公式字符串内部直接使用 LaTeX 颜色命令
tex02 = MathTex(
r"{\color{yellow} a^2} + {\color{green} b^2} = {\color{red} c^2}",
tex_template=my_template,
)
# 为了对比,把它放在第一个公式下方
tex02.next_to(self.mobjects, DOWN)
self.play(Write(tex02))
self.wait()
</code></pre>
<p><img src="https://img2024.cnblogs.com/blog/83005/202604/83005-20260422102054745-1441920564.png" alt="" loading="lazy"></p>
<h2 id="22-原理解析">2.2. 原理解析</h2>
<p>这里我们自定义了一个 <code>TexTemplate</code>,并在导言区(<code>preamble</code>)加入了 <code>\usepackage{xcolor}</code>,这是因为 <code>Manim</code> 默认的 <code>LaTeX</code> 环境为了编译速度,并没有加载所有宏包。</p>
<p>然后在 <code>MathTex</code> 的字符串里,我们直接用 <code>LaTeX</code> 语法 <code>{\color{yellow} 内容}</code> 来指定颜色。</p>
<h2 id="23-优缺点">2.3. 优缺点</h2>
<ul>
<li>✅ <strong>优点</strong>:符合 <code>LaTeX</code> 书写习惯,对于复杂的嵌套公式(比如分数内部着色),这种方式写起来更直观,不需要拆分字符串。</li>
<li>❌ <strong>缺点</strong>:颜色是“画”在公式上的。虽然 <code>Manim</code> 也能识别部分子对象,但相比方法一,它对各个颜色块的控制力较弱。</li>
<li>如果你想让黄色的 $ a^2 $ 单独飞入,可能会遇到麻烦。</li>
</ul>
<h1 id="3-总结我该选哪一种">3. 总结:我该选哪一种?</h1>
<p>作为初学者,面对这两种方法可能会纠结。这里有一个简单的决策指南:</p>
<table>
<thead>
<tr>
<th style="text-align: left">场景</th>
<th style="text-align: left">推荐方法</th>
<th style="text-align: left">理由</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left"><strong>需要分步动画</strong></td>
<td style="text-align: left"><strong>方法一 (拆分法)</strong></td>
<td style="text-align: left">你需要控制每个部分单独出现、移动或高亮,拆分法能让每个部分成为独立对象。</td>
</tr>
<tr>
<td style="text-align: left"><strong>公式复杂且静态</strong></td>
<td style="text-align: left"><strong>方法二 (LaTeX 法)</strong></td>
<td style="text-align: left">比如一个巨大的分式,只想给分子的一部分上色,拆分字符串太痛苦,直接用 LaTeX 命令更快捷。</td>
</tr>
<tr>
<td style="text-align: left"><strong>初学者练习</strong></td>
<td style="text-align: left"><strong>方法一 (拆分法)</strong></td>
<td style="text-align: left">更能帮助你理解 Manim 的“对象化”思维,方便后续调试。</td>
</tr>
</tbody>
</table>
<p><strong>完整参考代码</strong></p>
<p>为了方便大家练习,我将上述两种方法合并在一个完整的场景中,你可以直接复制运行:</p>
<pre><code class="language-python">from manim import *
class LatexColor(Scene):
def construct(self):
# --- 方法一:Manim 拆分设置颜色 ---
tex01 = MathTex("a^2", "+", "b^2", "=", "c^2")
tex01.set_color(YELLOW)
tex01.set_color(GREEN)
tex01.set_color(RED)
self.play(Write(tex01))
# --- 方法二:LaTeX 原生颜色设置 ---
my_template = TexTemplate()
my_template.add_to_preamble(r"\usepackage{xcolor}")
tex02 = MathTex(
r"{\color{yellow} a^2} + {\color{green} b^2} = {\color{red} c^2}",
tex_template=my_template,
).next_to(tex01, DOWN)
self.play(Write(tex02))
self.wait()
</code></pre>
<p>希望这篇教程能帮你更好地给公式“穿上彩衣”!</p>
<p>如果在实践中遇到索引对不上或者颜色不显示的问题,欢迎随时交流。</p><br><br>
来源:https://www.cnblogs.com/wang_yb/p/19906147
頁:
[1]