简体   繁体   中英

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). 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.

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')

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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