manim: Write Chinese words problem!

Hi,I am Chinese, so I need to use it to write some Chinese words, I have tried to use template.tex as README.TXT said, and I alse tried to add below codes at the beginning,It still not work in ./example_scenes.py WriteStuff

 #!/usr/bin/python
# -*- coding: UTF-8 -*-

here is error message,someone please help image

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 20 (4 by maintainers)

Most upvoted comments

title = Text('测试中文字符', font='Weibei SC')

Try to set the font that your computer has that support Chinese characters. This way works.

中国,如果下载的是最新版本的manim,并且只是想要在manim中显示中文, 对于LaTeX没有中文显示需求的, 可以直接按照如下操作进行.

直接修改 manimlib/constants.py 第50行的 TEX_USE_CTEX = FalseTEX_USE_CTEX = True


now, we can modify directly in manimlib/constants.py , change line 50, TEX_USE_CTEX = False to TEX_USE_CTEX = True

windows 路径问题

os.path.join(consts.TEX_DIR,"xxxx.tex") #会拼接 `\` 到文件路径中,需要做转换
result = Path(result).as_posix() # 替换 `\` 为 `/`
tex_file = Path(tex_file).as_posix()
tex_dir = Path(consts.TEX_DIR).as_posix()

LaTex 中文问题 修改constants.py line 50 TEX_USE_CTEX = True 对应逻辑: 1.使用CTEX模板文件 (默认为TEX模板文件) 2.使用XDV文件生成视频图片 (默认使用DVI)

Edit (October 15, 2019):

As per #315, now we only have to change the line

TEX_USE_CTEX = False

to

TEX_USE_CTEX = True

in the file manimlib/constants.py.

See below.


Original Comment

@pu17 Try changing manim/mobject/svg/svg_mobject.py, lines 50-63:

def ensure_valid_file(self):
        if self.file_name is None:
            raise Exception("Must specify file for SVGMobject")
        possible_paths = [
            os.path.join(SVG_IMAGE_DIR, self.file_name),
            os.path.join(SVG_IMAGE_DIR, self.file_name + ".svg"),
            self.file_name,
        ]
        for path in possible_paths:
            if os.path.exists(path):
                self.file_path = path
                return
        raise IOError("No file matching %s in image directory" %
                      self.file_name)

to

def ensure_valid_file(self):
        if self.file_name is None:
            raise Exception("Must specify file for SVGMobject")
        possible_paths = [
            os.path.join(SVG_IMAGE_DIR, self.file_name),
            os.path.join(SVG_IMAGE_DIR, self.file_name + ".svg"),
            os.path.join(SVG_IMAGE_DIR, self.file_name + ".xdv"),
            self.file_name,
        ]
        for path in possible_paths:
            if os.path.exists(path):
                self.file_path = path
                return
        raise IOError("No file matching %s in image directory" %
                      self.file_name)

2020 修改 / Edit:

据#315,只要在manimlib/constants.py改下面一行: / As per #315, now you only have to change the line in the file manimlib/constants.py:

TEX_USE_CTEX = False

改成 / to

TEX_USE_CTEX = True

就好了。

下面评论。 / See comment below.

这一行把 LaTeX 命令改成 XeLaTeX 来生成 xdv 而不是 dvi 文件,然后用它来生成 svg,并使用 ctex_template.tex 代替 tex_template.tex。 / This line replaces the LaTeX command with XeLaTeX, and outputs xdv instead of dvi to generate the svg file. It also uses the ctex_template.tex template instead of tex_template.tex.


原始评论 / Original comment

You can write Chinese with the following changes:

In tex_template.tex, change lines 19-20:

\DisableLigatures{encoding = *, family = * }
%\usepackage[UTF8]{ctex}

to:

%\DisableLigatures{encoding = *, family = * }  
\usepackage[UTF8]{ctex}

Then, in utils/tex_file_writing.py, change lines 46-88:

def tex_to_dvi(tex_file):
    result = tex_file.replace(".tex", ".dvi")
    if not os.path.exists(result):
        commands = [
            "latex",
            "-interaction=batchmode",
            "-halt-on-error",
            "-output-directory=" + TEX_DIR,
            tex_file,
            ">",
            get_null()
        ]
        exit_code = os.system(" ".join(commands))
        if exit_code != 0:
            log_file = tex_file.replace(".tex", ".log")
            raise Exception(
                "Latex error converting to dvi. "
                "See log output above or the log file: %s" % log_file)
    return result


def dvi_to_svg(dvi_file, regen_if_exists=False):
    """
    Converts a dvi, which potentially has multiple slides, into a
    directory full of enumerated pngs corresponding with these slides.
    Returns a list of PIL Image objects for these images sorted as they
    where in the dvi
    """
    result = dvi_file.replace(".dvi", ".svg")
    if not os.path.exists(result):
        commands = [
            "dvisvgm",
            dvi_file,
            "-n",
            "-v",
            "0",
            "-o",
            result,
            ">",
            get_null()
        ]
        os.system(" ".join(commands))
    return result

to:

def tex_to_dvi(tex_file):
    # result = tex_file.replace(".tex", ".dvi")
    result = tex_file.replace(".tex", ".xdv")
    if not os.path.exists(result):
        # commands = [
        #     "latex",
        #     "-interaction=batchmode",
        #     "-halt-on-error",
        #     "-output-directory=" + TEX_DIR,
        #     tex_file,
        #     ">",
        #     get_null()
        # ]
        commands = [
            "xelatex",
            "-no-pdf",
            "-interaction=batchmode",
            "-halt-on-error",
            "-output-directory=" + TEX_DIR,
            tex_file,
            ">",
            get_null()
        ]
        exit_code = os.system(" ".join(commands))
        if exit_code != 0:
            log_file = tex_file.replace(".tex", ".log")
            raise Exception(
                "Latex error converting to dvi. "
                "See log output above or the log file: %s" % log_file)
    return result


def dvi_to_svg(dvi_file, regen_if_exists=False):
    """
    Converts a dvi, which potentially has multiple slides, into a
    directory full of enumerated pngs corresponding with these slides.
    Returns a list of PIL Image objects for these images sorted as they
    where in the dvi
    """
    # result = dvi_file.replace(".dvi", ".svg")
    result = dvi_file.replace(".xdv", ".svg")
    if not os.path.exists(result):
        commands = [
            "dvisvgm",
            dvi_file,
            "-n",
            "-v",
            "0",
            "-o",
            result,
            ">",
            get_null()
        ]
        os.system(" ".join(commands))
    return result

Reference: PR #48 Issue #64

@SniperM99 Thanks! I’m interested why don’t set TEX_USE_CTEX = True as default vaule?

@DGideas It was like that a few years ago (see #48, the PR by @Solara570 back in 2017), but CTEX uses XeLaTeX which causes a lot of other problems like #64 or #124 or #142 (these were really old issues). Eventually it was deleted in #200.