API 参考:文本与公式(text)¶
共 14 个类,按字母排序。每个类含中文说明、继承链、参数表与上手示例,API 文档由 autodoc 从 manim v0.21.0 源码自动生成;带完整中文精讲的类见 API 索引。
BulletedList¶
项目符号列表:字符串数组一键排成带圆点的竖排列表,演示文稿风。
继承关系¶
参数¶
|
0.5,float |
|---|---|
|
2,float |
|
None,str | None |
|
0.1,float |
快速上手¶
bl = BulletedList("第一项", "第二项", "第三项")
API 文档¶
Code¶
代码高亮块:从文件或字符串渲染带语法着色的代码段,主题、字体、行号可调。
继承关系¶
参数¶
|
None,StrPath | None |
|---|---|
|
None,str | None |
|
None,str | None |
|
'vim',str | type[Style] |
|
4,int |
|
True,bool |
|
1,int |
|
'rectangle' |
|
None,dict[str, Any] | None |
|
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 做计数器。
继承关系¶
参数¶
|
0,float |
|---|---|
|
2,int |
|
|
|
False,bool |
|
True,bool |
|
0.001,float |
|
False,bool |
|
None,str | None |
|
0,float |
|
False,bool |
|
array([-1., 0., 0.]),Vector3DLike |
|
48,float |
|
0,float |
|
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
DecimalNumberto a new number.Parameters¶
- number
The value that will overwrite the current number of the
DecimalNumber.
Integer¶
整数版 DecimalNumber:无小数点,步进计数常用。
继承关系¶
参数¶
|
0,float |
|---|---|
|
0,int |
API 文档¶
- class manim.Integer(number: float = 0, num_decimal_places: int = 0, **kwargs: Any)¶
MarkupText¶
Pango 标记语言文本:字符串里直接写 b/i/span 等标签实现富文本混排。
继承关系¶
参数¶
|
—,str |
|---|---|
|
1,float |
|
0,float |
|
None |
|
48,float |
|
-1,float |
|
'',str |
|
'NORMAL',str |
|
'NORMAL',str |
|
False,bool |
|
None |
|
4,int |
|
None,int | None |
|
None,int | None |
|
True,bool |
|
False,bool |
|
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 片段是独立子对象,可按下标索引做局部动画,数学视频的核心。
继承关系¶
参数¶
|
' ',str |
|---|---|
|
None,Iterable[str] | None |
|
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)¶
-
- 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。
继承关系¶
参数¶
API 文档¶
Paragraph¶
多段落文本块:长文字自动分行成段,排文章类内容用。
继承关系¶
参数¶
|
-1,float |
|---|---|
|
None,str | None |
API 文档¶
SingleStringMathTex¶
单个 LaTeX 字符串的公式对象:不做分词切片,MathTex 的底层形式。
继承关系¶
参数¶
|
—,str |
|---|---|
|
0,float |
|
True,bool |
|
None,float | None |
|
False,bool |
|
|
|
None,TexTemplate | None |
|
48,float |
|
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 页面效果。
继承关系¶
参数¶
|
'',str |
|---|---|
|
'center',str | None |
API 文档¶
Text¶
Pango 渲染的系统字体文本:中文、emoji 原生支持,t2c/t2f 子串改色改字体,文字类内容首选。
继承关系¶
参数¶
|
—,str |
|---|---|
|
1.0,float |
|
0,float |
|
None |
|
48,float |
|
-1,float |
|
'',str |
|
'NORMAL',str |
|
'NORMAL',str |
|
None,dict[str, str] | None |
|
None,dict[str, str] | None |
|
None |
|
None,dict[str, str] | None |
|
None,dict[str, str] | None |
|
None |
|
4,int |
|
True,bool |
|
None,float | None |
|
None,float | None |
|
True,bool |
|
False,bool |
|
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¶
居中放大加下划线的标题文本:章节标题专用。
继承关系¶
参数¶
|
True,bool |
|---|---|
|
False,bool |
|
0.25,float |
快速上手¶
self.add(Title("第三章:动画进阶"))
API 文档¶
Typst¶
Typst 文档模式文本(v0.21 新增):整个文档用 Typst 排版,类似 Tex 的定位。
继承关系¶
参数¶
|
—,str |
|---|---|
|
48,float |
|
'',str |
|
None |
|
None,float | None |
|
None,list[str | Path] | None |
|
False,bool |
|
True,bool |
|
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 ofSingleStringMathTex.init_colors(): any submobject whose color is black is recolored toself.color, while explicitly colored submobjects (non-black) are preserved.
- modify_xml_tree(element_tree: ElementTree) ElementTree¶
Convert
data-typst-labelattributes toidbefore parsing.Typst's SVG renderer emits
data-typst-labelon<g>elements that carry a label (created via#box(body) <label>). Thesvgelementslibrary propagates customdata-*attributes from parent groups to all children, making them unusable as unique group keys.idattributes, on the other hand, are not inherited.This method walks the XML tree and promotes every
data-typst-labeltoid(on<g>elements only), so thatget_mobjects_from()can pick them up via its existingid-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¶
VMobjectself
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
manimgrphelper or automatically through the{{ }}double-brace notation inMathTypst.Parameters¶
- key
A label name (
str) matching adata-typst-labelin 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 实时联动。
继承关系¶
参数¶
|
—,float |
|---|---|
|
— |
|
|
|
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