I am creating many figures for my LaTeX document using matplotlib with 'text.usetex': True.

My full preamble looks like this:

rc('font', **{'family':'serif', 'serif':['Computer Modern Roman']})
params = {'backend': 'pdf',
          'axes.labelsize': 12,
          'text.fontsize': 12,
          'legend.fontsize': 12,
          'xtick.labelsize': 10,
          'ytick.labelsize': 10,
          'text.usetex': True,
          'figure.figsize': fig_size,
          'axes.unicode_minus': True}

In the plot I also use \texttt{}, which should match the formatting in the latex document. However it doesnt (compare legend and caption, both 12pt).

Matplotlib figure and caption. Typewriter text is rendered differently.

I assume I have to add something like 'family':'typewriter', 'typewriter':['Some font']. So my questions:

  • how can I find out which font and weight is used in the LaTeX document for \texttt
  • How to tell matplotlib to use the exact same font

Matplotlib apparently uses Courier as default, my LaTeX uses Computer Modern Typewriter.

I found some font specification examples here.

So changing the first line of my preamble to

rc('font', **{'family':'serif', 'serif':['Computer Modern Roman'], 
                                'monospace': ['Computer Modern Typewriter']})

renders the \texttt{} the same way LaTeX does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.