LaTeX 和 Python 适用于 Ubuntu 但不适用于 Windows

LaTeX and Python works on Ubuntu but not on Windows

我对 LaTeX 不是特别流利,但我需要根据原始输入 LaTeX 代码生成一堆 SVG 文件。我发现这样做的一种方法是使用名为 "latex2svg" 的 Python 脚本,位于此处:https://github.com/tuxu/latex2svg.

我可以在 Linux 中成功执行此操作,但在 Windows 中不能,我不明白为什么。在Ubuntu中,如果我运行:

sudo apt-get install texlive-full

安装 LaTeX,然后创建以下 Python 脚本(与 latex2svg.py 位于同一目录):

from latex2svg import latex2svg

myeq1 = r'e^{i\pi}+1=0'
myeq2 = r'\mathbb{Q}'
myeq3 = r'\int_{-\infty}^{\infty}{\frac{e^{\frac{-x^2}{2}}}{\sqrt{2\pi}}}\ dx=1'

eqs = [myeq1, myeq2, myeq3]

for ii, eq in enumerate(eqs):
  myeq = r'\( ' + eq + r' \)'
  out = latex2svg(myeq)
  with open('out{}.svg'.format(ii), 'w') as f:
    f.write(out['svg'])

一切正常,并且正确生成 SVG 文件(参见 here):

但是,在 Windows 10 上,安装了 MikTeX,如果我 运行 完全相同的 Python 脚本,我会收到警告 Warning: libgs not found,但脚本会继续并输出一些半破损的 SVG(看到它们 here). I tried to install libgs with pip, but I get this horrible error 并且不知道该怎么做。

我需要做什么才能让它在 Windows 上运行?虽然我在 Ubuntu 上工作,但我想了解这个问题,让它在 Windows 上工作。

不用走python的弯路,直接编译成svg即可

latex filename
dvisvgm filename.dvi

(其中文件名是您的 filename.tex 文件的基本名称)

我想通了,尽管这花了我很长时间。问题是我没有在“latex2svg.py”文件中列出的包:

\usepackage[libertine]{newtxmath}

如果您删除该行,它会呈现正常。我还不确定如何在 Windows 上安装它,但这是问题的症结所在。