Skip to content

[Bug]: Spaced symbols should not be spaced when in limits etc #26174

Closed as not planned

Description

@oscargus

Bug summary

As seen, LaTeX does not add a space around the arrows (or =/+-sign) when in subscripts, but mathtext does.

Note that there is no space around \to but in mathtext,around \rightarrow in mathtext, while for LaTeX they look exactly the same. (This is because \to is not listed in _mathtext.Parser._arrow_symbols, but it should be.)

Code for reproduction

import matplotlib.pyplot as plt

strings = [r'$\limsup_{x\to\infty}$',
           r'$\limsup_{x\rightarrow\infty}$',
           r'$x \to \infty$',
           r'$x \rightarrow \infty$',
           r'$\limsup_{x=a+b}$',
           r'$x=a+b$',
           r'$A^{a \times b}_{c \Uparrow d}$']

for i, s in enumerate(strings):
    plt.text(0, i*0.15 + 0.05, s, fontsize=20)
    plt.text(0.5, i*0.15 + 0.05, f"${s}$", usetex=True, fontsize=20)

plt.axis(False)
plt.savefig("spacing.png", dpi=200)

Actual outcome

image

Expected outcome

No spaces around "spaced operators" in subscript.

Additional information

@devRD is this something you know how to fix?

Related to the failing test in #26173

Operating system

No response

Matplotlib Version

Latest main

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Activity

  1. devRD commented on Jun 23, 2023

    @devRD
    Contributor

    All the equations get parsed through the def symbol: function, so in mathtext we have the following code to add spacing around the operators. Currently, we have a check for binary operators to neglect spacing at the start.

    return [Hlist([self._make_space(0.2),
    char,
    self._make_space(0.2)],
    do_kern=True)]

    For this issue, we could add a check for limits so that the symbols are not spaced by mathtext?

  2. anntzer commented on Jun 23, 2023

    @anntzer
    Contributor

    (Side point re: the original report: the fair comparison with latex would use $$...$$ in latex as we're effectively always in displaymath mode in mathtext, but even there the spacing gets suppressed.)

  3. oscargus commented on Jun 23, 2023

    @oscargus
    MemberAuthor

    The same issue happens in sub- and superscripts as well. I checked the code a bit and maybe the easiest way is to remove any space from sub- and superscripts (and over/under)?

    (Re: side point: I was too lazy to sort that out, but have updated the example, at least if you wait 30 seconds after this comment.)

  4. oscargus commented on Jun 23, 2023

    @oscargus
    MemberAuthor

    Or maybe one can use self._in_subscript_or_superscript to determine if spaces should be added? (Have not studied the code enough to realize if that may work.)

    I guess blindly removing spaces may also remove those added by e.g. \quad...

  5. oscargus commented on Jun 24, 2023

    @oscargus
    MemberAuthor

    @devRD, I fixed it in #26173 as it turned out to be straightforward (do not add space if self._in_subscript_or_superscript).

  6. oscargus commented on Jun 24, 2023

    @oscargus
    MemberAuthor

    Well, actually not fully. It works for the overunder case, but not for sub- and superscripts...

  7. devRD commented on Jun 24, 2023

    @devRD
    Contributor

    The self._in_subscript_or_superscript check does work for the over-under cases!

    I think the self._in_subscript_or_superscript flag determines if we need to add spacing to the nucleus, and it is not checking if the equation is a subscript or superscript, so for the string like r'$A^{a \times b}_{c \Uparrow d}$ in the example the flag's value is False

  8. oscargus commented on Jun 24, 2023

    @oscargus
    MemberAuthor

    Agreed. I've tried to insert a separate flag in subsuper, but that didn't work...

  9. github-actions commented on Sep 14, 2026

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  10. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.status: inactiveMarked by the “Stale” Github Actiontopic: text/mathtext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions