Skip to content

mathtext: Finetuning sup/super block to match TeX reference #4873

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

Merged
merged 14 commits into from
Aug 25, 2015

Conversation

zblz
Copy link
Member

@zblz zblz commented Aug 6, 2015

As mentioned in #4872, I found that sub/super blocks in mathtext have slightly too much space after them, even after @mdboom fix in #4401. As initial reference, this a TeX rendering of a bunch of sub/superscripts (with E and 5 because they are box-like and separation differences are easier to spot):
subsup_test_tex

The current master renders it as follows:
subsup_test_mpl_master

And with the modifications of this PR, it looks like this:
subsup_test_mpl

Summary of changes:

  • Additional kerning of -0.1 em after sub/super blocks.
  • Superscripts are shifted away from nucleus by 0.15 em when nucleus is slanted.
  • When there is both sub and superscript and nucleus is slanted, superscripts are only shifted an aditional 0.15 em (down from >0.20 em)
  • Subscripts are shifted down 0.25 x-height instead of 0.5 when there is no superscript.

Regression images coming soon!

@WeatherGod
Copy link
Member

Comparing between the Tex and mathtext, It looks like the superscript is
slightly too high as well.

On Thu, Aug 6, 2015 at 12:44 PM, Victor Zabalza notifications@github.com
wrote:

As mentioned in #4872 #4872,
I found that sub/super blocks in mathtext have slightly too much space
after them, even after @mdboom https://github.com/mdboom fix in #4401
#4401. As initial
reference, this a TeX rendering of a bunch of sub/superscripts (with E and
5 because they are box-like and separation differences are easier to spot):
[image: subsup_test_tex]
https://cloud.githubusercontent.com/assets/576258/9117283/05c01816-3c61-11e5-821a-484d800f4378.png

The current master renders it as follows:
[image: subsup_test_mpl_master]
https://cloud.githubusercontent.com/assets/576258/9117307/209ab696-3c61-11e5-8317-91bbc0f47877.png

And with the modifications of this PR, it looks like this:
[image: subsup_test_mpl]
https://cloud.githubusercontent.com/assets/576258/9117316/2b5a300c-3c61-11e5-85d0-3a157af8d0c7.png

Summary of changes:

  • Additional kerning of -0.1 em after sub/super blocks.
  • Superscripts are shifted away from nucleus by 0.15 em when nucleus
    is slanted.
  • When there is both sub and superscript and nucleus is slanted,
    superscripts are only shifted an aditional 0.15 em (down from >0.20 em)
  • Subscripts are shifted down 0.25 x-height instead of 0.5 when there
    is no superscript.

Regression images coming soon!

You can view, comment on, or merge this pull request online at:

#4873
Commit Summary

  • first attempt
  • finetuning with cm TeX

File Changes

Patch Links:


Reply to this email directly or view it on GitHub
#4873.

@zblz
Copy link
Member Author

zblz commented Aug 6, 2015

Well, it seems that reducing the space after the sub/subscript is not that simple after all: it works well with numbers, but for slanted characters:
subsup_test_mpl_finetune

TeX, of course, handles these beautifully. I'll try to see where the difference in spacing comes from.

@WeatherGod: indeed it is, I'll try to match that as well.

@tacaswell tacaswell added this to the Color overhaul milestone Aug 6, 2015
@zblz
Copy link
Member Author

zblz commented Aug 6, 2015

Trying to see why $a_{ji}$ was being rendered with too little kerning, I found that the current master renders $a_{i}$ and $a_i$ differently. After much blood and tears in the depths of mathtext.py, I think it is because calling .shrink() on a Char object results in different kerning that calling it on a Hlist object. The groups between { } are parsed as Hlist objects, whereas the characters in $a_i$ are parsed as Char. The current master shrinks and then converts into a Hlist, and I have changed it so that evereything is an Hlist before shrinking, so there is better consistency. To showcase this, I made a plot with two lines that only differ by (unnecesary) braces:

r'$x_kx^py^{p-2} \quad d_i^jb_jc_kd \quad x^j_i E^0 E^0_u$',
r'${x}_{k}{x}^{p}{y}^{p-2} \quad {d}_{i}^{j}{b}_{j}{c}_{k}{d} \quad {x}^{j}_{i}{E}^{0}{E}^0_u$',

Current master renders them clearly different:
subsup_test_mpl_master

And with the changes mentioned they are consistent with each other:
subsup_test_mpl_finetune

