Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 46d2a1e

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

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed
Lines changed: 11 additions & 0 deletions
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

Lines changed: 36 additions & 5 deletions
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|ex|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,24 @@ def _make_space(self, percentage):
23222328
r'\!': -0.16667, # -3/18 em = -3 mu
23232329
}
23242330

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

2744-
rule = float(rule)
2768+
rule = self._get_float_with_unit(rule)
27452769

27462770
if style is not self._MathStyle.DISPLAYSTYLE:
27472771
num.shrink()
@@ -2786,22 +2810,29 @@ def frac(self, s, loc, toks):
27862810
thickness = state.font_output.get_underline_thickness(
27872811
state.font, state.fontsize, state.dpi)
27882812
(num, den), = toks
2789-
return self._genfrac('', '', thickness, self._MathStyle.TEXTSTYLE,
2813+
return self._genfrac('', '', [thickness], self._MathStyle.TEXTSTYLE,
27902814
num, den)
27912815

27922816
def dfrac(self, s, loc, toks):
27932817
state = self.get_state()
27942818
thickness = state.font_output.get_underline_thickness(
27952819
state.font, state.fontsize, state.dpi)
27962820
(num, den), = toks
2797-
return self._genfrac('', '', thickness, self._MathStyle.DISPLAYSTYLE,
2821+
return self._genfrac('', '', [thickness], self._MathStyle.DISPLAYSTYLE,
27982822
num, den)
27992823

28002824
def binom(self, s, loc, toks):
28012825
(num, den), = toks
2802-
return self._genfrac('(', ')', 0.0, self._MathStyle.TEXTSTYLE,
2826+
return self._genfrac('(', ')', [0.0], self._MathStyle.TEXTSTYLE,
28032827
num, den)
28042828

2829+
def _get_float_with_unit(self, val):
2830+
ret = float(val[0])
2831+
if len(val) == 2:
2832+
# Units
2833+
ret = ret*self._unit_multipliers[val[1]]
2834+
return ret
2835+
28052836
def _genset(self, s, loc, toks):
28062837
(annotation, body), = toks
28072838
state = self.get_state()

0 commit comments

Comments
 (0)