As part of a project I'm working on, I need to have both bold text in Hebrew and mathtext equations inside a Matplotlib text artist. But, I need the mathtext equations to be rendered using the 'cm'
(computer modern) fontset, and the hebrew text to be rendered as actual bold Hebrew text and not dummy symbols.
I tried using custom matplotlib fonts and the mathtext.fallback_to_cm
flag to try and trick the renderer into rendering Hebrew with my font and everything else with cm, but it didn't seem to work, and even if it did would be very unportable and messy (creating a custom Arial font but with only Hebrew characters just to make a few sentences bold). Also, the documentations seems to suggest the custom mode is "unsupported and may go away in the future"
Here's some code to demonstrate the problem:
import matplotlib.pyplot as plt
plt.rcParams['text.latex.preamble'] = [r'\usepackage{lmodern}']
#plt.rcParams['mathtext.fontset'] = 'cm' #disable/enable this line!!
plt.text(0.1, 0.5, "equation: $\\sum_i2^3$. bold text: $\\bf{א}$", fontsize=20)
plt.show()
Enabling or disabling the commented - out line will alternate between having regular Hebrew letters and having normal mathtext equations.
some resources I used to look for solutions:
the mathtext Matplotlib guide (see "fonts")
rcparams options (for font customization, ctrl+F "mathtext")
Stackoverflow answer with explanation on how to have only some of the text bold
Edit: Computer modern does not have Hebrew Characters in its fonts. However, these characters don't need to be a computer modern font, it just seems like a limitation on Matplotlib's side which I hope can be bypassed in a way better than creating a new font family.