You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Manim官方Text示例运行报错,请求问题排查与解决方法

Manim Text组件报错问题排查与解决

问题背景

参考Manim官方示例页面的TextExample场景,精简得到以下最小化代码:

class TextExample(Scene):
    def construct(self):
        # To run this scene properly, you should have "Consolas" font in your computer
        text = Text("Here is a text", font="Arial", font_size=90)
        difference = Text(
            """
            The most important difference between Text and TexText is that\n
            you can change the font more easily, but can't use the LaTeX grammar
            """,
            font="Arial", font_size=24,
            # t2c is a dict that you can choose color for different text
            t2c={
                "Text": BLUE, "TexText": BLUE, "LaTeX": ORANGE
            }
        )
        VGroup(text, difference).arrange(DOWN, buff=1)
        self.play(Write(text))
        self.play(FadeIn(difference, UP))
        self.wait(3)

报错1:t2c参数触发歧义错误

使用t2c设置文本颜色时,出现以下错误:

text_mobject.py:678, in Text._merge_settings(self, left_setting, right_setting, default_args)
    676     default = default_args[arg]
    677     if left != default and getattr(right_setting, arg) != default:
--> 678         raise ValueError(
    679             f"Ambiguous style for text '{self.text[right_setting.start:right_setting.end]}':"
    680             + f"'{arg}' cannot be both '{left}' and '{right}'."
    681         )
    682     setattr(right_setting, arg, left if left != default else right)
    683 return new_setting

ValueError: Ambiguous style for text 'Text':'color' cannot be both '#58C4DD' and '#58C4DD'.

报错2:注释t2c后仍报错

即使注释掉t2c代码段,仍触发类型错误:

mobject.py:430, in Mobject.add(self, *mobjects)
    428 for m in mobjects:
    429     if not isinstance(m, Mobject):
--> 430         raise TypeError("All submobjects must be of type Mobject")
    431     if m is self:
    432         raise ValueError("Mobject cannot contain self")

TypeError: All submobjects must be of type Mobject

原因分析与解决方法

1. t2c参数歧义错误的原因

文本中存在重复匹配的字符串:difference的文本里,"Text"既作为独立单词存在,也作为"TexText"的前缀被匹配,导致Manim试图给同一部分文本重复设置相同颜色,触发内部样式冲突检查。

解决方法

确保t2c匹配的字符串唯一且不重叠,或改用更灵活的MarkupText组件(支持HTML标记):

difference = MarkupText(
    "The most important difference between <span color='blue'>Text</span> and <span color='blue'>TexText</span> is that\n"
    "you can change the font more easily, but can't use the <span color='orange'>LaTeX</span> grammar",
    font="Arial", font_size=24
)

2. 注释t2c后仍报错的原因

文本中存在多余空行或首尾空白字符,导致Text组件解析时生成了非Mobject类型的空内容块,添加到VGroup时触发类型检查失败。

解决方法

清理文本中的多余空白,确保字符串格式规整:

difference = Text(
    "The most important difference between Text and TexText is that\n"
    "you can change the font more easily, but can't use the LaTeX grammar",
    font="Arial", font_size=24
)

3. 官方文档是否过时?

是的,你参考的示例基于旧版Manim(v0.x版本),当前主流的Manim v1.x+ API已发生较多变化:

  • 旧版Text的t2c等参数在新版中存在兼容性问题
  • 新版推荐使用MarkupText实现富文本样式,Tex/MathTex处理LaTeX内容

完整修复后的代码

from manim import *

class TextExample(Scene):
    def construct(self):
        text = Text("Here is a text", font="Arial", font_size=90)
        difference = MarkupText(
            "The most important difference between <span color='blue'>Text</span> and <span color='blue'>TexText</span> is that\n"
            "you can change the font more easily, but can't use the <span color='orange'>LaTeX</span> grammar",
            font="Arial", font_size=24
        )
        VGroup(text, difference).arrange(DOWN, buff=1)
        self.play(Write(text))
        self.play(FadeIn(difference, UP))
        self.wait(3)

内容的提问来源于stack exchange,提问作者Qiang Li

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 15:14:59