【问题标题】:How do I display LaTeX braces in a f-string in Matplotlib如何在 Matplotlib 的 f 字符串中显示 LaTeX 大括号
【发布时间】:2020-12-12 08:17:49
【问题描述】:

当我使用 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 大括号?

【问题讨论】:

  • 如果它是相关的,(a)我也无法让settings for font name 产生任何效果,并且(b)我正在使用%matplotlib widget

标签: matplotlib latex f-string


【解决方案1】:

让我们看看你的 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 \\}$'
>>> 

重新改字体,因为你在评论里碰了,你要改数学字体,请看this answer

【讨论】:

  • 哇,我没想到把逃生放在那里;在每对大括号之前!
  • 现在我只需要一个字体补丁(用于 STIX sans)用于图形的非 LaTeX 部分(例如刻度标签)。
猜你喜欢
  • 2020-05-25
  • 2022-11-18
  • 2017-07-20
  • 1970-01-01
  • 2020-12-31
  • 1970-01-01
  • 2023-01-29
  • 2022-01-20
相关资源
最近更新 更多