Skip to content

Instantly share code, notes, and snippets.

@onemore118
Last active June 6, 2024 03:30
Show Gist options
  • Save onemore118/36d8c754f8be76f6df88906d0e2596b7 to your computer and use it in GitHub Desktop.
Save onemore118/36d8c754f8be76f6df88906d0e2596b7 to your computer and use it in GitHub Desktop.
修复Manim中文无法编译问题:ValueError: latex error converting to dvi.

前提

  1. 安装Manim Community版本
  2. 安装MikTex
  3. 进入MikTex,安装宏包(Package): ctex
  4. 重启电脑

代码

  1. 进入anaconda\envs\<你的anaconda环境名称>\Lib\site-packages\manim\utils\tex_file_writing.py 找到如下代码,大概在296行左右
if error_indices:
    with tex_file.open() as f:
        tex = f.readlines()

添加encoding="utf-8",修改为如下:

if error_indices:
    with tex_file.open(encoding="utf-8") as f:
        tex = f.readlines()
  1. 在自己使用Manim的py文件中,添加如下代码:
# 创建自定义 LaTeX 模板,使用中文包
my_tex_template = TexTemplate()
my_tex_template.add_to_preamble(r"""
\usepackage{ctex}
""")

# 将自定义模板配置到 config 中
config.tex_template = my_tex_template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment