Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Slightly better positioning of subscripts and superscripts of fractions#21850

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
tfpf wants to merge3 commits intomatplotlib:mainfromtfpf:subsuper

Conversation

tfpf
Copy link
Contributor

@tfpftfpf commentedDec 3, 2021
edited
Loading

PR Summary

Tries to improve the positioning of sub- and superscripts attached to fractions (fix#18086).

PR Checklist

Tests and Styling

  • Has pytest style unit tests (andpytest passes).
  • IsFlake 8 compliant (installflake8-docstrings and runflake8 --docstring-convention=all).

Documentation

  • [N/A] New features are documented, with examples if plot related.
  • [N/A] New features have an entry indoc/users/next_whats_new/ (follow instructions in README.rst there).
  • [N/A] API changes documented indoc/api/next_api_changes/ (follow instructions in README.rst there).
  • [N/A] Documentation is sphinx and numpydoc compliant (the docs shouldbuild without error).

Details

import matplotlib.pyplot as plts = r'$\left(\dfrac{n}{d}\right)_p^q$ $\left(\dfrac{n}{d}\right)^q$ $\left(\dfrac{n}{d}\right)_p$'fig = plt.figure()fig.text(0.1, 0.5, s)plt.show()

Before

before

After

after

Notes

I added a new production rule for the tokenisation of expressions containing subscripts and superscripts.

        p.subsuper      <<= Group(            (Optional(p.placeable)             + OneOrMore(p.subsuperop - p.placeable)             + Optional(p.apostrophe))            | (p.placeable + Optional(p.apostrophe))            | p.apostrophe            | (p.auto_delim + OneOrMore(p.subsuperop - p.placeable))        )

This should be considered suboptimal, but with my extremely limited knowledge of languages, grammars and parsing, this is the best idea I could come up with. This addition has the effect of making the nucleus (i.e. the expression to which a subscript and/or superscript are/is attached) available for the calculation ofshift_up andshift_down.

I hate magic constants (0.4 and1.4) as much as the next person, but can't think of anything better to differentiate nuclei which are and aren't fractions. This works for Dejavu Sans, but not for Computer Modern or STIX or Dejavu Serif. So, I'll leave this as a draft. Would appreciate experts weighing in.

LaTeX uses a wide assortment of font parameters (stored infont_info, an array) to calculateshift_up andshift_down, so the results of this will not exactly match what LaTeX gives us, but it will be close. Nevertheless, this will have to be tested extensively.

Needless to say, sub- and superscripts can be nested.

import matplotlib.pyplot as plts = r'$\left(\dfrac{\mathrm{Numerator}}{\mathrm{Denominator}}\right)_{y_{i^2}}^{\frac{y_k^{2^w}}{4^3}}$'fig = plt.figure()fig.text(0.1, 0.5, s, size=50)plt.show()

nesting

Another point to note is that this will be affected by#20627, so a rebase may be required later.

@QuLogicQuLogic added the status: needs workflow approvalFor PRs from new contributors, from which GitHub blocks workflows by default. labelDec 7, 2021
@tfpf
Copy link
ContributorAuthor

tfpf commentedDec 7, 2021

There are a total of 14 mathtext image miscompares, all due totest_mathtext_rendering[*-mathtext-*-81] i.e. the tests for\left(X\right)_a^b.

The reason is thatcm andstixsans do not expand brackets the waystix,dejavusans anddejavuserif do.
typeface-irregularities
The brackets are not high enough in case ofstixsans, and not low enough in case ofcm. So, the shifts calculated for one font may not look good when used on another.

I used shifts which (I hope) would make fractions more closely resemble what LaTeX does. I think the result is reasonable.
typeface-irregularities-2

Would love to know what experts think.

@tfpftfpf marked this pull request as ready for reviewDecember 16, 2021 16:24
@jklymak
Copy link
Member

This looks like really good work, bt I feel un-able to properly review it. Who is our resident expert in mathtext?

Copy link
Member

@QuLogicQuLogic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Were you able to check this against the texbook that@anntzer suggested in the issue? I think sub/superscripts are defined around program 756 on page 280 there. This might be difficult to use though if the nucleus is not accessible.

@tfpftfpf marked this pull request as draftMay 6, 2022 16:49
No point running the tests if we are absolutely sure they are going to fail.
@tfpf
Copy link
ContributorAuthor

I thought I'd revive this, since I now have a better understanding of TeX's algorithm. I implemented the calculations described in the book (with a few shortcuts). However, the CM font behaves weirdly: it reports a wrong x-height, which throws off the calculations.

import matplotlib.pyplot as plt(fig, ax) = plt.subplots()ax.text(.1, .5, r'$\mathrm{x}\alpha_{i+1}^j$', size=50)plt.rc('mathtext', fontset='cm')ax.text(.5, .5, r'$\mathrm{x}\alpha_{i+1}^j$', size=50)plt.show()

DejaVu Sans correctly reports the x-height as 38 (I verified this by counting the pixels 😅), whereas CM reports it as 59, which is clearly wrong, as this image shows.
wrong-_x-height
(The icons are missing because I am using a WSL installation of Matplotlib on Linux.)

Which means that even though the algorithm is correct, the alignment is incorrect for CM. Should we just shelve this?

@tfpf
Copy link
ContributorAuthor

I found the problem. DejaVu Sans does not store the x-height, so it is actually calculated in theget_xheight function. Whereas CM does, but the value it provides is incorrect.

If I force the "poor man's x-height" calculation for CM, the result is reasonably good.
poor_mans_x-height

However, making that change on themain branch breaks 126 Mathtext tests (incidentally, all of them are CM rendering tests).

@tfpf
Copy link
ContributorAuthor

tfpf commentedJun 11, 2022
edited
Loading

Shortcuts I took for the algorithm

  • The default values ofshift_up andshift_down depend on two font constants,supdrop andsubdrop, respectively. However, Mathtext usessubdrop for both (as the comment in theFontConstantsBase class mentions), so I did the same.
  • The actual value ofshift_up depends on the current style (display, text, script or script-script), and may be calculated usingsup1,sup2 orsup3. However, superscripts and subscripts in Mathtext do not know about the current style (Mathtext only has thesup1 constant); they are simply one size smaller than the nucleus. I've also used onlysup1.

@tfpf
Copy link
ContributorAuthor

Closing. Superseded by#22852.

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

@QuLogicQuLogicQuLogic left review comments

@anntzeranntzeranntzer left review comments

Assignees
No one assigned
Labels
status: needs workflow approvalFor PRs from new contributors, from which GitHub blocks workflows by default.topic: text/mathtext
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

sub/superscripts should be moved further from the baseline following large delimiters
5 participants
@tfpf@jklymak@QuLogic@anntzer@dstansby

[8]ページ先頭

©2009-2025 Movatter.jp