2

I am using the ieeconf latex class to write a document. To generate the plots for the document, I use matplotlib 1.2. For some reason, the ieeconf class uses Times font for regular text and Computer modern roman for math text. If in matplotlib I use the following matplotlibrc file

font.family         : serif
font.serif          : Times
text.usetex         : True

the regular text (non-math) in the plots looks exactly like the regular text in the rest of the document. However, the math text in the plots looks different to the math text in the document. If instead I use font.serif : Computer Modern Roman, then the roles are reversed, and the math texts look identical, but the regular ones look different.

How can I make matplotlib use one font type for the regular text, and another font type for the math text?

1
  • As of version 3.4, you can do this with the math_fontfamily parameter. Commented Apr 5, 2022 at 19:10

2 Answers 2

3

In Matplotlib you can give Latex commands using: rc('text.latex', preamble = [r'']) (sort of like they do it here). Then this may help you out.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't mind the green checkmark next to the answer, but can you post the actual code you used? I feel that my answer is pointing in the right direction, but some actual code would be useful.
1

As of version 3.4, you can do this with the math_fontfamily parameter:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(6, 5))

# A simple plot for the background.
ax.plot(range(11), color="0.9")

# A text mixing normal text and math text.
msg = (r"Normal Text. $Text\ in\ math\ mode:\ "
       r"\int_{0}^{\infty } x^2 dx$")

# Set the text in the plot.
ax.text(1, 7, msg, size=12, math_fontfamily='cm')

# Set another font for the next text.
ax.text(1, 3, msg, size=12, math_fontfamily='dejavuserif')

# *math_fontfamily* can be used in most places where there is text,
# like in the title:
ax.set_title(r"$Title\ in\ math\ mode:\ \int_{0}^{\infty } x^2 dx$",
             math_fontfamily='stixsans', size=14)

# Note that the normal text is not changed by *math_fontfamily*.
plt.show()

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.