API 参考:文本与公式(text)

共 14 个类,按字母排序。每个类含中文说明、继承链、参数表与上手示例,API 文档由 autodoc 从 manim v0.21.0 源码自动生成;带完整中文精讲的类见 API 索引。

BulletedList

项目符号列表:字符串数组一键排成带圆点的竖排列表,演示文稿风。

继承关系

G Mobject Mobject VMobject VMobject Mobject->VMobject SVGMobject SVGMobject VMobject->SVGMobject SingleStringMathTex SingleStringMathTex SVGMobject->SingleStringMathTex MathTex MathTex SingleStringMathTex->MathTex Tex Tex MathTex->Tex BulletedList BulletedList Tex->BulletedList

参数

buff

0.5,float

dot_scale_factor

2,float

tex_environment

None,str | None

dot_buff

0.1,float

快速上手

bl = BulletedList("第一项", "第二项", "第三项")

API 文档

class manim.BulletedList(*items: str, buff: float = 0.5, dot_scale_factor: float = 2, tex_environment: str | None = None, dot_buff: float = 0.1, **kwargs: Any)

基类:Tex

Code

代码高亮块:从文件或字符串渲染带语法着色的代码段,主题、字体、行号可调。

继承关系

G Mobject Mobject VMobject VMobject Mobject->VMobject Code Code VMobject->Code

参数

code_file

None,StrPath | None

code_string

None,str | None

language

None,str | None

formatter_style

'vim',str | type[Style]

tab_width

4,int

add_line_numbers

True,bool

line_numbers_from

1,int

background

'rectangle'

background_config

None,dict[str, Any] | None

paragraph_config

None,dict[str, Any] | None

快速上手

code = Code("script.py", language="python")

API 文档

class manim.Code(code_file: str | PathLike[str] | None = None, code_string: str | None = None, language: str | None = None, formatter_style: str | type[Style] = 'vim', tab_width: int = 4, add_line_numbers: bool = True, line_numbers_from: int = 1, background: Literal['rectangle', 'window'] = 'rectangle', background_config: dict[str, Any] | None = None, paragraph_config: dict[str, Any] | None = None)

基类:VMobject

classmethod get_pygments_style(name: str) → type[Style]

Return the Pygments style registered under name.

Parameters

name

The name of the Pygments style to retrieve.

Returns

type[Style]

The corresponding Pygments style class.

classmethod get_styles_list() → list[str]

Get the list of all available formatter styles.

DecimalNumber

可动小数的数字文本:set_value 即重排,配 ValueTracker 做计数器。

继承关系

G Mobject Mobject VMobject VMobject Mobject->VMobject DecimalNumber DecimalNumber VMobject->DecimalNumber

参数

number

0,float

num_decimal_places

2,int

mob_class

<class 'manim.mobject.text.…

include_sign

False,bool

group_with_commas

True,bool

digit_buff_per_font_unit

0.001,float

show_ellipsis

False,bool

unit

None,str | None

unit_buff_per_font_unit

0,float

include_background_rectangle

False,bool

edge_to_fix

array([-1., 0., 0.]),Vector3DLike

font_size

48,float

stroke_width

0,float

fill_opacity

1.0,float

快速上手

num = DecimalNumber(0, num_decimal_places=2)

API 文档

class manim.DecimalNumber(number: float = 0, num_decimal_places: int = 2, mob_class: type[SingleStringMathTex] = <class 'manim.mobject.text.tex_mobject.MathTex'>, include_sign: bool = False, group_with_commas: bool = True, digit_buff_per_font_unit: float = 0.001, show_ellipsis: bool = False, unit: str | None = None, unit_buff_per_font_unit: float = 0, include_background_rectangle: bool = False, edge_to_fix: float64] | tuple[float, float, float]=array([-1., 0., 0.]), font_size: float = 48, stroke_width: float = 0, fill_opacity: float = 1.0, **kwargs: Any)

