Skip to content

Support units for the third \genfrac argument #22171

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/users/next_whats_new/mathtext_genfrac_supports_unit.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
``mathtext`` now supports units for the bar thickness ``\genfrac`` argument
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This follows the standard LaTeX version where units are required.

.. code-block:: python

import matplotlib.pyplot as plt

plt.text(0.5, 0.5, r'$\genfrac{(}{)}{0.5cm}{0}{foo}{bar}$')
plt.draw()
43 changes: 38 additions & 5 deletions lib/matplotlib/_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,7 @@ def __init__(self):
p.customspace = Forward()
p.end_group = Forward()
p.float_literal = Forward()
p.float_with_wo_unit = Forward()
p.font = Forward()
p.frac = Forward()
p.dfrac = Forward()
Expand Down Expand Up @@ -1995,6 +1996,7 @@ def __init__(self):
p.symbol_name = Forward()
p.token = Forward()
p.underset = Forward()
p.unit = Forward()
p.unknown_symbol = Forward()

# Set names on everything -- very useful for debugging
Expand All @@ -2004,6 +2006,10 @@ def __init__(self):

p.float_literal <<= Regex(r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)")
p.int_literal <<= Regex("[-+]?[0-9]+")
p.unit <<= Regex("(pt|mm|cm|in|em|mu|"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can get rid of the grouping parentheses, which should be marginally more efficient.

"sp|bp|dd|pc|nc|nd|cc)")
p.float_with_wo_unit <<= Group((p.float_literal + p.unit) |
Copy link
Contributor

Choose a reason for hiding this comment

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

what's "wo"?

p.float_literal)

p.lbrace <<= Literal('{').suppress()
p.rbrace <<= Literal('}').suppress()
Expand Down Expand Up @@ -2093,7 +2099,7 @@ def __init__(self):
+ (p.lbrace
+ Optional(p.ambi_delim | p.right_delim_safe, default='')
+ p.rbrace)
+ (p.lbrace + p.float_literal + p.rbrace)
+ (p.lbrace + p.float_with_wo_unit + p.rbrace)
+ p.simple_group + p.required_group + p.required_group)
| Error("Expected "
r"\genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}"))
Expand Down Expand Up @@ -2322,6 +2328,22 @@ def _make_space(self, percentage):
r'\!': -0.16667, # -3/18 em = -3 mu
}

_unit_multipliers = {
# pt|mm|cm|in|mu|sp|bp|dd|pc|nc|nd|cc
"pt": 1,
'mm': 7227/2540,
'cm': 7227/254,
'in': 7227/100,
'mu': 7227/2540000,
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't find a ref for mu, nc, nd as standard tex units, perhaps you can link their definition? (as a comment here at least, not in the code)

Copy link
Member Author

Choose a reason for hiding this comment

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

I used some stack exchange thread combined with other sources, but cannot seem to find where I got ex and em from now. I agree with that they shouldn't be there though (just added what was there without really thinking about the meaning).

Here are some better sources:
https://en.wikibooks.org/wiki/LaTeX/Lengths
https://latexref.xyz/Units-of-length.html

(And here is a good discussion why ex and em are bad choices: https://tex.stackexchange.com/questions/4239/which-measurement-units-should-one-use-in-latex
)

Copy link
Member Author

Choose a reason for hiding this comment

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

'sp': 1/65536,
'bp': 803/800,
'dd': 1238/1157,
'pc': 12,
'nc': 1370/107,
'nd': 685/642,
'cc': 14856/1157
}

def space(self, s, loc, toks):
tok, = toks
num = self._space_widths[tok]
Expand Down Expand Up @@ -2741,7 +2763,7 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
thickness = state.font_output.get_underline_thickness(
state.font, state.fontsize, state.dpi)

rule = float(rule)
rule = self._get_float_with_unit(rule, state.fontsize)

if style is not self._MathStyle.DISPLAYSTYLE:
num.shrink()
Expand Down Expand Up @@ -2786,22 +2808,33 @@ def frac(self, s, loc, toks):
thickness = state.font_output.get_underline_thickness(
state.font, state.fontsize, state.dpi)
(num, den), = toks
return self._genfrac('', '', thickness, self._MathStyle.TEXTSTYLE,
return self._genfrac('', '', [thickness], self._MathStyle.TEXTSTYLE,
num, den)

def dfrac(self, s, loc, toks):
state = self.get_state()
thickness = state.font_output.get_underline_thickness(
state.font, state.fontsize, state.dpi)
(num, den), = toks
return self._genfrac('', '', thickness, self._MathStyle.DISPLAYSTYLE,
return self._genfrac('', '', [thickness], self._MathStyle.DISPLAYSTYLE,
num, den)

def binom(self, s, loc, toks):
(num, den), = toks
return self._genfrac('(', ')', 0.0, self._MathStyle.TEXTSTYLE,
return self._genfrac('(', ')', [0.0], self._MathStyle.TEXTSTYLE,
num, den)

def _get_float_with_unit(self, val, fontsize):
ret = float(val[0])
if len(val) == 2:
if val[1].lower() == 'em':
# "1 em is a distance equal to the type size"
ret = ret*fontsize
else:
# Remaining units
ret = ret*self._unit_multipliers[val[1]]
return ret

def _genset(self, s, loc, toks):
(annotation, body), = toks
state = self.get_state()
Expand Down