为什么在我使用 LaTeX 渲染文本时 python 变得无响应?

Why does python become unresponsive when I use LaTeX to render text?

我目前正在使用 python 2.7.11 运行 我在 command prompt 中的代码。我正在使用 matplotlib 为我的 LaTeX 文档创建 3D 图形。但是,当我尝试使用 LaTeX 为图像呈现文本时,我收到一条 windows 错误消息,告诉我 python.exe 已停止工作,我必须关闭程序。为什么会发生这种情况,我该如何解决?我不懂技术,所以简单的答案将不胜感激。提前谢谢你。

代码

此错误的最小代码是:

from mpl_toolkits.mplot3d import Axes3D
import numpy, matplotlib, matplotlib.pyplot as pyplot

matplotlib.rcParams['text.usetex'] = True

Module_Colour = '#F0AE1E'

fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')

X_arr = numpy.array([1.0,0.0,0.0])
Y_arr = numpy.array([0.0,1.0,0.0])
Z_arr = numpy.array([0.0,0.0,1.0])
O_arr = numpy.array([0.0,0.0,0.0])

pyplot.quiver(O_arr,O_arr,O_arr,X_arr,Y_arr,Z_arr,
              pivot='tail', length=1.0, linewidth=2.5,
              color = Module_Colour)

pyplot.savefig('C:/Users/alexd/Documents/University/Physics/Physics Figures/fig.jpeg', bbox_inches='tight')
pyplot.show()

请注意 pyplot.show() 阻止 直到您关闭 window。

如果您要为 LaTeX 文档生成绘图,只需删除 pyplot.show()

确保 Python 实际上可以 找到 latexdvipnggs。 (如果不是这种情况,我希望您会收到一条错误消息,但我没有在 ms-windows 上使用 python/matplotlib,所以我不确定。)也就是说,它们的位置应该是在你的 PATH 环境变量中。参见 matplotlib documentation on environment variables.

在交互式 Python 会话中尝试以下操作:

>>> import subprocess
>>> subprocess.call(['latex', '--version'])
pdfTeX 3.14159265-2.6-1.40.17 (TeX Live 2016)
kpathsea version 6.2.2
Copyright 2016 Han The Thanh (pdfTeX) et al.
There is NO warranty.  Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the pdfTeX source.
Primary author of pdfTeX: Han The Thanh (pdfTeX) et al.
Compiled with libpng 1.6.21; using libpng 1.6.21
Compiled with zlib 1.2.8; using zlib 1.2.8
Compiled with xpdf version 3.04
0

有关 TeX 版本的内容可能会有所不同;这取决于您使用的是哪个版本的 TeX 发行版。 最重要是最后一行;这是subprocess.call的return值,应该为0,说明命令return没有错误。

如果 subprocess.call 引发异常(如下所示;不确定 ms-windows 是否相同),您需要修改 PATH 以便 python 可以找到 LaTeX 和它需要的其他东西。

>>> subprocess.call(['foo', '--version'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/subprocess.py", line 168, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/local/lib/python2.7/subprocess.py", line 390, in __init__
    errread, errwrite)
  File "/usr/local/lib/python2.7/subprocess.py", line 1024, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

编辑: 如果这不是问题,那可能是 TeX 没有退出。尝试 运行 脚本。当您看到错误对话框时,查看任务管理器是否还有 (la)tex 进程 运行。查看您使用的 LaTeX 代码,并在 LaTeX 中尝试 运行 它以检查它是否确实有效。它可能会因错误而挂起...