Feature request: mathtext support for \displaystyle #1888

breedlun opened this Issue Apr 9, 2013 · 11 comments


None yet

5 participants

breedlun commented Apr 9, 2013

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.

In a LaTeX document, if you give the commands

\frac{A}{B} = C

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. =(

I am aware I can use \displaystyle if I change text.usetex to true in my .matplotlibrc file, but I would like to stay with mathtext since it is a lot faster. Is there a chance of implementing \displaystyle in math text? If not, does anyone have a work around?

My setup: matplotlib v1.2.0, python 2.7, OS X 10.8.3

mdboom commented Apr 9, 2013

I agree it would be nice to have this. mathtext has always been a subset of TeX, just because time is finite, but we gradually add new features on a sort of "as needed" basis. If you want to tackle this yourself, I'd be happy to provide some pointers -- getting yourself a copy of The TeX Reference: Volume B (there are freely available PDFs online) is a good start...


Thanks for your reply. While I definitely have an interest in diving into Matplotlib and LaTeX to add this functionality, I currently lack the time. I am already overextended right now from all the time it has taken me to switch from Matlab to Python. Hopefully I will be able to get out from under my mountain of work to help more in the future.


Ok. The lack of \displaystyle is starting to drive me nuts. I'm not sure how much help I will be, but I thought I should at least try to tackle this. I just looked for "The TeX Reference: Volume B" and found nothing that matched that title. Any other advice you can lend would be much appreciated.

mdboom commented Sep 24, 2013

If you just want it to work, the path of least resistance here is going to be to turn text.usetex on and use a real TeX backend.

However, if you're game on extending matplotlib, the book I meant was (sorry I didn't have the title exactly right):


This is the same text I used to implement the existing mathtext support. You can also build that book from the source code to TeX (I did it so many years ago, I can't remember how, but it's not too difficult on a Linux system with cweb installed).


Yeah, I found that using TeX (via text.usetex) to typeset every figure was too slow for me. I would rather get it done right. Is there a reason you went with \textstyle rather than \displaystyle? I was thinking it might make sense to create an option in matplotlib.rcParams that allows the user to decide which is the default.

I tried to find pdfs of "Computers & Typesetting, Volume B: TeX: The Program" online, but I did not come up with anything. I did find http://www.ctan.org/tex-archive/systems/knuth/dist/tex, but it looks like people are not supposed to typeset the source without Knuth's direct permission.

mdboom commented Sep 25, 2013

Ah -- I didn't see that license restriction earlier. Looks like purchasing or library-borrowing the book is probably the only way to go (and I do own the book, FWIW, and had typeset it myself while it was in transit).

@tacaswell tacaswell added the wishlist label Aug 17, 2014
@tacaswell tacaswell added this to the v1.5.x milestone Aug 17, 2014

This hasn't been updated in a while. I'm wondering about the priority of this issue given that there's a good workaround. I guess it depends on how far we want MPL to go into this territory


I am obviously biased, but this issue still matters to me. I realize it would be a large effort for mathtext to recreate everything in LaTeX, but I think \displaystyle fractions are one feature that deserves to be implemented. I have been using matplotlib intensively for three years now and I have not run into any other shortcoming in mathtext that gets in my way more than this one. Any time I want to normalize a variable by another variable, I have to use a / b instead of \frac{a}{b}.

If no one has made any headway on this issue by this summer, I should have some time to work on it. I am a newbie when it comes to heavy duty TeX, but I am willing to learn.

I just ran into this problem and would be interested in a fix. Like breedlun, I thought I could get over it, but I can't (maybe its because we both had the same thesis adviser who broke us). It appears that my library has Knuth's "METAFONT : the program" book (i.e., Volume C), but sadly not "TeX: The Program". If I were to get the book, how intensive would it be for me to implement this feature? I've meddled around with the underbelly of matplotlib some, so I am familiar with much of its structure. I also have extensive experience with LaTeX and some with TeX.

breedlun commented Jan 26, 2017 edited
watkinrt commented Feb 7, 2017

Just poking around in mathtex.py, I think it should be pretty easy to implement this functionality. It looks like the tex macro \genfrac had been implement, which is the base macro for making things like \frac, \binom, \stackrel, etc; the usage of \genfrac is { \genfrac #1 #2 #3 #4 #5 #6 }, where #1 is left delimiter, #2 is the right delimiter, #3 is the fraction bar thickness, #4 is the style (0: displaystyle, 1: textstyle, 2: scriptstyle, 3: scriptscriptstyle), #5 is the numerator, and #6 is the denominator. It looks like the current implementation of the child macros \frac, \binom, etc all are hard coded to use the \textstyle style (i.e., \displaystyle is ignored). If I have time this weekend, I'll see if I can get the style to depend on the input style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment