10000 Fix substack parsing · matplotlib/matplotlib@e93cc3a · GitHub
[go: up one dir, main page]

Skip to content

Commit e93cc3a

Browse files
committed
Fix substack parsing
1 parent a48e51b commit e93cc3a

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,6 @@ def names(elt):
17001700
csname = expr.split("{", 1)[0]
17011701
err = (csname + "".join("{%s}" % name for name in names(args))
17021702
if expr == csname else expr)
1703-
print(csname, args, err)
17041703
return csname - (args | Error(f"Expected {err}"))
17051704

17061705

@@ -1871,7 +1870,6 @@ def csnames(group, names):
18711870
p.required_group = Forward()
18721871
p.optional_group = Forward()
18731872
p.token = Forward()
1874-
p.substack = Forward()
18751873

18761874
set_names_and_parse_actions() # for mutually recursive definitions.
18771875

@@ -1917,6 +1915,10 @@ def csnames(group, names):
19171915

19181916
p.text = cmd(r"\text", QuotedString('{', '\\', endQuoteChar="}"))
19191917

1918+
p.substack = cmd(r"\substack", "{" +
1919+
delimited_list(OneOrMore(p.token), delim="\\",
1920+
combine=False)("parts") + "}")
1921+
19201922
p.subsuper = (
19211923
(Optional(p.placeable)("nucleus")
19221924
+ OneOrMore(oneOf(["_", "^"]) - p.placeable)("subsuper")
@@ -1935,9 +1937,6 @@ def csnames(group, names):
19351937

19361938
p.operatorname = cmd(r"\operatorname", "{" + ZeroOrMore(p.simple)("name") + "}")
19371939

1938-
p.substack <<= cmd(
1939-
r"\substack", "{"+delimited_list(OneOrMore(p.token), delim="\\\\", combine=True)("parts")+"}")
1940-
19411940
p.placeable <<= (
19421941
p.accent # Must be before symbol as all accents are symbols
19431942
| p.symbol # Must be second to catch all named symbols and single
@@ -1953,8 +1952,8 @@ def csnames(group, names):
19531952
| p.underset
19541953
| p.sqrt
19551954
| p.overline
1956-
| p.substack
19571955
| p.text
1956+
| p.substack
19581957
)
19591958

19601959
p.auto_delim <<= (
@@ -1977,7 +1976,6 @@ def csnames(group, names):
19771976

19781977
# To add space to nucleus operators after sub/superscripts
19791978
self._in_subscript_or_superscript = False
1980-
self.p = p
19811979

19821980
def parse(self, s, fonts_object, fontsize, dpi):
19831981
"""
@@ -2606,17 +2604,24 @@ def auto_delim(self, s, loc, toks):
26062604
toks["right"])
26072605

26082606
def substack(self, s, loc, toks):
2609-
print(list(toks.keys()))
2610-
print(toks["parts"])
2611-
parts = toks["parts"].split("\\\\")
2612-
print(parts)
2607+
parts = toks["parts"]
26132608
state = self.get_state()
26142609
thickness = state.get_current_underline_thickness()
26152610
vlist = []
26162611

2617-
max_width = max(map(lambda c: c.width, parts))
2612+
p = [[]]
2613+
# Checking idle kern value as whitespace to
2614+
# determine line break for multiple values
2615+
for b in parts:
2616+
if isinstance(b, Kern):
2617+
p.append([])
2618+
else:
2619+
p[-1].append(b)
2620+
2621+
hlist = [Hlist(k) for k in p]
2622+
max_width = max(map(lambda c: c.width, hlist))
26182623

2619-
for sub in parts:
2624+
for sub in hlist:
26202625
cp = HCentered([sub])
26212626
cp.hpack(max_width, 'exactly')
26222627
vlist.append(cp)
Loading

lib/matplotlib/tests/test_mathtext.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@
134134
r'$\sum x\quad\sum^nx\quad\sum_nx\quad\sum_n^nx\quad\prod x\quad\prod^nx\quad\prod_nx\quad\prod_n^nx$', # GitHub issue 18085
135135
r'$1.$ $2.$ $19680801.$ $a.$ $b.$ $mpl.$',
136136
r'$\text{text}_{\text{sub}}^{\text{sup}} + \text{\$foo\$} + \frac{\text{num}}{\mathbf{\text{den}}}\text{with space, curly brackets \{\}, and dash -}$',
137-
r'$\sum_{\substack{k = 1}{k \neq \lfloor n/2\rfloor}}^{n}P(i,j) \sum_{\substack{i \neq 0}{-1 \leq i \leq 3}{1 \leq j \leq 5}} F^i(x,y) \sum_{\substack{\left \lfloor \frac{n}{2} \right\rfloor}} F(n)$',
138137
]
139138

140139
digits = "0123456789"
@@ -300,7 +299,7 @@ def test_fontinfo():
300299
(r'$\left(\right$', r'Expected a delimiter'),
301300
# PyParsing 2 uses double quotes, PyParsing 3 uses single quotes and an
302301
# extra backslash.
303-
(r'$\left($', re.compile(r'Expected ("|\'\\)\\right["\']')),
302+
(r'$\left($', re.compile(r'Expected (\"|\'\\)\right[\"\']')),
304303
(r'$\dfrac$', r'Expected \dfrac{num}{den}'),
305304
(r'$\dfrac{}{}$', r'Expected \dfrac{num}{den}'),
306305
(r'$\overset$', r'Expected \overset{annotation}{body}'),

0 commit comments

Comments
 (0)
0