基类:VMobject

property font_size: float

The font size of the tex mobject.

set_value(number: float) → Self

Set the value of the DecimalNumber to a new number.

Parameters

number

The value that will overwrite the current number of the DecimalNumber.

Integer

整数版 DecimalNumber:无小数点,步进计数常用。

继承关系

G Mobject Mobject VMobject VMobject Mobject->VMobject DecimalNumber DecimalNumber VMobject->DecimalNumber Integer Integer DecimalNumber->Integer

参数

number

0,float

num_decimal_places

0,int

API 文档

class manim.Integer(number: float = 0, num_decimal_places: int = 0, **kwargs: Any)

基类:DecimalNumber

MarkupText

Pango 标记语言文本:字符串里直接写 b/i/span 等标签实现富文本混排。

继承关系

G Mobject Mobject VMobject VMobject Mobject->VMobject SVGMobject SVGMobject VMobject->SVGMobject MarkupText MarkupText SVGMobject->MarkupText

参数

text

—,str

fill_opacity

1,float

stroke_width

0,float

color

None

font_size

48,float

line_spacing

-1,float

font

'',str

slant

'NORMAL',str

weight

'NORMAL',str

justify

False,bool

gradient

None

tab_width

4,int

height

None,int | None

width

None,int | None

should_center

True,bool

disable_ligatures

False,bool

warn_missing_font

True,bool

快速上手

mt = MarkupText('<b>bold</b> <span foreground="#FFFF00">yellow</span>')

API 文档

class manim.MarkupText(text: str, fill_opacity: float = 1, stroke_width: float = 0, color: ManimColor | int | str | NDArray[int64] | tuple[int, int, int] | NDArray[float64] | tuple[float, float, float] | tuple[int, int, int, int] | tuple[float, float, float, float] | None = None, font_size: float = 48, line_spacing: float = -1, font: str = '', slant: str = 'NORMAL', weight: str = 'NORMAL', justify: bool = False, gradient: Iterable[ManimColor | int | str | NDArray[int64] | tuple[int, int, int] | NDArray[float64] | tuple[float, float, float] | tuple[int, int, int, int] | tuple[float, float, float, float]] | None = None, tab_width: int = 4, height: int | None = None, width: int | None = None, should_center: bool = True, disable_ligatures: bool = False, warn_missing_font: bool = True, **kwargs: Any)

基类:SVGMobject

MathTex

LaTeX 公式文本:每个 LaTeX 片段是独立子对象,可按下标索引做局部动画,数学视频的核心。

继承关系

G Mobject Mobject VMobject VMobject Mobject->VMobject SVGMobject SVGMobject VMobject->SVGMobject SingleStringMathTex SingleStringMathTex SVGMobject->SingleStringMathTex MathTex MathTex SingleStringMathTex->MathTex

参数

arg_separator

' ',str

substrings_to_isolate

None,Iterable[str] | None

tex_to_color_map

None

tex_environment

'align*',str | None

快速上手

eq = MathTex(r" rac{a}{b} = \sqrt{c}")

API 文档

class manim.MathTex(*tex_strings: str, arg_separator: str = ' ', substrings_to_isolate: Iterable[str] | None = None, tex_to_color_map: dict[str, ManimColor | int | str | NDArray[int64] | tuple[int, int, int] | NDArray[float64] | tuple[float, float, float] | tuple[int, int, int, int] | tuple[float, float, float, float]] | None = None, tex_environment: str | None = 'align*', **kwargs: Any)

基类:SingleStringMathTex

set_opacity_by_tex(tex: str, opacity: float = 0.5, remaining_opacity: float | None = None, **kwargs: Any) → Self

Sets the opacity of the tex specified. If 'remaining_opacity' is specified, then the remaining tex will be set to that opacity.

Parameters

tex

The tex to set the opacity of.

opacity

Default 0.5. The opacity to set the tex to

remaining_opacity

Default None. The opacity to set the remaining tex to. If None, then the remaining tex will not be changed

MathTypst

Typst 排版的公式对象(v0.21 新增):用 Typst 语法替代 LaTeX,需安装 typst。

继承关系

G Mobject Mobject VMobject VMobject Mobject->VMobject SVGMobject SVGMobject VMobject->SVGMobject Typst Typst SVGMobject->Typst MathTypst MathTypst Typst->MathTypst

参数

API 文档

class manim.MathTypst(math_expression: str, **kwargs: Any)

基类:Typst

Paragraph

多段落文本块:长文字自动分行成段,排文章类内容用。

继承关系

G Mobject Mobject VMobject VMobject Mobject->VMobject VGroup VGroup VMobject->VGroup Paragraph Paragraph VGroup->Paragraph

参数

line_spacing

-1,float

alignment

None,str | None

API 文档

class manim.Paragraph(*text: str, line_spacing: float = -1, alignment: str | None = None, **kwargs: Any)

基类:VGroup

SingleStringMathTex

单个 LaTeX 字符串的公式对象:不做分词切片,MathTex 的底层形式。

继承关系

G Mobject Mobject VMobject VMobject Mobject->VMobject SVGMobject SVGMobject VMobject->SVGMobject SingleStringMathTex SingleStringMathTex SVGMobject->SingleStringMathTex

参数

tex_string

—,str

stroke_width

0,float

should_center

True,bool

height

None,float | None

organize_left_to_right

False,bool

tex_environment

'align*',str | None

tex_template

None,TexTemplate | None

font_size

48,float

color

None

API 文档

class manim.SingleStringMathTex(tex_string: str, stroke_width: float = 0, should_center: bool = True, height: float | None = None, organize_left_to_right: bool = False, tex_environment: str | None = 'align*', tex_template: TexTemplate | None = None, font_size: float = 48, color: ManimColor | int | str | NDArray[int64] | tuple[int, int, int] | NDArray[float64] | tuple[float, float, float] | tuple[int, int, int, int] | tuple[float, float, float, float] | None = None, **kwargs: Any)

基类:SVGMobject

property font_size: float

The font size of the tex mobject.

init_colors(propagate_colors: bool = True) → Self

Initializes the colors.

Gets called upon creation. This is an empty method that can be implemented by subclasses.

Tex

LaTeX 完整文档模式:可加载宏包与自定义导言区,出整个 LaTeX 页面效果。

继承关系

G Mobject Mobject VMobject VMobject Mobject->VMobject SVGMobject SVGMobject VMobject->SVGMobject SingleStringMathTex SingleStringMathTex SVGMobject->SingleStringMathTex MathTex MathTex SingleStringMathTex->MathTex Tex Tex MathTex->Tex

参数

arg_separator

'',str

tex_environment

'center',str | None

API 文档

class manim.Tex(*tex_strings: str, arg_separator: str = '', tex_environment: str | None = 'center', **kwargs: Any)

基类:MathTex

Text

Pango 渲染的系统字体文本:中文、emoji 原生支持,t2c/t2f 子串改色改字体,文字类内容首选。

继承关系

G Mobject Mobject VMobject VMobject Mobject->VMobject SVGMobject SVGMobject VMobject->SVGMobject Text Text SVGMobject->Text

参数

text

—,str

fill_opacity

1.0,float

stroke_width

0,float

color

None

font_size

48,float

line_spacing

-1,float

font

'',str

slant

'NORMAL',str

weight

'NORMAL',str

t2c

None,dict[str, str] | None

t2f

None,dict[str, str] | None

t2g

None

t2s

None,dict[str, str] | None

t2w

None,dict[str, str] | None

gradient

None

tab_width

4,int

warn_missing_font

True,bool

height

None,float | None

width

None,float | None

should_center

True,bool

disable_ligatures

False,bool

use_svg_cache

False,bool

快速上手

