PGF / LaTeX后端Matplotlib通过Jupyter Notebook SLURM Job在HPC系统上.&



我是一名使用学校计算集群的大学生。

我将Tex Live安装到我的主目录~/.local/texlive/。我有一个文件叫mplrc。将MATPLOTLIBRC环境变量设置为mplrc文件。mplrc文件包含以下行

backend:            pgf  
pgf.rcfonts:        false  
pgf.texsystem:      pdflatex 
pgf.preamble:       input{mpl_settings.tex} 
text.usetex:        true
font.family:        serif
font.size:          12

mpl_settings.tex文件与mplrc文件位于同一目录,包含以下内容

usepackage{amsmath}
usepackage[T1]{fontenc}
usepackage{gensymb}
usepackage{lmodern}
usepackage{siunitx}

在我正在使用的集群上,我必须提交一个SLURM作业来运行Jupyter笔记本。我试图在笔记本中运行的示例代码是

formula = (
r'$displaystyle '
r'N = int_{E_text{min}}^{E_text{max}} '
r'int_0^A'
r'int_{t_text{min}}^{t_text{max}} '
r'Phi_0 left(frac{E}{SI{1}{GeV}}right)^{!!-γ}'
r' , symup{d}A , symup{d}t , symup{d}E'
r'$'
)

def power_law_spectrum(energy, normalisation, spectral_index):
return normalisation * energy**(-spectral_index)

bin_edges = np.logspace(2, 5, 15)
bin_centers = 0.5 * (bin_edges[:-1] + bin_edges[1:])
y = power_law_spectrum(bin_centers, 1e-5, 2.5)
relative_error = np.random.normal(1, 0.2, size=len(y))
y_with_err = relative_error * y
fig, ax = plt.subplots()
ax.errorbar(
np.log10(bin_centers),
y_with_err,
xerr=[
np.log10(bin_centers) - np.log10(bin_edges[:-1]),
np.log10(bin_edges[1:]) - np.log10(bin_centers)
],
yerr=0.5 * y_with_err,
linestyle='',
)
ax.text(0.1, 0.1, formula, transform=plt.gca().transAxes)
ax.set_yscale('log')
fig.tight_layout(pad=0)
plt.show()

这将生成一个巨大的错误消息,但是它的根是

RuntimeError: latex was not able to process the following string:
b'lp'

然而,在这背后,我看到了我认为真正的问题

! LaTeX Error: File `article.cls' not found.

我已经设置了我的PATH,以便它找到正确的latex命令,但是为了找到article.cls文件,还需要设置什么?这似乎是Python笔记本特有的东西。当在Jupyterlab界面的终端中运行kpsewhich article.cls时,将找到该文件。但是在Python笔记本中尝试! kpsewhich article.clssubprocess.run(['kpsewhich', 'article.cls'])无法找到该文件。

我明白了。我忘了我已经运行了一段设置

的代码
TEXINPUTS=/path/to/some/directory

看起来我在TEXINPUTS中错过了:,所以TeX在/path/to/some/directory中只查找

解决方案是使用

TEXINPUTS=/path/to/some/directory:

这样在我当前目录中查找,但也继续在其他地方查找。

最新更新