Skip to content

Commit 96b4bae

Browse files
committed
mathtext now supports units for the third genfrac argument
1 parent 612cfae commit 96b4bae

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
``mathtext`` now supports units for the bar thickness ``\genfrac`` argument
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
This follows the standard LaTeX version where units are required.
5+
6+
.. code-block:: python
7+
8+
import matplotlib.pyplot as plt
9+
10+
plt.text(0.5, 0.5, r'$\genfrac{(}{)}{0.5cm}{0}{foo}{bar}$')
11+
plt.draw()

lib/matplotlib/_mathtext.py

+32-7
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,7 @@ def __init__(self):
19581958
p.customspace = Forward()
19591959
p.end_group = Forward()
19601960
p.float_literal = Forward()
1961+
p.float_with_wo_unit = Forward()
19611962
p.font = Forward()
19621963
p.frac = Forward()
19631964
p.dfrac = Forward()
@@ -1995,6 +1996,7 @@ def __init__(self):
19951996
p.symbol_name = Forward()
19961997
p.token = Forward()
19971998
p.underset = Forward()
1999+
p.unit = Forward()
19982000
p.unknown_symbol = Forward()
19992001

20002002
# Set names on everything -- very useful for debugging
@@ -2004,6 +2006,9 @@ def __init__(self):
20042006

20052007
p.float_literal <<= Regex(r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)")
20062008
p.int_literal <<= Regex("[-+]?[0-9]+")
2009+
p.unit <<= Regex("(pt|mm|cm|in|ex|em|mu|"
2010+
"sp|bp|dd|pc|nc|nd|cc)")
2011+
p.float_with_wo_unit <<= Group((p.float_literal + p.unit) | p.float_literal)
20072012

20082013
p.lbrace <<= Literal('{').suppress()
20092014
p.rbrace <<= Literal('}').suppress()
@@ -2093,7 +2098,7 @@ def __init__(self):
20932098
+ (p.lbrace
20942099
+ Optional(p.ambi_delim | p.right_delim_safe, default='')
20952100
+ p.rbrace)
2096-
+ (p.lbrace + p.float_literal + p.rbrace)
2101+
+ (p.lbrace + p.float_with_wo_unit + p.rbrace)
20972102
+ p.simple_group + p.required_group + p.required_group)
20982103
| Error("Expected "
20992104
r"\genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}"))
@@ -2322,6 +2327,24 @@ def _make_space(self, percentage):
23222327
r'\!': -0.16667, # -3/18 em = -3 mu
23232328
}
23242329

2330+
_unit_multipliers = {
2331+
# pt|mm|cm|in|ex|em|mu|sp|bp|dd|pc|nc|nd|cc
2332+
"pt": 1,
2333+
'mm': 7227/2540,
2334+
'cm': 7227/254,
2335+
'in': 7227/100,
2336+
'ex': 35271/8192,
2337+
'em': 655361/65536,
2338+
'mu': 7227/2540000,
2339+
'sp': 1/65536,
2340+
'bp': 803/800,
2341+
'dd': 1238/1157,
2342+
'pc': 12,
2343+
'nc': 1370/107,
2344+
'nd': 685/642,
2345+
'cc': 14856/1157
2346+
}
2347+
23252348
def space(self, s, loc, toks):
23262349
tok, = toks
23272350
num = self._space_widths[tok]
@@ -2740,8 +2763,10 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
27402763
state = self.get_state()
27412764
thickness = state.font_output.get_underline_thickness(
27422765
state.font, state.fontsize, state.dpi)
2743-
2744-
rule = float(rule)
2766+
ruleval = float(rule[0])
2767+
if len(rule) == 2:
2768+
# Units
2769+
ruleval = ruleval*self._unit_multipliers[rule[1]]
27452770

27462771
if style is not self._MathStyle.DISPLAYSTYLE:
27472772
num.shrink()
@@ -2753,7 +2778,7 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
27532778
cden.hpack(width, 'exactly')
27542779
vlist = Vlist([cnum, # numerator
27552780
Vbox(0, thickness * 2.0), # space
2756-
Hrule(state, rule), # rule
2781+
Hrule(state, ruleval), # rule
27572782
Vbox(0, thickness * 2.0), # space
27582783
cden # denominator
27592784
])
@@ -2786,20 +2811,20 @@ def frac(self, s, loc, toks):
27862811
thickness = state.font_output.get_underline_thickness(
27872812
state.font, state.fontsize, state.dpi)
27882813
(num, den), = toks
2789-
return self._genfrac('', '', thickness, self._MathStyle.TEXTSTYLE,
2814+
return self._genfrac('', '', [thickness], self._MathStyle.TEXTSTYLE,
27902815
num, den)
27912816

27922817
def dfrac(self, s, loc, toks):
27932818
state = self.get_state()
27942819
thickness = state.font_output.get_underline_thickness(
27952820
state.font, state.fontsize, state.dpi)
27962821
(num, den), = toks
2797-
return self._genfrac('', '', thickness, self._MathStyle.DISPLAYSTYLE,
2822+
return self._genfrac('', '', [thickness], self._MathStyle.DISPLAYSTYLE,
27982823
num, den)
27992824

28002825
def binom(self, s, loc, toks):
28012826
(num, den), = toks
2802-
return self._genfrac('(', ')', 0.0, self._MathStyle.TEXTSTYLE,
2827+
return self._genfrac('(', ')', [0.0], self._MathStyle.TEXTSTYLE,
28032828
num, den)
28042829

28052830
def _genset(self, s, loc, toks):

0 commit comments

Comments
 (0)