-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
|
@@ -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 | ||
|
@@ -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|" | ||
"sp|bp|dd|pc|nc|nd|cc)") | ||
p.float_with_wo_unit <<= Group((p.float_literal + p.unit) | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
@@ -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}")) | ||
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: (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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here is for mu: https://sv.overleaf.com/learn/latex/Lengths_in_LaTeX |
||
'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] | ||
|
@@ -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() | ||
|
@@ -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() | ||
|
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.
I think you can get rid of the grouping parentheses, which should be marginally more efficient.