Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature request: mathtext support for \displaystyle #1888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
breedlun opened this issue Apr 9, 2013 · 12 comments
Closed

Feature request: mathtext support for \displaystyle #1888

breedlun opened this issue Apr 9, 2013 · 12 comments

Comments

@breedlun
Copy link
Contributor

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

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

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
Copy link
Member

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

@breedlun
Copy link
Contributor Author

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.

@breedlun
Copy link
Contributor Author

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
Copy link
Member

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):

http://www.amazon.com/Computers-Typesetting-Volume-TeX-Program/dp/0201134373/ref=sr_1_2?ie=UTF8&qid=1380049773&sr=8-2&keywords=tex%3A+the+program

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).

@breedlun
Copy link
Contributor Author

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
Copy link
Member

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 this to the v1.5.x milestone Aug 17, 2014
@petehuang
Copy link
Contributor

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

@breedlun
Copy link
Contributor Author

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.

@watkinrt
Copy link
Contributor

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
Copy link
Contributor Author

breedlun commented Jan 26, 2017 via email

@watkinrt
Copy link
Contributor

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.

QuLogic pushed a commit that referenced this issue Jul 26, 2017
)

* Feature addition: I added in the the \dfrac macro (displaystyle fraction) to the mathtex implementation.

* PEP8 fixes

* Documentation: Updated the docstring for the dfrac_demo
Bug fix: I added a plt.show() to the end of the dfrac_demo
Tests: I added in a new test for the dfrac macro (along with the corresponding comparison files) within the test_mathtex test suite.

* Documentation update: I updated the dfrac_demo docstring to be a raw string with the backslashes escaped.
Feature modification: I added in a dictionary to the Parser class of mathtex to link math style names to their TeX numerical values. I used a dictionary instead of hardcoding specific math style (ex. DISPLAYSTYLE=0) as I will use the dictionary in my implementation of the math styles in the future.

* Although Sphyinx requires "\\" to show "\" characters, I removed them as matplotlib doesn't currently support this behavior in docstrings.

* Remove stray text from documentation.

* With the resent merging of the sphinx-gallery branch, I went through and updated the docstring to appropriately escape the backslashes.

* TST: add missing tests
@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Oct 3, 2017
@anntzer
Copy link
Contributor

anntzer commented Aug 8, 2019

#8151 implemented \dfrac, which covers the initial request. That doesn't cover all of \displaystyle, but I'll close this until there are other concrete points to implement.

@anntzer anntzer closed this as completed Aug 8, 2019
@story645 story645 removed this from the future releases milestone Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants