text_mobject

用于显示(非 LaTeX)文本的Mobject。

注意

正如你可以使用 TexMathTex(来自模块 tex_mobject)在视频中插入LaTeX一样,你可以使用 Text 添加普通文本。

重要提示

请参阅相应的教程 不使用LaTeX的文本,特别是关于字体的信息。

在动画中添加文本最简单的方法是使用 Text 类。它使用 Pango 库来渲染文本。通过 Pango,你还可以渲染非英文字母,例如 你好こんにちは안녕하세요مرحبا بالعالم

示例

示例:HelloWorld

../_images/HelloWorld-2.png
from manim import *

class HelloWorld(Scene):
    def construct(self):
        text = Text('Hello world').scale(3)
        self.add(text)
class HelloWorld(Scene):
    def construct(self):
        text = Text('Hello world').scale(3)
        self.add(text)

示例:文本对齐

../_images/TextAlignment-1.png
from manim import *

class TextAlignment(Scene):
    def construct(self):
        title = Text("K-means clustering and Logistic Regression", color=WHITE)
        title.scale(0.75)
        self.add(title.to_edge(UP))

        t1 = Text("1. Measuring").set_color(WHITE)

        t2 = Text("2. Clustering").set_color(WHITE)

        t3 = Text("3. Regression").set_color(WHITE)

        t4 = Text("4. Prediction").set_color(WHITE)

        x = VGroup(t1, t2, t3, t4).arrange(direction=DOWN, aligned_edge=LEFT).scale(0.7).next_to(ORIGIN,DR)
        x.set_opacity(0.5)
        x.submobjects[1].set_opacity(1)
        self.add(x)
class TextAlignment(Scene):
    def construct(self):
        title = Text("K-means clustering and Logistic Regression", color=WHITE)
        title.scale(0.75)
        self.add(title.to_edge(UP))

        t1 = Text("1. Measuring").set_color(WHITE)

        t2 = Text("2. Clustering").set_color(WHITE)

        t3 = Text("3. Regression").set_color(WHITE)

        t4 = Text("4. Prediction").set_color(WHITE)

        x = VGroup(t1, t2, t3, t4).arrange(direction=DOWN, aligned_edge=LEFT).scale(0.7).next_to(ORIGIN,DR)
        x.set_opacity(0.5)
        x.submobjects[1].set_opacity(1)
        self.add(x)

标记文本

使用 Pango 渲染显示(非 LaTeX)文本。

段落

显示一段文本。

文本

函数

register_font(font_file)[源文件]

临时将字体文件添加到 Pango 的搜索路径中。

它在各种位置搜索 font_file。搜索顺序如下所述。

  1. 绝对路径。

  2. assets/fonts 文件夹中。

  3. font/ 文件夹中。

  4. 在同一目录下。

参数:

font_file (str | Path) – 要添加的字体文件。

示例

使用 with register_font(...) 将字体文件添加到搜索路径。

with register_font("path/to/font_file.ttf"):
    a = Text("Hello", font="Custom Font Name")
抛出:
  • FileNotFoundError: – 如果字体不存在。

  • AttributeError: – 如果此方法在 macOS 上使用。

  • .. important :: – 此方法适用于 ManimPango>=v0.2.3 版本的 macOS。在 macOS 上使用此方法及更早版本将引发 AttributeError

参数:

font_file (str | Path)

remove_invisible_chars(mobject)[源文件]

从某些 mobject 中移除不需要的不可见字符的函数。

参数:

mobject (SVGMobject) – 任何我们希望从中移除不需要的不可见字符的 SVGMobject。

返回:

不含不需要的不可见字符的 SVGMobject。

返回类型:

SVGMobject