Manim如何在数学公式中完美显示中文?
<p>很多刚开始学习 <code>ManimCE</code> 的同学,在兴致勃勃地想要制作中文数学动画时,都会遇到同一个“拦路虎”:<strong>一旦在公式里输入中文,程序直接报错,红一片!</strong></p><p>比如你想写“勾股定理”,直接这样写:</p>
<pre><code class="language-python"># 错误示范 ❌
tex = MathTex("勾股定理:a^2+b^2=c^2")
</code></pre>
<p>运行后,你会得到一堆 <code>LaTeX</code> 编译错误。这是因为默认的 <code>LaTeX</code> 模板是不支持中文字符的。</p>
<p>别担心,今天我们就来彻底解决这个问题,教会大家如何在 <code>MathTex</code>、<code>Tex</code> 以及 <code>Title</code> 组件中优雅地显示中文。</p>
<h1 id="1-为什么会出现报错">1. 为什么会出现报错?</h1>
<p><code>Manim</code> 底层使用 <code>LaTeX</code> 来渲染公式。</p>
<p>标准的 <code>LaTeX</code> 环境主要针对英文和数学符号设计,如果没有加载特定的中文宏包(如 <code>ctex</code>),它就无法识别汉字,从而导致编译失败。</p>
<p>所以,核心思路就是:<strong>告诉 Manim 使用支持中文的 LaTeX 模板。</strong></p>
<h1 id="2-三大组件的中文显示方案">2. 三大组件的中文显示方案</h1>
<p>在 <code>ManimCE</code> 中,常用的可以显示数学公式的三个组件是 <code>MathTex</code>、<code>Tex</code> 和 <code>Title</code>。</p>
<p>它们显示中文的设置略有不同,我们逐个来看。</p>
<h2 id="21-mathtex-组件">2.1. MathTex 组件</h2>
<p><code>MathTex</code> 主要用于数学公式。要显示中文,只需要添加一个关键参数:<code>tex_template</code>。</p>
<ul>
<li><strong>解决方法</strong>:设置 <code>tex_template=TexTemplateLibrary.ctex</code></li>
<li><strong>代码示例</strong>:</li>
</ul>
<pre><code class="language-python"># 正确示范 ✅
tex02 = MathTex(
r"\text{勾股定理}: a^2+b^2=c^2",
tex_template=TexTemplateLibrary.ctex,# 关键!
)
</code></pre>
<p><em>注意:中文最好包裹在 <em><code>\text{}</code></em> 中,这样 LaTeX 才知道这是文本而不是数学变量。</em></p>
<h2 id="22-tex-组件">2.2. Tex 组件</h2>
<p><code>Tex</code> 组件通常用于包含更多文本内容的段落。对于中文显示,它比 <code>MathTex</code> 多了一个要求。</p>
<ul>
<li><strong>解决方法</strong>:需要同时设置 <code>tex_template</code> 和 <code>tex_environment</code>。</li>
<li><strong>关键参数</strong>:
<ol>
<li><code>tex_template=TexTemplateLibrary.ctex</code></li>
<li><code>tex_environment="align*"</code></li>
</ol>
</li>
<li><strong>代码示例</strong>:</li>
</ul>
<pre><code class="language-python"># 正确示范 ✅
tex01 = Tex(
r"\text{勾股定理}: a^2+b^2=c^2",
tex_environment="align*", # 关键!
tex_template=TexTemplateLibrary.ctex,# 关键!
)
</code></pre>
<h2 id="23-title-组件">2.3. Title 组件</h2>
<p><code>Title</code> 组件用于生成标题。很多同学不知道,其实 <code>Title</code> 底层是继承自 <code>Tex</code> 组件的。</p>
<ul>
<li><strong>解决方法</strong>:既然它继承自 <code>Tex</code>,那么它显示中文的要求也与 <code>Tex</code> 完全一致。</li>
<li><strong>关键参数</strong>:同样需要 <code>tex_environment="align*"</code> 和 <code>tex_template=TexTemplateLibrary.ctex</code>。</li>
<li><strong>代码示例</strong>:</li>
</ul>
<pre><code class="language-python"># 正确示范 ✅
title = Title(
r"\text{勾股定理}: a^2+b^2=c^2",
tex_environment="align*", # 关键!
tex_template=TexTemplateLibrary.ctex,# 关键!
)
</code></pre>
<h1 id="3-完整代码演示">3. 完整代码演示</h1>
<p>为了让大家能够直接运行成功,我整理了一份完整的代码示例。你可以直接复制到你的编辑器中尝试。</p>
<pre><code class="language-python">from manim import *
class Keshihua(Scene):
def construct(self):
# 1. Tex 组件显示中文
# 需要两个参数:tex_environment 和 tex_template
tex01 = Tex(
r"\text{勾股定理}: a^2+b^2=c^2",
tex_environment="align*",
tex_template=TexTemplateLibrary.ctex,
).set_color_by_gradient(RED, GREEN)
self.play(Write(tex01))
# 2. MathTex 组件显示中文
# 只需要一个参数:tex_template
tex02 = MathTex(
r"\text{勾股定理}: a^2+b^2=c^2",
tex_template=TexTemplateLibrary.ctex,
).set_color_by_gradient(YELLOW, BLUE)
tex02.next_to(tex01, DOWN)
self.play(Write(tex02))
# 3. Title 组件显示中文
# 因为继承自 Tex,所以参数要求与 Tex 相同
title = Title(
r"\text{勾股定理}: a^2+b^2=c^2",
tex_environment="align*",
tex_template=TexTemplateLibrary.ctex,
)
self.play(Write(title))
self.wait()
</code></pre>
<p><img src="https://img2024.cnblogs.com/blog/83005/202604/83005-20260419131547489-209308904.png" alt="" loading="lazy"></p>
<h1 id="4-总结与建议">4. 总结与建议</h1>
<p>为了方便记忆,我把今天的重点整理成了一个小表格:</p>
<table>
<thead>
<tr>
<th style="text-align: left">组件类型</th>
<th style="text-align: center">是否需要中文模板?</th>
<th style="text-align: center">是否需要指定环境?</th>
<th style="text-align: left">关键参数设置</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left"><strong>MathTex</strong></td>
<td style="text-align: center">✅ 是</td>
<td style="text-align: center">❌ 否</td>
<td style="text-align: left"><code>tex_template=TexTemplateLibrary.ctex</code></td>
</tr>
<tr>
<td style="text-align: left"><strong>Tex</strong></td>
<td style="text-align: center">✅ 是</td>
<td style="text-align: center">✅ 是</td>
<td style="text-align: left"><code>tex_template=...</code>, <code>tex_environment="align*"</code></td>
</tr>
<tr>
<td style="text-align: left"><strong>Title</strong></td>
<td style="text-align: center">✅ 是</td>
<td style="text-align: center">✅ 是</td>
<td style="text-align: left">同 <code>Tex</code> 组件</td>
</tr>
</tbody>
</table>
<p><strong>小贴士:</strong></p>
<ol>
<li><strong>环境配置</strong>:确保你的电脑已经安装了 <code>LaTeX</code> 环境(如 <code>TeX Live</code> 或 <code>MiKTeX</code>),并且安装了 <code>ctex</code> 宏包。<code>Manim</code> 调用的是系统本地的 <code>LaTeX</code> 编译器。</li>
<li><strong>转义字符</strong>:在 <code>Python</code> 字符串中,记得在公式前加 <code>r</code>(如 <code>r"\text{...}"</code>),防止反斜杠被转义。</li>
<li><strong>文本包裹</strong>:尽量用 <code>\text{}</code> 包裹中文,这样排版会更规范。</li>
</ol>
<p>希望这篇教程能帮你扫清障碍,做出更漂亮的中文数学动画!</p><br><br>
来源:https://www.cnblogs.com/wang_yb/p/19890727
頁:
[1]