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

Skip to content

Commit aca130b

Browse files
committed
Making parser more robust -- will only accept valid symbol names.
(Re-)adding \hspace{ } command. svn path=/trunk/matplotlib/; revision=3724
1 parent 569923b commit aca130b

3 files changed

Lines changed: 23 additions & 27 deletions

File tree

examples/mathtext_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
r'$100\%y\ x*y\ x/y x\$y$',
1010
r'$x\leftarrow y\ x\forall y\ x-y$',
1111
r'$x \sf x \bf x {\cal X} \rm x$',
12-
r'$x\ x\,x\;x\quad x\qquad x\!x$',
12+
r'$x\ x\,x\;x\quad x\qquad x\!x\hspace{0.5}y$',
1313
r'$\{ \rm braces \}$',
1414
r'$\left[\left\lfloor\frac{5}{\frac{\left(3\right)}{4}} y\right)\right]$',
1515
r'$\left(x\right)$',

lib/matplotlib/_mathtext_data.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,6 @@
17631763
'Theta': 920,
17641764
'origof': 8886,
17651765
'blacksquare': 9632,
1766-
'hspace': 8202,
17671766
'solbar': 9023,
17681767
'neg': 172,
17691768
'sum': 8721,

lib/matplotlib/mathtext.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,12 +1750,10 @@ class Parser(object):
17501750
_overunder_symbols = Set(r'''
17511751
\sum \prod \coprod \bigcap \bigcup \bigsqcup \bigvee
17521752
\bigwedge \bigodot \bigotimes \bigoplus \biguplus
1753-
'''.split()
1754-
)
1753+
'''.split())
17551754

17561755
_overunder_functions = Set(
1757-
r"lim liminf limsup sup max min".split()
1758-
)
1756+
r"lim liminf limsup sup max min".split())
17591757

17601758
_dropsub_symbols = Set(r'''\int \oint'''.split())
17611759

@@ -1769,11 +1767,13 @@ def __init__(self):
17691767
autoDelim = Forward().setParseAction(self.auto_sized_delimiter)
17701768
self._expression = Forward().setParseAction(self.finish).setName("finish")
17711769

1770+
float = Regex(r"-?[0-9]+\.?[0-9]*")
1771+
17721772
lbrace = Literal('{').suppress()
17731773
rbrace = Literal('}').suppress()
17741774
start_group = (Optional(latexfont) + lbrace)
17751775
start_group.setParseAction(self.start_group)
1776-
end_group = rbrace
1776+
end_group = rbrace.copy()
17771777
end_group.setParseAction(self.end_group)
17781778

17791779
bslash = Literal('\\')
@@ -1786,15 +1786,9 @@ def __init__(self):
17861786
"cosh gcd ln sup cot hom log tan coth inf max "
17871787
"tanh")
17881788

1789-
number = Combine(Word(nums) + Optional(Literal('.')) + Optional( Word(nums) ))
1790-
17911789
fontname = oneOf("rm cal it tt sf bf")
17921790
latex2efont = oneOf("mathrm mathcal mathit mathtt mathsf mathbf")
17931791

1794-
texsym = Combine(bslash + Word(alphanums) + NotAny("{"))
1795-
1796-
char = Word(alphanums + ' ', exact=1).leaveWhitespace()
1797-
17981792
space =(FollowedBy(bslash)
17991793
+ (Literal(r'\ ')
18001794
| Literal(r'\/')
@@ -1806,19 +1800,18 @@ def __init__(self):
18061800
)
18071801
).setParseAction(self.space).setName('space')
18081802

1809-
symbol = Regex("(" + ")|(".join(
1810-
[
1811-
r"\\(?!quad)(?!qquad)(?!left[^a-z])(?!right[^a-z])[a-zA-Z0-9]+(?!{)",
1812-
r"[a-zA-Z0-9 ]",
1813-
r"[+\-*/]",
1814-
r"[<>=]",
1815-
r"[:,.;!'@[()]",
1816-
r"\\[$%{}]",
1817-
])
1818-
+ ")"
1819-
).setParseAction(self.symbol).leaveWhitespace()
1803+
customspace =(Literal(r'\hspace')
1804+
+ lbrace
1805+
+ float
1806+
+ rbrace
1807+
).setParseAction(self.customspace).setName('customspace')
18201808

1821-
rightBracket = Literal("[").setParseAction(self.symbol).leaveWhitespace()
1809+
symbol =(Regex(r"[a-zA-Z0-9 +\-*/<>=:,.;!'@()[\]]")
1810+
^ Combine(
1811+
bslash
1812+
+ oneOf(tex2uni.keys())
1813+
)
1814+
).setParseAction(self.symbol).leaveWhitespace()
18221815

18231816
accent = Group(
18241817
Combine(bslash + accent)
@@ -1873,13 +1866,13 @@ def __init__(self):
18731866
placeable <<(accent
18741867
^ function
18751868
^ symbol
1876-
^ rightBracket
18771869
^ group
18781870
^ frac
18791871
^ sqrt
18801872
)
18811873

18821874
simple <<(space
1875+
| customspace
18831876
| font
18841877
| subsuper
18851878
)
@@ -2016,6 +2009,9 @@ def space(self, s, loc, toks):
20162009
box = self._make_space(num)
20172010
return [box]
20182011

2012+
def customspace(self, s, loc, toks):
2013+
return [self._make_space(float(toks[1]))]
2014+
20192015
def symbol(self, s, loc, toks):
20202016
# print "symbol", toks
20212017
c = toks[0]
@@ -2226,7 +2222,8 @@ def subsuperscript(self, s, loc, toks):
22262222
y = Hlist([sub])
22272223
# y.width += SCRIPT_SPACE * xHeight
22282224
shift_down = max(shift_down, SUB1 * xHeight)
2229-
clr = 2.0 * rule_thickness - ((shift_up - x.depth) - (y.height - shift_down))
2225+
clr = (2.0 * rule_thickness -
2226+
((shift_up - x.depth) - (y.height - shift_down)))
22302227
if clr > 0.:
22312228
shift_up += clr
22322229
shift_down += clr

0 commit comments

Comments
 (0)