GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(mathtext): support underline by tfpf · Pull Request #23616 · matplotlib/matplotlib

Conversation

@tfpf

@tfpf tfpf commented Aug 14, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

PR Summary

#15624 hasn't seen any activity for a while, so I went ahead with this.

Note that the underline is drawn below the lowest descender. This is consistent with TeX. (The PNG images suffer from what I assume is pixel rounding.)

Fixes #14235.
Closes #15624

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@tfpf
tfpf marked this pull request as ready for review August 14, 2022 13:06
@oscargus oscargus added the topic: text/mathtext label Aug 14, 2022
@oscargus

oscargus commented Aug 14, 2022

Copy link
Copy Markdown
Member

Looks good! I think it makes sense to add a brief What's new for this to make users aware that it is added.

Also, I think it may be enough to have a "lightweight" test for this as it doesn't seem very dependent on the font:

matplotlib/lib/matplotlib/tests/test_mathtext.py

Lines 124 to 132 in ecba9d2

# 'lightweight' tests test only a single fontset (dejavusans, which is the
# default) and only png outputs, in order to minimize the size of baseline
# images.
lightweight_math_tests = [
r'$\sqrt[ab]{123}$', # github issue #8665
r'$x \overset{f}{\rightarrow} \overset{f}{x} \underset{xx}{ff} \overset{xx}{ff} \underset{f}{x} \underset{f}{\leftarrow} x$', # github issue #18241
r'$\sum x\quad\sum^nx\quad\sum_nx\quad\sum_n^nx\quad\prod x\quad\prod^nx\quad\prod_nx\quad\prod_n^nx$', # GitHub issue 18085
r'$1.$ $2.$ $19680801.$ $a.$ $b.$ $mpl.$',
]

tfpf reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tfpf
tfpf force-pushed the underline branch from 771db39 to b87c1b4 Compare August 15, 2022 05:06
oscargus
oscargus approved these changes Aug 15, 2022
View reviewed changes
@timhoffm
timhoffm requested a review from anntzer August 15, 2022 08:04
@timhoffm

timhoffm commented Aug 15, 2022

Copy link
Copy Markdown
Member

@anntzer I think you as our TeX expert should review this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@anntzer

anntzer commented Aug 15, 2022

Copy link
Copy Markdown
Contributor

Sure, l'll need a couple of days though.

timhoffm reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@anntzer

anntzer commented Aug 18, 2022

Copy link
Copy Markdown
Contributor

This seems reasonable, but 1) do you know why the overline() implementation is so different, using a fill instead? (which seems inconsistent with the TeXbook); 2) the TeXbook (rules G9/G10 p443) add an extra thickness of whitespace clearance over the overline / under the underline; do you know why they are not needed here (if they indeed aren't)?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tfpf

tfpf commented Aug 20, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

the TeXbook (rules G9/G10 p443) add an extra thickness of whitespace clearance over the overline / under the underline

I think that extra space is used to set the minimum leading.

more_leading
If the leading is high, the vertical distance between the tops of the Ts and the underscores on the previous lines remains unchanged even if the underscores are underlined. (As if the underline wasn't there at all.)

less_leading
Upon reducing the leading, the Ts remain some distance away from the underline. It's like the underline forces the next line to go lower if it is too close.

I have not looked through Mathtext's leading rules, but it certainly looks like it's better to follow what TeX does. I will update this. Getting second thoughts about this now. Should I add that space at the bottom, or leave this as it is?

do you know why the overline() implementation is so different, using a fill instead

That's a good question, one which I wondered myself, but couldn't figure out. In fact, I haven't completely understood how glue works in Mathtext.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tfpf
tfpf marked this pull request as draft August 20, 2022 06:47
@tfpf tfpf mentioned this pull request Aug 28, 2022
[Bug]: Inconsistent rendering between backends when rendering Mathtext horizontal rule #23763
Closed
@github-actions github-actions Bot added the status: needs rebase label Mar 31, 2023
@melissawm melissawm mentioned this pull request Jul 4, 2023
Add \underline to mathtext #15624
Closed
6 tasks done
@QuLogic

QuLogic commented Mar 11, 2026

Copy link
Copy Markdown
Member

Could the differences have to do with the rule being incorrectly rendered inverted? #31046 (comment)

anntzer reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tfpf
tfpf force-pushed the underline branch from b87c1b4 to eaad1a0 Compare April 11, 2026 19:24
@github-actions github-actions Bot removed the status: needs rebase label Apr 11, 2026
@tfpf tfpf changed the title Support \underline in Mathtext. feat(mathtext): support underline Apr 11, 2026
@tfpf

tfpf commented Apr 11, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

As pointed out by QuLogic and anntzer, the rendering issue is now resolved. Time to bring this back.😴

Preliminary Check

import string

import matplotlib.pyplot as plt

plt.rcdefaults()

fig, ax = plt.subplots(figsize=(20, 5))
ax.text(
    .5, .5, " ".join(f"${c}\\underline{{{c}}}$" for c in string.ascii_lowercase),
    size=30, ha="center", va="baseline",
)
ax.axhline(.5)
fig.savefig("underline.png")
fig.savefig("underline.svg")
plt.show()

The underlines are drawn as described in TEX: The Program. Thus, text with descenders will push the underline lower. This is the expected behaviour; see exercise 18.26 on page 178 of The TeXbook.

underline.png underline.svg

Caveats

  • The lines under 'e' and 'f' are not aligned because their lowermost points aren't aligned. (This will be font-specific.)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tfpf

tfpf commented Apr 11, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

do you know why the overline() implementation is so different, using a fill instead? (which seems inconsistent with the TeXbook)

I can make a guess.

The line above the body of a radical must touch the top of the radical (a tick mark), so it is imperative that the line be rendered at the correct height. This combined with the fact that the text under radicals (and overlines) is supposed to be rendered in cramped style might explain why the space between it and the body was not calculated but filled up with stretchable glue. You can see this in the sqrt function. That logic appears to have been reused for overline.

the TeXbook (rules G9/G10 p443) add an extra thickness of whitespace clearance over the overline / under the underline; do you know why they are not needed here (if they indeed aren't)?

I am now fairly sure that this was done to give the line some space between it and the previous or next lines. I suppose the same could be done here, but it is not clear why he set the depth directly instead of adding a vertical space.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tfpf
tfpf marked this pull request as ready for review April 12, 2026 11:30
@tfpf
tfpf force-pushed the underline branch from 3caa19a to 4730c8a Compare April 12, 2026 11:33
@QuLogic

QuLogic commented Apr 17, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Comparing with usetex=True, this does look pretty consistent with LaTeX, though it seems our rule thickness is a bit bigger (and so the gap is bigger).

But the inconsistency with the underline thickness does make me wonder if we should disable snapping on all mathtext boxes. Any thoughts @anntzer @tacaswell? Unfortunately, this would cause about 120 test image changes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

QuLogic
QuLogic approved these changes Apr 17, 2026
View reviewed changes

@QuLogic QuLogic left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This is good otherwise.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@tacaswell

tacaswell commented Apr 23, 2026

Copy link
Copy Markdown
Member

As discussed on the call:

  • take this as is with the varying lines (using a higher DPI makes this a moot issue)
  • we have the plan in our back pocket to change the behavior and then manually set the current behavior in the existing tests to avoid having to regenerate them if in the future we decide to accept the more consistent (but slightly fuzzier) no-snapping on boxes
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tacaswell
tacaswell approved these changes Apr 23, 2026
View reviewed changes
@tacaswell tacaswell added this to the v3.11.0 milestone Apr 23, 2026
@tfpf @QuLogic
Implement underline logic as in TeX
44a6337
@QuLogic
QuLogic force-pushed the underline branch from 4730c8a to 44a6337 Compare April 23, 2026 20:00
@QuLogic

QuLogic commented Apr 23, 2026

Copy link
Copy Markdown
Member

I decided to rebase so I could squash and push an optimization to the test image with oxipng. And then that should get the CI to rerun.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@QuLogic
QuLogic merged commit 57f697c into matplotlib:main Apr 23, 2026
41 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@tfpf
tfpf deleted the underline branch September 25, 2026 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tacaswell tacaswell tacaswell approved these changes
@QuLogic QuLogic QuLogic approved these changes
@oscargus oscargus oscargus approved these changes
@anntzer anntzer Awaiting requested review from anntzer

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

topic: text/mathtext

Projects

None yet

Milestone

v3.11.0

Development

Successfully merging this pull request may close these issues.

Add \underline to mathtext?

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@tfpf @oscargus @timhoffm @anntzer @QuLogic @tacaswell
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.