manim中提供了两种方式来表示文字信息,一种是 Text系列,一种是 Tex系列。
从目前我自己的使用的体验来看,
Text系列在显示文字信息方面,提供了更多的的属性来调整显示效果。如果有大段的文字要排版,或者对文字的显示效果要求高的话,建议使用 Text。

而 Tex系列最大的优势是支持 Latex,也就是说它能更好的显示数学公式。
Tex目前还不支持显示中文,如果一段文本中既有中文又有数学公式的话,需要结合 Text和 Tex一起来显示。

下面介绍 Text和 Tex中几种常用的显示文字和公式的方法。

1. Text

Text系列常用的对象主要有三种:

  1. Text:一般用于单行的文本
  2. Paragraph:一般用于多行的文本
  3. MarkupText:一般用于富文本显示

1.1 一般文本

一般文本 Text支持显示任何语言:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
return Dot(pos, **kwargs)
class Z0023(Scene):
def construct(self):
sc = self
self.play(*[FadeIn(_o) for _o in [Text('hello world')]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text('你好,世界')]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text('こんにちは世界')]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text('Привет, мир')]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text('مرحبا بالعالم', font='sans-serif')]], run_time=1.0)
self.wait(0.5)
self.wait(0.40)

下载 Python 脚本

运行效果:

1.2 段落文本

段落文本 Paragraph可以多行文本进行简单的排版:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
return Dot(pos, **kwargs)
class Z0024(Scene):
def construct(self):
sc = self
paragraph = Paragraph(' 春晓\n', '春眠不觉晓', '处处闻啼鸟', '夜来风雨声', '花落知多少')
self.play(FadeIn(paragraph), run_time=1.0)
self.wait(1)
self.wait(0.5)
self.wait(3.10)

下载 Python 脚本

运行效果:

1.3 带标记文本

带标记的文本MarkupText类似HTML,可以像 word 那样提供一些标记文本的功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
return Dot(pos, **kwargs)
class Z0025(Scene):
def construct(self):
sc = self
self.play(*[FadeIn(_o) for _o in [MarkupText('<b>bold</b> <i>斜体文字</i>')]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [MarkupText('<u>下划线</u> <s>中划线</s>')]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [MarkupText('<span underline="double">双下划线</span> <span underline="error">错误标记</span>')]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [MarkupText('下标和上标:H<sub>2</sub>O H<sub>3</sub>O<sup>+</sup>')]], run_time=1.0)
self.wait(0.5)
self.wait(1.10)

下载 Python 脚本

运行效果:

2. Tex

Tex系列的对象主要是用来显示数学公式,基于Tex之上,manim还封装了列表和标题两种常用的对象。
不过,Tex默认只支持显示英文,所以多用于显示公式,显示文本还是 Text系列的对象比较常用。

2.1 数学公式

Tex可以显示任何 Latex格式的数学公式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
return Dot(pos, **kwargs)
class Z0026(Scene):
def construct(self):
sc = self
self.play(*[FadeIn(_o) for _o in [MathTex('a^2 + b^2 = c^2')]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [MathTex('E = mc^2')]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [MathTex('e^{i\\pi} + 1 = 0')]], run_time=1.0)
self.wait(0.5)
self.wait(2.10)

下载 Python 脚本

运行效果:

2.2 列表文本

BulletedList会将传入的多个字符串以列表的方式排列起来:

1
2
3
4
5
6
7
8
9
10
11
12
13
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
return Dot(pos, **kwargs)
class Z0027(Scene):
def construct(self):
sc = self
self.play(*[FadeIn(_o) for _o in [BulletedList('', '', 'e^{i\\pi} + 1 = 0')]], run_time=1.0)
self.wait(0.5)
self.wait(4.10)

下载 Python 脚本

运行效果:

2.3 标题

Title自动显示在顶部,它和 BulletedList一样,都是为了减轻排版的负担。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
return Dot(pos, **kwargs)
class Z0028(Scene):
def construct(self):
sc = self
import manim
self.play(*[FadeIn(_o) for _o in [Title(f'Manim version {manim.__version__}')]], run_time=1.0)
self.wait(0.5)
self.wait(4.10)

下载 Python 脚本

运行效果:

3. 总结回顾

Text和 Tex都是用来显示文本的,
在使用时,如果我们要显示很多说明文字,且包括英文以外的语言文字,那么,选择Text来显示。
如果显示数学公式,那么毫无疑问,只能选择 Tex。

Tex系列的对象只能显示英文,对于我们非英语国家来说,
我平时其实基本不怎么使用 BulletedList 和 Title的。

为了显示中文,可以基于 Text封装自己的 列表和标题 对象。