我需要在绘图的文本框中以及在LaTeX环境中(因为我还需要编写一些数学代码)编写包含下划线的变量的名称。
问题是LaTeX将变量名称中的下划线解释为子索引命令,并且变量名称失真。请参阅(下面的MWE):
变量的名称是m_out
。
如何在不将LaTeX解释为subindex命令的情况下编写包含下划线的字符串?
在纯LaTeX中,我可以使用以下\textunderscore
命令来编写:
N = m \textunderscore out \pm 0.2
正确产生:
但这似乎在这里不起作用。
威斯康星州
import matplotlib.pyplot as plt
import matplotlib.offsetbox as offsetbox
import random
# Generate random data.
x = [random.random() for i in xrange(10)]
y = [random.random() for i in xrange(10)]
# Define string with underscore.
name = 'm_out'
# Create plot.
fig = plt.figure()
ax = plt.subplot()
# Add text box
text = r'$N={}\pm0.2$'.format(name)
ob = offsetbox.AnchoredText(text, loc=1, prop=dict(size=12))
ax.add_artist(ob)
plt.scatter(x, y)
# Save plot to file.
fig.tight_layout()
plt.savefig('out.png')
一个简单的转义就可以解决问题:
name = 'm\_out'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句