简体   繁体   English

如何使用 LaTeX 写大写粗斜体字母 matplotlib?

[英]How to write uppercase bold italic letters matplotlib using LaTeX?

I want to have the text shown in the image 1 in a plot (this is taken from Rubidium 87 D Line Data - Daniel A. Steck, page 23).我想在 plot 中显示图像1中显示的文本(取自 Rubidium 87 D Line Data - Daniel A. Steck,第 23 页)。 It's a simple equation involving an uppercase letter, in bold italics, with font Computer Modern .这是一个简单的等式,涉及一个大写字母,粗斜体,字体 Computer Modern So far I've tried到目前为止我已经尝试过

import matplotlib.pyplot as plt
plt.rcParams["text.usetex"] = True
plt.rcParams["text.latex.preamble"].join([
    r"\usepackage{bm}",
    r"\usepackage{amsmath}"])


fig, ax = plt.subplots()

# Not bold
ax.text(0, 0.5, r"$F=0$", fontsize=18)

# Not italic
ax.text(0.2, 0.5, r"$\mathbf{F=0}$", fontsize=18)

# Not italic, not the correct font
ax.text(0.4, 0.5, r"\textit{\textbf{F}}=0", fontsize=18)

# Returns an error from LaTeX
ax.text(0.6, 0.5, r"$\bm{F}=0$", fontsize=18)

# Returns an error from LaTeX
ax.text(0.8, 0.5, r"$\boldsymbol{F}=0$", fontsize=18)

I'm using pdfTeX, Version 3.141592653-2.6-1.40.23 (TeX Live 2021/W32TeX) to display the equations.我正在使用 pdfTeX,版本 3.141592653-2.6-1.40.23 (TeX Live 2021/W32TeX) 来显示方程。

It doesn't seem like you want to achieve anything TeX specific, so you can slip away with doing it all by tweaking your rcParams and the rest when calling plt.text您似乎不想实现任何特定于 TeX 的内容,因此您可以在调用plt.text时通过调整 rcParams 和 rest 来完成所有操作

>>> plt.rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
>>> plt.text(0,0,text.upper(), fontweight='bold', fontstyle='italic')

I found a way to make it run.我找到了让它运行的方法。 For some reason the preamble was not set properly.由于某种原因,序言没有正确设置。 The following code did the job:以下代码完成了这项工作:

import matplotlib.pyplot as plt
plt.rcParams["text.usetex"] = True
plt.rcParams["text.latex.preamble"] = r"\usepackage{bm}"

fig, ax = plt.subplots()
ax.text(0.6, 0.5, r"$\bm{F}\:\mathbf{=0}$", fontsize=18)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM