Manim 文本
MarkupText
Text

参数 |
说明 |
text |
str对象,需要写入的字符串 |
color |
str对象,文字的颜色,默认为WHITE |
font |
文字的字体 |
font_size |
文字的大小,默认为48 |
size |
文字的缩放比例,默认为1,表示原来的尺寸 |
tab_width |
制表符tab的宽度,默认为4,表示4个空格的宽度 |
slant |
设置斜体,默认为NORMAL。设为ITATIC表示文字设为斜体 |
weight |
设置加粗,默认为NORMAL。设为BOLD表示文字加粗 |
gradient |
可迭代对象,且元素为颜色,表示设置的渐变色的各个节点 |
t2c |
字典Dict,子串到颜色的映射表 |
t2g |
字典Dict,子串到渐变色节点迭代器的映射表 |
Text类只接受一个字符串,不支持数学公式,不支持’\t’,换行可以用’\n’
属性
在构造函数上通过键值对的形式传递参数,修改属性
颜色相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| text = Text('Hello', color=BLUE)
text = Text('Hello, world!', t2c={'world':BLUE})
text = Text('Hello', gradient=(BLUE, GREEN,RED))
text = Text( 'Google', t2c={ '[:1]':'#3174f0', '[1:2]':'#e53125', '[2:3]':'#fbb003', '[3:4]':'#3174f0', '[4:5]':'#269a43', '[5:]':'#e53125', } )
text = Text( 'empire march!', fill_color=RED, fill_opacity=0.5, stroke_color=BLACK, stroke_width=2, )
|
字体相关
1 2
| text = Text('Hello', font='Source Han Sans') text = Text('Hello, world!', t2f={'world':'Forte'})
|
倾斜相关
1 2
| text = Text('Hello', slant=ITALIC) text = Text('Hello, world!', t2s={'world':ITALIC})
|
粗细相关
1 2
| text = Text('Hello', weight=BOLD) text = Text('Hello, world!', t2w={'world':BOLD})
|
大小相关
1
| text = Text('Hello', size=5)
|
行间距
line_spacing_height,lsh
1
| text = Text('Hello\nWorld', lsh=1.5)
|
函数
Text类对象实例化之后,作用于对象的成员函数
1 2 3 4 5 6 7 8 9 10
| self.play(FadeIn(text)) self.wait() text.set_color(RED) self.wait() text[1:6].set_color(BLUE) self.wait() text.set_color_by_gradient(RED,BLACK) self.wait() text[4:8].set_color_by_gradient(WHITE,BLUE) self.wait()
|
SingleStringTex
manim中Tex的基类,主要用于渲染单行公式或者数学符号.
也可以使用换行符//渲染多行公式
属性 |
说明 |
tex_string |
str对象,表示输入的tex代码 |
fill_opacity |
float,填充的透明度 |
font_size |
字体大小,默认为48 |
alignment |
str对象,对齐方式,默认为\center |
1 2 3 4 5
| class Main(Scene): def construct(self): tex=SingleStringTex(r"f(x)=a_0+a_1x+a_2x^2+...+a_nx^n=\sum_{i=0}^n a_i x^i") tex.set_color(BLUE) self.play(Write(tex))
|
效果

文字转换为公式的效果
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| from turtle import left from manimlib import *
class Main(Scene): def construct(self): text=Text("Definition of the ellipse") tex2 = SingleStringTex( tex_string=r"\frac{x^2}{a^2}+\frac{y^2}{b^2}=1", font_size=80 ) self.add(text) self.wait() self.play(ReplacementTransform(text, tex2), run_time=1) self.wait()
|

公式上色
使用字典映射
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| from turtle import left from manimlib import *
class Main(Scene): def construct(self): color_dict={ "x":RED, "f":PINK, "a":GREEN } tex=Tex( r"f(x)=a_0+a_1x+a_2x^2+...+a_nx^n=\sum_{i=0}^n a_i x^i", font_size=60, tex_to_color_map=color_dict ) self.add(tex) self.play(Write(tex))
|
效果:

Tex
Tex是SingleStringTex的派生类,可以接收任意多个tex代码作为参数.
如果想要使用多行公式,不如使用换行符//
,在typora中编辑好了之后放到SingleStringTex中
TexText
Tex的派生类
1 2 3 4 5
| class TexText(Tex): CONFIG = { "math_mode": False, "arg_separator": "", }
|
math_mode默认是false,此时的用法和typora中很像
BulletedList
项目列表,TexText的子类
用于产生条目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| from turtle import left, position from manimlib import *
class demo(Scene): def construct(self): text=Text("To-Do List") self.add(text) self.play(Write(text)) self.wait() text2=text.copy() text2.move_to(np.array([0,3,0])) self.play(ReplacementTransform(text,text2)) tex=BulletedList( "win32 WinMine", "win32 notepad", "x86 assembly", "linux kernel 0.12" ) self.add(tex) self.play(Write(tex))
|
效果

Title
参数 |
说明 |
scale_factor |
float或者int,缩放比例,用来控制标题大小。默认值为1,表示不做缩放 |
include_underline |
bool,表示标题是否需要下划线,默认为True |
underline_width |
float或者int,表示标题下下划线的宽度,默认为屏幕宽度减去2 |
match_underline_width_to_text |
bool,表示是否需要让下划线宽度适应于标题的宽度,默认为False |
underline_buff |
float,下划线的厚度,默认为MED_SMALL_BUFF(也就是0.25) |
数学公式渲染的标题,好处就是自动定在比较靠上的位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| from turtle import left, position from manimlib import *
class Main(Scene): def construct(self): title=Title( r"TODO List" ) self.play(Write(title)) tex=BulletedList( "win32 WinMine", "win32 notepad", "x86 assembly", "linux kernel 0.12" ) self.add(tex) self.play(Write(tex))
|

如果设置tex支持中文,则渲染效果是很好的,但是所有数学公式的渲染都需要花费时间,这是tex相对于text的最大缺点
BraceLabel
SingleStringTex的派生类,增强的Brace,可以在花括号上写字了
用于标注形体长度等等
1 2 3 4 5 6 7 8 9 10 11 12
| from manimlib import *
class Main(Scene): def construct(self): circle=Circle() self.play(Write(circle)) bl=BraceLabel( circle, text="d=10", brace_direction=UP ) self.play(Write(bl))
|

BraceText
类似BraceLabel,但是允许$行内公式$
1 2 3 4 5 6 7 8 9 10 11 12
| from manimlib import *
class Main(Scene): def construct(self): circle=Circle() self.play(Write(circle)) bt=BraceText( obj=circle, text="直径$=10$", brace_direction=UP ) self.play(Write(bt))
|
