Skip to content

Commit b8b3d77

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

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
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

+38-5
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,10 @@ 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|em|mu|"
2010+
"sp|bp|dd|pc|nc|nd|cc)")
2011+
p.float_with_wo_unit <<= Group((p.float_literal + p.unit) |
2012+
p.float_literal)
20072013

20082014
p.lbrace <<= Literal('{').suppress()
20092015
p.rbrace <<= Literal('}').suppress()
@@ -2093,7 +2099,7 @@ def __init__(self):
20932099
+ (p.lbrace
20942100
+ Optional(p.ambi_delim | p.right_delim_safe, default='')
20952101
+ p.rbrace)
2096-
+ (p.lbrace + p.float_literal + p.rbrace)
2102+
+ (p.lbrace + p.float_with_wo_unit + p.rbrace)
20972103
+ p.simple_group + p.required_group + p.required_group)
20982104
| Error("Expected "
20992105
r"\genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}"))
@@ -2322,6 +2328,22 @@ def _make_space(self, percentage):
23222328
r'\!': -0.16667, # -3/18 em = -3 mu
23232329
}
23242330

2331+
_unit_multipliers = {
2332+
# pt|mm|cm|in|mu|sp|bp|dd|pc|nc|nd|cc
2333+
"pt": 1,
2334+
'mm': 7227/2540,
2335+
'cm': 7227/254,
2336+
'in': 7227/100,
2337+
'mu': 7227/2540000,
2338+
'sp': 1/65536,
2339+
'bp': 803/800,
2340+
'dd': 1238/1157,
2341+
'pc': 12,
2342+
'nc': 1370/107,
2343+
'nd': 685/642,
2344+
'cc': 14856/1157
2345+
}
2346+
23252347
def space(self, s, loc, toks):
23262348
tok, = toks
23272349
num = self._space_widths[tok]
@@ -2741,7 +2763,7 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
27412763
thickness = state.font_output.get_underline_thickness(
27422764
state.font, state.fontsize, state.dpi)
27432765

2744-
rule = float(rule)
2766+
rule = self._get_float_with_unit(rule, state.fontsize)
27452767

27462768
if style is not self._MathStyle.DISPLAYSTYLE:
27472769
num.shrink()
@@ -2786,22 +2808,33 @@ def frac(self, s, loc, toks):
27862808
thickness = state.font_output.get_underline_thickness(
27872809
state.font, state.fontsize, state.dpi)
27882810
(num, den), = toks
2789-
return self._genfrac('', '', thickness, self._MathStyle.TEXTSTYLE,
2811+
return self._genfrac('', '', [thickness], self._MathStyle.TEXTSTYLE,
27902812
num, den)
27912813

27922814
def dfrac(self, s, loc, toks):
27932815
state = self.get_state()
27942816
thickness = state.font_output.get_underline_thickness(
27952817
state.font, state.fontsize, state.dpi)
27962818
(num, den), = toks
2797-
return self._genfrac('', '', thickness, self._MathStyle.DISPLAYSTYLE,
2819+
return self._genfrac('', '', [thickness], self._MathStyle.DISPLAYSTYLE,
27982820
num, den)
27992821

28002822
def binom(self, s, loc, toks):
28012823
(num, den), = toks
2802-
return self._genfrac('(', ')', 0.0, self._MathStyle.TEXTSTYLE,
2824+
return self._genfrac('(', ')', [0.0], self._MathStyle.TEXTSTYLE,
28032825
num, den)
28042826

2827+
def _get_float_with_unit(self, val, fontsize):
2828+
ret = float(val[0])
2829+
if len(val) == 2:
2830+
if val[1].lower() == 'em':
2831+
# "1 em is a distance equal to the type size"
2832+
ret = ret*fontsize
2833+
else:
2834+
# Remaining units
2835+
ret = ret*self._unit_multipliers[val[1]]
2836+
return ret
2837+
28052838
def _genset(self, s, loc, toks):
28062839
(annotation, body), = toks
28072840
state = self.get_state()

0 commit comments

Comments
 (0)