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). It's a simple equation involving an uppercase letter, in bold italics, with font 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.
CodePudding user response:
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
>>> plt.rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
>>> plt.text(0,0,text.upper(), fontweight='bold', fontstyle='italic')
CodePudding user response:
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)