I have also finetuned the supsuper spacings to match Computer Modern on TeX. Here is the reference TeX rendering (with text.usetex=True):
subsup_test_mpl_tex

And with this PR:
subsup_test_mpl_finetune

Compared to the current master:
subsup_test_mpl_master

However, not all is rosy, because the same parameters that work for CM fail miserably when using stixsans or stix as mathtext.fontset, so I will try to do a dict that sets them based on the fontset. See for stix (note the superscripts in particular):
subsup_test_mpl_finetune-stix

zblz added 2 commits August 7, 2015 01:08
finetuning with cm TeX

match superscript height
@zblz zblz force-pushed the subsuper-finetune branch from e070ae5 to e304248 Compare August 7, 2015 00:09
@zblz zblz force-pushed the subsuper-finetune branch from e304248 to 14a05ef Compare August 7, 2015 00:33
@tacaswell
Copy link
Member

@zblz Thank you for taking this on!

@zblz zblz force-pushed the subsuper-finetune branch from a99c3bc to 68d99be Compare August 7, 2015 10:13
@zblz
Copy link
Member Author

zblz commented Aug 7, 2015

So I think I've got it under control now for CM, stix and stixsans fontsets. There were a few challenges with integrals (also related to the Char vs Hlist issue above), but they look good now and I have added a couple of tests for them. I will amend the baseline images shortly and then the PR can be properly reviewed.

This is how a sample looks with this PR in cm:
edit: outdated, see images further below

To be compared with the usetex=True rendering:
edit: outdated, see images further below

The same sample also looks good for stix and stixsans:
edit: outdated, see images further below

@zblz zblz force-pushed the subsuper-finetune branch from c157234 to a45cd06 Compare August 7, 2015 10:56
@zblz zblz force-pushed the subsuper-finetune branch from a45cd06 to de67357 Compare August 7, 2015 11:16
@zblz
Copy link
Member Author

zblz commented Aug 7, 2015

@tacaswell, @mdboom : I think this is now complete, it would be great if you could review it at some point.

Looking at the issue list, this PR will fix the issue with spacing after superscripts reported in #4333.

@zblz
Copy link
Member Author

zblz commented Aug 7, 2015

I have also added the sub/supercript parameters for Arev Sans, which is checked for by looking at

rcParams['mathtext.fontset'] == 'custom'
rcParams['mathtext.rm'] == 'sans'
rcParams['font.sans-serif'][0].lower() == 'arev sans'

This could be useful for users using it and if #4636 is accepted.

@zblz zblz force-pushed the subsuper-finetune branch from 7a76c4d to 91a6146 Compare August 7, 2015 13:24
@zblz zblz force-pushed the subsuper-finetune branch from 91a6146 to 2959e57 Compare August 8, 2015 20:11
@zblz zblz mentioned this pull request Aug 9, 2015
zblz added 2 commits August 10, 2015 10:48
clean up, remove unnecesary do_kern=False

fix overunder

make cm clearer at small sizes
@zblz zblz force-pushed the subsuper-finetune branch from c8fee9e to 2c67193 Compare August 10, 2015 10:09
@mdboom
Copy link
Member

mdboom commented Aug 24, 2015

Thanks! I'm happy to merge this once Travis passes.

@WeatherGod
Copy link
Member

hold on. The second to last line has some spacing issues. The "f" and "2"
and "9" (both as a superscript and as a regular numeral) are much too close.

On Mon, Aug 24, 2015 at 10:45 AM, Michael Droettboom <
notifications@github.com> wrote:

Thanks! I'm happy to merge this once Travis passes.


Reply to this email directly or view it on GitHub
#4873 (comment)
.

@WeatherGod
Copy link
Member

Also, the "prime" symbol in that line is much too close, especially when
the "f" is italic.

On Mon, Aug 24, 2015 at 10:49 AM, Benjamin Root ben.v.root@gmail.com
wrote:

hold on. The second to last line has some spacing issues. The "f" and "2"
and "9" (both as a superscript and as a regular numeral) are much too close.

On Mon, Aug 24, 2015 at 10:45 AM, Michael Droettboom <
notifications@github.com> wrote:

Thanks! I'm happy to merge this once Travis passes.


Reply to this email directly or view it on GitHub
#4873 (comment)
.

@WeatherGod
Copy link
Member

Also, our mathtext might be parsing something slightly differently than
LaTeX is. In that same line, LaTeX sees the prime symbol as being on its
own and positions it as if it was a normal piece of text (nice and big),
but everywhere else, it is some sort of weird, scraggly looking superscript.

