Manim常见问题解决方案

ManimCE

中文渲染以及自定义字体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from manim import Scene, Tex, TexTemplateLibrary, DOWN, UP


class TestText(Scene):
def construct(self):
tex_template = TexTemplateLibrary.ctex
tex_template.add_to_preamble(r"""
\setCJKmainfont{Noto Sans SC}[BoldFont={Noto Sans SC}, AutoFakeSlant]
\setCJKsansfont{Noto Sans SC}[BoldFont={Noto Sans SC}]
""")

text1 = Tex(r'要使得 $\overline{\varepsilon^2}$ 最小,则对b的选择应满足:', tex_template=tex_template)
text2 = Tex(
r"""
\frac{\partial}{\partial b}[E(a_j-b)^2] &= \frac{\partial}{\partial b}[E(a_j^2-2a_jb+b^2)] \nonumber
\\
&= -2[E(a_j)-b] \nonumber
\\
&=0 \nonumber""",
tex_environment='align',
tex_template=tex_template,
)
self.add(text1.move_to(UP * 3))
self.add(text2.next_to(text1, DOWN * 2))

实际上就是ctex中自定义字体的方式,修改前后效果如下图所示:

字体名称可以通过以下指令获得:

1
fc-list :lang=zh

Manim常见问题解决方案
https://www.aye10032.com/2025/06/03/2025-06-03-manim/
作者
Aye10032
发布于
2025年6月3日
许可协议