在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
相关产品推荐
相关产品推荐

