I'm using matplotlib to prepare figures for a LaTeX document. I use TeX-mode in matplotlib. But subscript and superscipt in math mode gets a different size than in my LaTeX document.

Rendered with matplotlib: Rendered with matplotlib

Rendered with LaTeX: Rendered with LaTeX

Sorry for the slightly different size, but I think it is clear that the mean has a different relative size to the d.

Below is my matplotlib settings I use:

plt.rc('font', **{'family':'serif', 'serif':['Computer Modern Roman'],
                  'monospace':['Computer Modern Typewriter']})
params = {'backend': 'ps',
          'text.latex.preamble': [r"\usepackage{upgreek}",
          'axes.labelsize': 12,
          'text.fontsize': 12,
          'legend.fontsize': 8,
          'xtick.labelsize': 10,
          'ytick.labelsize': 10,
          'text.usetex': True,
          'figure.figsize': fig_size,
          'axes.unicode_minus': True}

Does anyone know what setting I have to add? Is there anything from my .cls file that I have to input into the rcParams?

  • 1
    how are you entering the "mean"? if you use amsmath, or more specifically the subpackage amstext, you can in ordinary latex input \text{mean} and it will be set at the appropriate size for the surrounding environment. i don't know how matplotlib works, but maybe this is applicable. – barbara beeton Sep 6 '12 at 12:36
  • I am using $d_\textnormal{mean}^{100}$. \textnormal because it will change to sans serif (if you use sans serif font), while \text{} doesn't. But I will try if this has any influence. – user334287 Sep 6 '12 at 13:26
  • hmmm. \text is supposed to "match" the surrounding font style, as well as the size. i'll put this on the list of things to look into when amsmath is next opened up for an overhaul. in the meantime, try inserting \scriptsize in the subscript expression, and definitely wrap the entire subscript expression in braces; i'm surprised that "mean" actually came out as a subscript without this protection. – barbara beeton Sep 6 '12 at 14:07
  • update: in a text environment (i'm not able to test matplotlib), $d_{\text{mean}}^{100}$ comes out with "mean" in sans in a sans environment (\sffamily) and italic in an \itshape` environment, while \textnormal consistently comes out roman. but do observe the extra braces around the subscript; even though they may not be strictly required (to my surprise), always including them around anything that's more than one letter or digit is good practice. – barbara beeton Sep 6 '12 at 14:31
  • I added r"\usepackage{amstext}" to the preamble and now it comes out in the right size with $d_\text{mean}$, $d_{\text{mean}}$ and the same with \textnormal. I saw that my .cls contains this: \usepackage{amsmath,amsfonts,amssymb,amscd,amsthm,xspace}. Is there anything that loads amstext? Conclusion: Adding amstext to the preamble solves the original question. Edit: "The amsmath package incorporates amstext, amsopn, and amsbsy." from User's guide for the amsmath Package V 2.0. – user334287 Sep 6 '12 at 14:44

adding amstext to the complement of packages used for matplotlib will make available the \text{...} command, which matches the surrounding text style and also uses the proper size in various parts of math expressions. amstext is incorporated in amsmath, but the latter is probably overkill in this case.

coding of the questioned expression would then best be done like this:


edit: here is a small test of \textnormal and \text in a simple display.


  d_\textnormal{mean}^{100} \quad d_{\textnormal{mean}}^{100}
  \quad d_\text{mean}^{100} \quad d_{\text{mean}}^{100}

Here we test \verb+\textnormal+ and \verb+\text+ in a subscript to see
how they affect the style as the surrounding text style changes.
some more text, then
\sffamily switch to sans
switch back to
\rmfamily roman and then to
\itshape italic
that's all.

and its output:

output of example code

  • Is \textnormal{} superseeded by amsmath's \text{}? I was following this blog post stefaanlippens.net/textnormal. – user334287 Sep 6 '12 at 16:22
  • @user334287 -- i won't argue with the post you've cited, but i've edited my answer to include an example that shows different behavior of \text and \textnormal. – barbara beeton Sep 6 '12 at 16:54

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.