如何在 Matplotlib 中的 f 字符串中显示 LaTeX 大括号

How do I display LaTeX braces in a f-string in Matplotlib

当我使用 f 字符串创建标签时,我无法让 LaTeX 大括号显示在我的 Matplotlib 图形中。例如

fig, ax = plt.subplots(1, 3, figsize=(12,4), sharey=True)
fig.suptitle("$x_n = x_{n-1}^2$, " + f"$x_0={5:.2f}, \,r={6:.2f}, \,n \in {{ 0,\ldots,{7} }}$")

结果

如何在 Matplotlib 的 f 字符串中显示 LaTeX 大括号?

让我们看看你的f-string

的价值是多少
>>> f"$x_0={5:.2f}, \,r={6:.2f}, \,n \in {{ 0,\ldots,{7} }}$"
'$x_0=5.00, \,r=6.00, \,n \in { 0,\ldots,7 }$'
>>> 

哦,好吧,但是这是传递给 LaTeX 的!在 LaTeX 中,大括号是分隔组的活动字符,要在格式化方程式中使用 LITERAL 大括号,您需要引用大括号

>>> f"$x_0={5:.2f}, \,r={6:.2f}, \,n \in \{{ 0,\ldots,{7} \}}$"
'$x_0=5.00, \,r=6.00, \,n \in \{ 0,\ldots,7 \}$'
>>> 

重新更改字体,如您在评论中提到的,您必须更改MATH字体,请参阅