6

When I give matplotlib a annotation string such as

'$\frac{A}{B} = C$'

and I specify a fontsize of 18, the A and B are rendered at 12.6 pt, while the C is rendered at 18 pt. I want A, B, and C to all be the same size. How do I do this?

In a LaTeX document, if you give the commands

\begin{equation}
\frac{A}{B} = C
\end{equation}

you get a fraction, where A, B, and C are all the same size, but if you do

$\frac{A}{B} = C$

inline with text, you get the A and B rendered at 12.6 pt, while the C is rendered at 18 pt. Thus it appears matplotlib's mathtext is emulating LaTeX's inline mode. In LaTeX you can write

$\displaystyle\frac{A}{B} = C$

and then A, B, and C are all the same size, even in inline mode. I tried this in matplotlib, but mathtext did not recognize the command \displaystyle. =(

Is there a way to get this to work in Matplotlib's mathtext, or am I stuck changing text.usetex to true in my .matplotlibrc file? (If possible I would like to stay with mathtext since it is a lot faster.)

My setup: matplotlib v1.2.0 python 2.7 OS X 10.8

Stretch
  • 1,581
  • 3
  • 17
  • 31
  • Perhaps see if some ideas in [Setting math font size](http://tex.stackexchange.com/q/94232/5764) solves your problem. – Werner Apr 08 '13 at 18:30
  • @Werner Thanks for the link, but I did not find anything that worked. The \Large, \Huge, \small, etc commands only work for text or equation mode. They do not work in inline math mode ($ ... $), which is the only mode for mathtext. The rest of the discussion in the link seems to pertain only to Matlab. Perhaps I just need to ask the matplotlib developers if they can implement \displaystyle into mathtext. – Stretch Apr 09 '13 at 03:28
  • Do that, report back and share with the community. – Werner Apr 09 '13 at 03:50
  • @Werner: Ok. I submitted a feature request at https://github.com/matplotlib/matplotlib/issues/1888 – Stretch Apr 09 '13 at 12:32

2 Answers2

6

For anyone stumbling on this and also not wanting to set "text.usetex" to True, matplotlib now supports the \dfrac macro (equivalent to \displaystyle\frac in LaTeX) to replace \frac since version 2.1

Silmathoron
  • 1,781
  • 1
  • 15
  • 32
  • That's awesome news. The funny thing is I am friends with the guy who implemented \dfrac, yet I did not know he did it. – Stretch Feb 21 '19 at 02:35
4

As you said, you can fix it by using \displaystyle:

$\displaystyle\frac{A}{B} = C$

In order to allow matplotlib to use latex for all text handling you have to define in your matplotlibrc the text.usetex variable as True:

text.usetex          : True

I made a little example to verify it and it is working well:

import matplotlib.pyplot as plt
plt.plot(range(200))
plt.text(100,50,r'$\displaystyle\frac{A}{B}=C$')

I am sorry but since I am new, I can not post any image.

Alejandro
  • 3,263
  • 2
  • 22
  • 38
  • Thanks for your comment. As I mention at the bottom of my post, I would rather not use latex for all text handling. It is quite slow. I want to use mathtext. – Stretch Apr 25 '13 at 03:04