t = Text("你好,Manim!", t2c={"Manim": YELLOW})

API 文档

class manim.Text(text: str, fill_opacity: float = 1.0, stroke_width: float = 0, color: ManimColor | int | str | NDArray[int64] | tuple[int, int, int] | NDArray[float64] | tuple[float, float, float] | tuple[int, int, int, int] | tuple[float, float, float, float] | None = None, font_size: float = 48, line_spacing: float = -1, font: str = '', slant: str = 'NORMAL', weight: str = 'NORMAL', t2c: dict[str, str] | None = None, t2f: dict[str, str] | None = None, t2g: dict[str, Iterable[ManimColor | int | str | NDArray[int64] | tuple[int, int, int] | NDArray[float64] | tuple[float, float, float] | tuple[int, int, int, int] | tuple[float, float, float, float]]] | None = None, t2s: dict[str, str] | None = None, t2w: dict[str, str] | None = None, gradient: Iterable[ManimColor | int | str | NDArray[int64] | tuple[int, int, int] | NDArray[float64] | tuple[float, float, float] | tuple[int, int, int, int] | tuple[float, float, float, float]] | None = None, tab_width: int = 4, warn_missing_font: bool = True, height: float | None = None, width: float | None = None, should_center: bool = True, disable_ligatures: bool = False, use_svg_cache: bool = False, **kwargs: Any)

基类:SVGMobject

init_colors(propagate_colors: bool = True) → Self

Initializes the colors.

Gets called upon creation. This is an empty method that can be implemented by subclasses.

Title

居中放大加下划线的标题文本:章节标题专用。

继承关系

G Mobject Mobject VMobject VMobject Mobject->VMobject SVGMobject SVGMobject VMobject->SVGMobject SingleStringMathTex SingleStringMathTex SVGMobject->SingleStringMathTex MathTex MathTex SingleStringMathTex->MathTex Tex Tex MathTex->Tex Title Title Tex->Title

参数

include_underline

True,bool

match_underline_width_to_text

False,bool

underline_buff

0.25,float

快速上手

self.add(Title("第三章:动画进阶"))

API 文档

class manim.Title(*text_parts: str, include_underline: bool = True, match_underline_width_to_text: bool = False, underline_buff: float = 0.25, **kwargs: Any)

基类:Tex

Typst

Typst 文档模式文本(v0.21 新增):整个文档用 Typst 排版,类似 Tex 的定位。

继承关系

G Mobject Mobject VMobject VMobject Mobject->VMobject SVGMobject SVGMobject VMobject->SVGMobject Typst Typst SVGMobject->Typst

参数

typst_code

—,str

font_size

48,float

typst_preamble

'',str

color

None

stroke_width

None,float | None

font_paths

None,list[str | Path] | None

track_baselines

False,bool

should_center

True,bool

height

None,float | None

API 文档

class manim.Typst(typst_code: str, *, font_size: float = 48, typst_preamble: str = '', color: ManimColor | int | str | NDArray[int64] | tuple[int, int, int] | NDArray[float64] | tuple[float, float, float] | tuple[int, int, int, int] | tuple[float, float, float, float] | None = None, stroke_width: float | None = None, font_paths: list[str | Path] | None = None, track_baselines: bool = False, should_center: bool = True, height: float | None = None, **kwargs: Any)

基类:SVGMobject

property baseline_frames: list[tuple[ndarray, ndarray, ndarray]]

Current Typst baseline frames for all tracked leaf submobjects.

property font_size: float

The font size of the Typst mobject.

get_baseline_frame(submobject: VMobject) → tuple[ndarray, ndarray, ndarray]

Return the current Typst baseline frame for a tracked submobject.

The returned tuple contains the current positions of (orig, right, up). These are recovered from the stored reference frame and the submobject's current affine position in the scene.

get_mob_from_shape_element(shape: SVGElement) → VMobject | None

Attach Typst-specific metadata to imported shape mobjects.

