Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb8b3d77

Browse files
committed
mathtext now supports units for the third genfrac argument
1 parent612cfae commitb8b3d77

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-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: 38 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|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
defspace(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
ifstyleisnotself._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-
returnself._genfrac('','',thickness,self._MathStyle.TEXTSTYLE,
2811+
returnself._genfrac('','',[thickness],self._MathStyle.TEXTSTYLE,
27902812
num,den)
27912813

27922814
defdfrac(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-
returnself._genfrac('','',thickness,self._MathStyle.DISPLAYSTYLE,
2819+
returnself._genfrac('','',[thickness],self._MathStyle.DISPLAYSTYLE,
27982820
num,den)
27992821

28002822
defbinom(self,s,loc,toks):
28012823
(num,den),=toks
2802-
returnself._genfrac('(',')',0.0,self._MathStyle.TEXTSTYLE,
2824+
returnself._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+
iflen(val)==2:
2830+
ifval[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+
returnret
2837+
28052838
def_genset(self,s,loc,toks):
28062839
(annotation,body),=toks
28072840
state=self.get_state()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp