当我使用LaTeX渲染文本时,为什么python没有响应? [英] Why does python become unresponsive when I use LaTeX to render text?

查看:119
本文介绍了当我使用LaTeX渲染文本时,为什么python没有响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently using python 2.7.11 running my code in command prompt. I am using matplotlib to create 3D figures for my LaTeX document. However when I try to use LaTeX to render my text for the images I receive a windows error message telling me python.exe has stopped working and I must close the program. Why is this happening and how can I fix it? I'm not tec savvy, so simple answers would be appreciated. Thank you in advance.

代码

此错误的最小代码为:

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() 会阻止,直到关闭窗口。

Note that pyplot.show() blocks until you close the window.

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

If you are generating plots for your LaTeX documents, just remove pyplot.show().

请确保Python实际上可以找到 乳胶 dvipng gs 。 (如果不是这种情况,我希望您会收到一条错误消息,但是我没有在ms-windows上使用python / matplotlib,所以我不确定。)也就是说,它们的位置应该在您的<$中c $ c> PATH 环境变量。请参阅有关环境变量的matplotlib文档

Make sure that Python can actually find latex, dvipng and gs. (I would expect you to get a error message if that is not the case, but I haven't used python/matplotlib on ms-windows, so I'm not sure.) That is, their location should be in your PATH environment variable. See the matplotlib documentation on environment variables.

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

Try the following in an interactive Python session:

>>> 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 应该的返回值为0,表示该命令没有返回错误。

The stuff about the TeX version might well be different; it depends on which version of which TeX distribution you're using. Most important is the last line; this is the return value of subprocess.call and should be 0, indicating that the command return no error.

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

If the subprocess.call raises an exception (like below; not sure if it will be the same on ms-windows), you need to modify the PATH so that python can find LaTeX and the other things it needs.

>>> 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中运行它以检查其是否有效。它可能会挂在错误上...

If this is not the problem, it could be that is it TeX that is not quitting. Try running the script. When you get the error dialog, look in the task manager if there is still a (la)tex process running. Look at the LaTeX code that you use, and try running it in LaTeX to check that it is actually valid. It might get hung on an error...

这篇关于当我使用LaTeX渲染文本时,为什么python没有响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