property hash_seed: tuple

Include baseline tracking in the SVG cache key.

init_colors(propagate_colors: bool = True) → Self

Recolor black submobjects to self.color.

Typst renders text in black (fill="#000000") by default. This mirrors the approach of SingleStringMathTex.init_colors(): any submobject whose color is black is recolored to self.color, while explicitly colored submobjects (non-black) are preserved.

modify_xml_tree(element_tree: ElementTree) → ElementTree

Convert data-typst-label attributes to id before parsing.

Typst's SVG renderer emits data-typst-label on <g> elements that carry a label (created via #box(body) <label>). The svgelements library propagates custom data-* attributes from parent groups to all children, making them unusable as unique group keys. id attributes, on the other hand, are not inherited.

This method walks the XML tree and promotes every data-typst-label to id (on <g> elements only), so that get_mobjects_from() can pick them up via its existing id-based grouping logic.

scale(scale_factor: float, scale_stroke: bool = False, *, about_point: ndarray | None = None, about_edge: ndarray | None = None) → Self

Scale the size by a factor.

Default behavior is to scale about the center of the vmobject.

Parameters

scale_factor

The scaling factor \(\alpha\). If \(0 < |\alpha| < 1\), the mobject will shrink, and for \(|\alpha| > 1\) it will grow. Furthermore, if \(\alpha < 0\), the mobject is also flipped.

scale_stroke

Boolean determining if each submobject's outline is scaled when the object is scaled. If enabled, each submobject keeps its relative stroke width (for example, a submobject with a 2px outline scaled by a factor of .5 will have a 1px outline, while a submobject with 0px stroke remains at 0px).

kwargs

Additional keyword arguments passed to scale().

Returns

VMobject

self

Examples

class MobjectScaleExample(Scene):
    def construct(self):
        c1 = Circle(1, RED).set_x(-1)
        c2 = Circle(1, GREEN).set_x(1)

        vg = VGroup(c1, c2)
        vg.set_stroke(width=50)
        self.add(vg)

        self.play(
            c1.animate.scale(.25),
            c2.animate.scale(.25,
                scale_stroke=True)
        )

See also

move_to()

select(key: str | int) → VGroup

Select a labeled sub-expression.

Labels are created in the Typst source either manually via the manimgrp helper or automatically through the {{ }} double-brace notation in MathTypst.

Parameters

key

A label name (str) matching a data-typst-label in the SVG, or an integer index into the auto-numbered {{ }} groups (_grp-0, _grp-1, …).

Returns

VGroup

The submobjects corresponding to the selected group.

Raises

KeyError

If no group with the given label exists.

IndexError

If an integer index is out of range.

Examples

class TypstSelectExample(Scene):
    def construct(self):
        eq = MathTypst(
            "{{ a + b : num }} / {{ c : den }} = {{ lambda }} {{ x }}"
        )
        eq.select("num").set_color(RED)  # "a + b"
        eq.select("den").set_color(BLUE) # "c"
        eq.select(0).set_color(YELLOW)   # "lambda" (auto-numbered: "grp-0")
        eq.select(1).set_color(GREEN)    # "x" (auto-numbered: "grp-1")

        self.add(eq)

Variable

名字 + 数值的组合显示:变量名在上数值在下,配 ValueTracker 实时联动。

继承关系

G Mobject Mobject VMobject VMobject Mobject->VMobject Variable Variable VMobject->Variable

参数

var

—,float

label

—

var_type

<class 'manim.mobject.text.…

num_decimal_places

2,int

快速上手

v = Variable(0, "x", num_decimal_places=2)

API 文档

class manim.Variable(var: float, label: str | Tex | MathTex | Text | SingleStringMathTex, var_type: type[DecimalNumber | Integer] = <class 'manim.mobject.text.numbers.DecimalNumber'>, num_decimal_places: int = 2, **kwargs: Any)

基类:VMobject