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

在Google Colab中使用Manim绘制\mathds{C}时出现LaTeX编译错误

解决Manim绘制\mathds{C}的LaTeX编译错误

报错原因

\mathds 是LaTeX的dsfont宏包提供的命令,Manim默认的TeX编译环境没有加载这个宏包,因此LaTeX无法识别该命令,抛出“未定义的控制序列”错误。

修复方案

在创建Tex或MathTex对象时,手动指定加载dsfont宏包即可,修改后的代码如下:

方法1:使用Tex对象

%%manim -qm -v WARNING Main
from manim import TexTemplate

class Main(Scene):
    def construct(self):
        # 自定义TeX模板,添加dsfont宏包
        tex_template = TexTemplate(preamble="\\usepackage{dsfont}")
        tex = Tex("$\\mathds{C}$", tex_template=tex_template)
        self.play(Create(tex))

方法2:使用MathTex对象

%%manim -qm -v WARNING Main
from manim import TexTemplate, MathTex

class Main(Scene):
    def construct(self):
        tex_template = TexTemplate(preamble="\\usepackage{dsfont}")
        tex = MathTex("\\mathds{C}", tex_template=tex_template)
        self.play(Create(tex))

便捷优化

如果需要频繁使用这类依赖额外宏包的命令,可以全局修改Manim的默认TeX模板,避免重复配置:

from manim import config
# 给默认TeX模板追加dsfont宏包
config.tex_template.preamble += "\\usepackage{dsfont}"

之后再创建Tex或MathTex对象时,直接使用\mathds{C}就不会再报错了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 12:02:24