Python在原始字符串中转义反斜杠

3

我想在matplotlib图表中使用latex。到目前为止,这个想法是可行的 - 但是现在反斜杠会使事情变得困难。

以下是我的尝试:

r'$a_{{\text{{test}}}}$ ({})'.format('foo')
Out[13]: '$a_{\\text{test}}$ (foo)'

但是我期望的输出是

'$a_{\text{test}}$ (foo)'

我尝试了很多方法来解决这个问题,但都没有成功:在原始环境中转义反斜杠自然会创建4个反斜杠。如何获得预期的单个反斜杠?


基本上无法正常运行的是在matplotlib中绘制此图。

import matplotlib.pyplot as plt
plt.plot(np.array([1, 1]), label=r'$a_{{\text{{test}}}}$ ({})'.format('foo'))
plt.legend()

这会导致如下错误:
RuntimeError: latex was not able to process the following string:
b'$a_{\\\\text{test}}$ (foo)'
Here is the full report generated by latex:
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017/Debian) (preloaded format=latex)
 restricted \write18 enabled.
entering extended mode
(/home/saman/.cache/matplotlib/tex.cache/3d4d47cda002ea7b54e89aca2a4b3fae.tex
LaTeX2e <2017-04-15>
Babel <3.18> and hyphenation patterns for 84 language(s) loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/type1cm/type1cm.sty)
(/usr/share/texlive/texmf-dist/tex/latex/psnfss/helvet.sty
(/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty))
(/usr/share/texlive/texmf-dist/tex/latex/base/textcomp.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1enc.def))
(/usr/share/texlive/texmf-dist/tex/latex/geometry/geometry.sty
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifvtex.sty)
(/usr/share/texlive/texmf-dist/tex/generic/ifxetex/ifxetex.sty)
Package geometry Warning: Over-specification in `h'-direction.
    `width' (5058.9pt) is ignored.
Package geometry Warning: Over-specification in `v'-direction.
    `height' (5058.9pt) is ignored.
)
No file 3d4d47cda002ea7b54e89aca2a4b3fae.aux.
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1cmr.fd)
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
(/usr/share/texlive/texmf-dist/tex/latex/psnfss/ot1phv.fd)
! Undefined control sequence.
l.13 ...{10.000000}{12.500000}{\sffamily $a_{\text
                                                  {test}}$ (foo)}
No pages of output.
Transcript written on 3d4d47cda002ea7b54e89aca2a4b3fae.log.

你得到的错误是 \text 未定义,实际上,它确实未定义(你没有调用 amsmath)。也许你想要使用 \mathrm 或者 \textnormal。参见 https://tex.stackexchange.com/q/98406/95042 - torek
@torek 的确,那就是答案,而且值得被表述为一个答案 :) - FooBar
2个回答

0

一旦您的格式不是原始的,输出就可以了。 尝试将其存储到变量中,然后打印出来。

s = r"\{}"
print(s)
res = s.format("foo")
print(res)

另外,您可以使用 r'string'.decode('string_escape') - CTheCheese
我已经更新了问题,因为您建议输出结果是正确的:总的来说,matplotlib不喜欢这个字符串,所以我尝试以这种方式进行调试。 - FooBar
@FooBar,LaTeX中有问题。你注意到输出了吗?l.13 ...{10.000000}{12.500000}{\sffamily $a_{\text{test}}$ (foo)},这正确吗? - Netwave

0

你只有一个反斜杠。我不知道你是如何查看字符串的,但输出结果并不是原始字符串表示法,这就是为什么你看到了\\。但那只是为了演示目的。例如,输出还显示单引号('...'),它们实际上也不是你字符串的一部分。

尝试使用print(r'$a_{{\text{{test}}}}$ ({})'.format('foo'))


我已经更新了问题,因为您建议输出结果是正确的:总的来说,matplotlib不喜欢这个字符串,所以我尝试以这种方式进行调试。 - FooBar
@FooBar LaTeX 错误“l.13 ...{10.000000}{12.500000}{\sffamily $a_{\text {test}}$ (foo)}”表明它实际上是 \text{test}。还有其他问题(例如,\text{...} 是否实际上是有效的 LaTeX 代码?)。 - melpomene
除了Python之外,$a_{\text{test}}$ (foo) 直接在LaTeX中编译正常。您建议我开一个新的问题专注于Matplotlib吗? - FooBar
@FooBar 那样做是有意义的,对。 - melpomene

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接