out01.gif

文本的样式主要指颜色和字体相关的属性设置。对于manim的两个文本对象 Text和 Tex来说, Text对象有更多的属性可以调整样式,相对来说,由于 Tex主要用来显示数学公式,所以关于样式的属性要少一些。下面介绍一些我在视频制作时最常用的一些颜色和字体相关的属性。

1. 颜色相关

颜色设置主要分为单色,渐变色两种,对于 Text对象,manim还提供了更加精细的按字符设置的属性。

1.1 单色

单色设置通过 color属性。

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 R027_00(Scene):
def construct(self):
sc = self
self.play(*[FadeIn(_o) for _o in [Text('hello world', color=RED)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text('你好,世界', color=YELLOW)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text('こんにちは世界', color=GREEN)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [MathTex('a^2 + b^2 = c^2', color=RED)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [MathTex('E = mc^2', color=YELLOW)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [MathTex('e^{i\\pi} + 1 = 0', color=GREEN)]], run_time=1.0)
self.wait(0.5)

运行效果:

下载 Python 脚本

1.2 渐变色

渐变色设置通过 set_color_by_gradient方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# -*- coding: utf-8 -*-
# 示例 1.2:Text / MathTex 的渐变色 set_color_by_gradient(对应视频 20240830211647001)
# 本地复现:manim -qh 20240830211647001.py GradientColorDemo
# 知识点:set_color_by_gradient 是 Mobject 的方法,Text 与 MathTex 都能用;
# 它把两种颜色按「子对象」从左到右插值分配,字符越多过渡越细腻。
# 本篇主题就是字体:含中文/日文的 Text 必须显式给覆盖该字符集的字体,否则是白色方块。
from manim import *
# 华文隶书覆盖简体中文;微软雅黑同时覆盖日文假名,专用于「こんにちは世界」那一行。
CN = "STLiti"
CJK = "Microsoft YaHei"
class GradientColorDemo(Scene):
def construct(self):
title = Text("1.2 渐变色 set_color_by_gradient", font=CN, font_size=40)
title.to_edge(UP, buff=0.4)
self.play(FadeIn(title, shift=DOWN * 0.15), run_time=0.6)
# 原文写的是 Tex(r""),公式里再套 $ 会编译异常;数学公式用 MathTex 传裸 LaTeX。
# 顺序与原文六行一一对应:三行文字、三行公式。
spec = [(Text("hello world", font=CN, font_size=38), (RED, GREEN)),
(Text("你好,世界", font=CN, font_size=38), (YELLOW, BLUE)),
(Text("こんにちは世界", font=CJK, font_size=38), (BLUE, RED)),
(MathTex(r"a^2 + b^2 = c^2", font_size=44), (RED, GREEN)),
(MathTex(r"E = mc^2", font_size=44), (YELLOW, BLUE)),
(MathTex(r"e^{i\pi} + 1 = 0", font_size=44), (BLUE, RED))]
items = [obj.set_color_by_gradient(pair) for obj, pair in spec]
group = VGroup(*items).arrange(DOWN, buff=0.26, aligned_edge=LEFT)
group.next_to(title, DOWN, buff=0.4)
# 逐行上屏:能看清每一行的渐变方向,也方便对照代码块里的顺序。
for m in items:
self.play(FadeIn(m, shift=RIGHT * 0.2), run_time=0.55)
# 全屏完成态停留,供封面取帧(第 5 秒)。
self.wait(2.2)

运行效果:

下载 Python 脚本

1.3 按字符设置

如果一段文本中需要突出某些字符,可以用 t2c属性来设置特定字符的颜色。

1
2
from manim import *
Text("databook.top", t2c={"data": BLUE, "book": RED, "top": GREEN})

对于 Tex对象来说,虽然没有 t2c属性,也可以通过 set_color_by_tex方法来设置特定字符的颜色。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from manim import *
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
class Demo(Scene):
def construct(self):
t = Tex(r"", " + ", r"", " = ", r"")
t.set_color_by_tex("a^2", RED)
t.set_color_by_tex("b^2", YELLOW)
t.set_color_by_tex("c^2", GREEN)
_obj = t
self.play(FadeIn(_obj), run_time=1.0)
self.wait(3.00)
self.wait(1.60)

运行效果:

下载 Python 脚本

2. 字体相关

manim可以直接使用系统中已有的字体,也可以设置字号,Text对象还可以设置粗体,斜体等等。

2.1 系统字体

manim通过 font属性设置字体。

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 R027_03(Scene):
def construct(self):
sc = self
self.play(*[FadeIn(_o) for _o in [Text('manim', font='Consolas')]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text('数学', font='STXingkai')]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text('之旅', font='STCaiyun')]], run_time=1.0)
self.wait(0.5)
self.wait(2.10)

运行效果:

下载 Python 脚本

2.2 字号

字号就是字体的大小,通过 font_size属性设置。

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 R027_04(Scene):
def construct(self):
sc = self
self.play(*[FadeIn(_o) for _o in [Text('manim', font_size=20)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text('数', font_size=30)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text('学', font_size=40)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text('之', font_size=50)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text('旅', font_size=60)]], run_time=1.0)
self.wait(0.5)
self.wait(0.40)

运行效果:

下载 Python 脚本

2.3 粗体

粗体通过 weight属性设置。

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 R027_05(Scene):
def construct(self):
sc = self
self.play(*[FadeIn(_o) for _o in [Text('NORMAL font weight')]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text('BOLD font weight', weight=BOLD)]], run_time=1.0)
self.wait(0.5)
self.wait(3.10)

运行效果:

下载 Python 脚本

2.4 斜体

斜体通过 slant属性设置。

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 R027_06(Scene):
def construct(self):
sc = self
self.play(*[FadeIn(_o) for _o in [Text('NORMAL font')]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text('ITALIC font', slant=ITALIC)]], run_time=1.0)
self.wait(0.5)
self.wait(3.10)

运行效果:

下载 Python 脚本

2.5 按字符设置

字体相关的属性也可以像颜色那样,按字符设置。相关的主要属性包括:

  1. t2f:按字符设置字体
  2. t2s:按字符设置斜体
  3. t2w:按字符设置粗体
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 R027_07(Scene):
def construct(self):
sc = self
self.play(*[FadeIn(_o) for _o in [Text('manim 数学之旅', t2f={'数学': 'STXingkai', '之旅': 'STCaiyun'}, t2s={'ma': ITALIC}, t2w={'nim': BOLD})]], run_time=1.0)
self.wait(0.5)
self.wait(4.10)

运行效果:

下载 Python 脚本

3. 总结回顾

本篇主要介绍了文本的颜色和字体相关的设置,其中 Text提供了丰富的属性来设置文本的样式。 颜色相关的属性主要有:

  1. color:单色设置
  2. set_color_by_gradient:渐变色设置
  3. t2c:按字符设置颜色

字体相关的属性主要有:

  1. font:字体设置
  2. font_size:字号设置
  3. weight:粗体设置
  4. slant:斜体设置
  5. t2f:按字符设置字体
  6. t2s:按字符设置斜体
  7. t2w:按字符设置字体

文字说明和公式是制作数学视频时必不可少的部分,通过设置其常用属性,让文字和公式在配合其他图形部分时,在色彩,大小等形式上保持一致性,更加美观的呈现最终的效果。