使用latex格式的Python断开字符串

2024-04-26 01:23:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个方程式,我需要在图上展示。这个方程式的公式很长,所以我想把它拆分成一行不超过80个字符。在

以下是MWE:

import matplotlib.pyplot as plt

num = 25

text = (r"$RE_{%d}$ = $\frac{\left(\sum^{l_k}{q_m} -\sum^{n}{f_h}\right) - |B_t - f_h+f_g)|}{B}$") % num

fig = plt.figure()
ax = fig.add_subplot(111)

plt.scatter([0., 1.], [0., 1.])
plt.text(0.5, 0.5, text, transform=ax.transAxes)

plt.show()

如果我这样写的话,绘图是没有问题的,但是当我试图打破text行时,我会得到各种各样的错误。在

我该怎么做?在


Tags: textimportrematplotlibasfigpltax
1条回答
网友
1楼 · 发布于 2024-04-26 01:23:12

一种简单的可能性是只使用串联:

text = (r"$RE_{%d}$ = $\frac{\left(\sum^{l_k}{q_m} -\sum^{n}{f_h}\right) " +
        r" - |B_t - f_h+f_g)|}{B}$") % num

也可以使用三重引号的多行字符串,但需要手动替换换行符:

^{pr2}$

相关问题 更多 >