On Mon, Aug 24, 2015 at 10:49 AM, Benjamin Root ben.v.root@gmail.com
wrote:

Also, the "prime" symbol in that line is much too close, especially when
the "f" is italic.

On Mon, Aug 24, 2015 at 10:49 AM, Benjamin Root ben.v.root@gmail.com
wrote:

hold on. The second to last line has some spacing issues. The "f" and "2"
and "9" (both as a superscript and as a regular numeral) are much too close.

On Mon, Aug 24, 2015 at 10:45 AM, Michael Droettboom <
notifications@github.com> wrote:

Thanks! I'm happy to merge this once Travis passes.


Reply to this email directly or view it on GitHub
#4873 (comment)
.

@zblz
Copy link
Member Author

zblz commented Aug 24, 2015

@WeatherGod: Yes, the kerning for the f is particularly tight, but it is tight everywhere, as you mention for the regular f and numerals 2 and 9 (and that's why I added them to that line), so it is not an issue with the sub/super script. I tried to minimize it for cm, where it looks pretty good, but did not manage for stix of stixsans. Maybe the kerning for the f should be slightly widened elsewhere to match TeX. I added a modification of the superscript kerning that takes into account how much the last character rises above the x-height to try and mitigate this, but it is tricky: if it is correct for the italic f, it is too wide for, e.g., $E^0$ (which is already slightly wider than tex), so I tried to reach a compromise.

The prime issue only happens for stix and stixsans, where it seems to match $\prime$ to an already superscripted prime, as opposed to a nice and big prime for TeX and cm. This I also believe is outside the scope of this PR, but should definitely be fixed so they are consistent.

@zblz zblz force-pushed the subsuper-finetune branch from 9fba51b to f1fe6e5 Compare August 24, 2015 20:18
@zblz
Copy link
Member Author

zblz commented Aug 24, 2015

I fixed a test failure in test_axes that came from having no nucleus inside mathtext (e.g., a$^2$). I added a test in mathtext so it can be caught by running only the test_mathtext tests. There were also a couple of superscripts in other tests that were failing because I had not replaced the baseline images. Should be fine now.

@mdboom
Copy link
Member

mdboom commented Aug 24, 2015

I agree that the stix issues with the prime symbol and some of the other miscellaneous spacing is a separate issue from this PR, and indeed this PR makes things no worse than it was before in that regard.

@WeatherGod
Copy link
Member

Ok, well, let's create some issue tickets for those so that we don't forget to fix them. This is great work @zblz!

@WeatherGod
Copy link
Member

Ok, I created issue #4987 to track the issues I noted above. Great work!

Mike, I have not reviewed a single line of code here (I wouldn't know what any of it means anyway). So, I'll leave merging this up to you?

mdboom added a commit that referenced this pull request Aug 25, 2015
mathtext: Finetuning sup/super block to match TeX reference
@mdboom mdboom merged commit 6e4e3a2 into matplotlib:master Aug 25, 2015
@tacaswell tacaswell modified the milestones: Color overhaul, next major release (2.0) Oct 26, 2015
zblz added a commit to zblz/matplotlib that referenced this pull request Oct 29, 2015
see comments in matplotlib#4873 and matplotlib#4987: Primes in stix and DejaVu are already
superscripted, so the TeX way of setting a prime as $f^\prime$ results in
doubly-superscripted primes. For consistency with TeX, here we use the CM prime
symbol.
zblz added a commit to zblz/matplotlib that referenced this pull request Oct 29, 2015
see comments in matplotlib#4873 and matplotlib#4987: Primes in stix and DejaVu are already
superscripted, so the TeX way of setting a prime as $f^\prime$ results in
doubly-superscripted primes. For consistency with TeX, here we use the CM prime
symbol.
zblz added a commit to zblz/matplotlib that referenced this pull request Oct 29, 2015
see comments in matplotlib#4873 and matplotlib#4987: Primes in stix and DejaVu are already
superscripted, so the TeX way of setting a prime as $f^\prime$ results in
doubly-superscripted primes. For consistency with TeX, here we use the CM prime
symbol.
zblz added a commit to zblz/matplotlib that referenced this pull request Nov 5, 2015
see comments in matplotlib#4873 and matplotlib#4987: Primes in stix and DejaVu are already
superscripted, so the TeX way of setting a prime as $f^\prime$ results in
doubly-superscripted primes. For consistency with TeX, here we use the CM prime
symbol.
@QuLogic QuLogic modified the milestones: v1.5.0, 2.0 (style change major release) Oct 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants