Skip to content

Public API to modify mathtext font constants #9165

Open

Description

@u55

Hi matplotlib developers,

I recently needed to modify the mathtext font constants (the superscript height) of a non-standard font (Arial) and found that there is no public API to accomplish this. I did, however, find that I could do what I wanted by accessing the private mathtext._font_constant_mapping dictionary. A minimal working example is below:

import matplotlib.pyplot as plt
from matplotlib import mathtext

plt.rcParams.update({
    'text.usetex':False,
    'font.family':'sans-serif',
    'font.sans-serif':['Arial'],
    'mathtext.default':'regular',
    })

class ArialFontConstants(mathtext.FontConstantsBase):
    # Percentage of x-height that superscripts are raised from the baseline
    sup1 = 0.4

# Register font constants for Arial
mathtext._font_constant_mapping['Arial'] = ArialFontConstants


text = r'$h = 6.62607004 \times 10^{-34}\; m^2\, kg\,/\,s$'

plt.figure(figsize=(3, 2))
plt.axis('off')
plt.text(0, 0.5, text)
plt.show()

This works great, but I think there should be a public and documented method to customize mathtext font constants. Perhaps we should just remove the leading underscore of _font_constant_mapping to make it permanent and public? Also, I think that an example of the new public API should be added to the mathtext tutorial documentation.

Thanks,

Tested with matplotlib 2.0.2 and python 3.6.2 running on Arch Linux.

Activity

  1. u55 commented on Sep 9, 2017

    @u55
    ContributorAuthor

    @mdboom Michael, git blame tells me that you changed the API back in baa2b6e. Did you have a particular reason in mind for keeping _font_constant_mapping private?

  2. anntzer commented on Sep 10, 2017

    @anntzer
    Contributor

    It's probably private because no one asked for it to be public, and we are actively trying to keep the public API as small as possible (and if it goes public it should be properly documented). Of course the funny thing is that the various constant classes are themselves public.

    FWIW having spent ~10s looking at it I'd guess the various classes should just be namedtuples (using ._replace if we want to be able to inherit from another font constant), but otherwise have no strong opinion as to making the dictionary public (if documented).

  3. github-actions commented on Apr 18, 2023

    @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!

  4. added
    keepItems to be ignored by the “Stale” Github Action
    on Apr 18, 2023
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

    keepItems to be ignored 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