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

Skip to content

Commit 10b33c7

Browse files
committed
Support inverted parentheses in mathtext.
TeX is perfectly happy to start with a closing parenthesis and end with an opening one; no need to distinguish them.
1 parent f278686 commit 10b33c7

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,13 +1685,12 @@ class _MathStyle(enum.Enum):
16851685
liminf sin cos exp limsup sinh cosh gcd ln sup cot hom log tan
16861686
coth inf max tanh""".split())
16871687

1688-
_ambi_delim = set(r"""
1688+
_ambi_delims = set(r"""
16891689
| \| / \backslash \uparrow \downarrow \updownarrow \Uparrow
16901690
\Downarrow \Updownarrow . \vert \Vert \\|""".split())
1691-
1692-
_left_delim = set(r"( [ \{ < \lfloor \langle \lceil".split())
1693-
1694-
_right_delim = set(r") ] \} > \rfloor \rangle \rceil".split())
1691+
_left_delims = set(r"( [ \{ < \lfloor \langle \lceil".split())
1692+
_right_delims = set(r") ] \} > \rfloor \rangle \rceil".split())
1693+
_delims = _left_delims | _right_delims | _ambi_delims
16951694

16961695
def __init__(self):
16971696
p = types.SimpleNamespace()
@@ -1726,9 +1725,7 @@ def set_names_and_parse_actions():
17261725
Optional(r"\math" + oneOf(self._fontnames)("font")) + "{")
17271726
p.end_group = Literal("}")
17281727

1729-
p.ambi_delim = oneOf(self._ambi_delim)
1730-
p.left_delim = oneOf(self._left_delim)
1731-
p.right_delim = oneOf(self._right_delim)
1728+
p.delim = oneOf(self._delims)
17321729

17331730
set_names_and_parse_actions() # for root definitions.
17341731

@@ -1788,8 +1785,8 @@ def set_names_and_parse_actions():
17881785
| Error(r"Expected \binom{num}{den}"))
17891786

17901787
p.genfrac <<= r"\genfrac" - (
1791-
"{" + Optional(p.ambi_delim | p.left_delim)("ldelim") + "}"
1792-
+ "{" + Optional(p.ambi_delim | p.right_delim)("rdelim") + "}"
1788+
"{" + Optional(p.delim)("ldelim") + "}"
1789+
+ "{" + Optional(p.delim)("rdelim") + "}"
17931790
+ "{" + p.float_literal("rulesize") + "}"
17941791
+ p.simple_group("style")
17951792
+ p.required_group("num")
@@ -1854,13 +1851,9 @@ def set_names_and_parse_actions():
18541851
)
18551852

18561853
p.auto_delim <<= (
1857-
r"\left"
1858-
- ((p.left_delim | p.ambi_delim)("left")
1859-
| Error("Expected a delimiter"))
1854+
r"\left" - (p.delim("left") | Error("Expected a delimiter"))
18601855
+ ZeroOrMore(p.simple | p.auto_delim)("mid")
1861-
+ r"\right"
1862-
- ((p.right_delim | p.ambi_delim)("right")
1863-
| Error("Expected a delimiter"))
1856+
+ r"\right" - (p.delim("right") | Error("Expected a delimiter"))
18641857
)
18651858

18661859
# Leaf definitions.
@@ -1982,7 +1975,7 @@ def symbol(self, s, loc, toks):
19821975
# Binary operators at start of string should not be spaced
19831976
if (c in self._binary_operators and
19841977
(len(s[:loc].split()) == 0 or prev_char == '{' or
1985-
prev_char in self._left_delim)):
1978+
prev_char in self._left_delims)):
19861979
return [char]
19871980
else:
19881981
return [Hlist([self._make_space(0.2),
@@ -2087,8 +2080,7 @@ def operatorname(self, s, loc, toks):
20872080
if isinstance(name, ParseResults):
20882081
next_char_loc += len('operatorname{}')
20892082
next_char = next((c for c in s[next_char_loc:] if c != ' '), '')
2090-
delimiters = self._left_delim | self._ambi_delim | self._right_delim
2091-
delimiters |= {'^', '_'}
2083+
delimiters = self._delims | {'^', '_'}
20922084
if (next_char not in delimiters and
20932085
name not in self._overunder_functions):
20942086
# Add thin space except when followed by parenthesis, bracket, etc.

lib/matplotlib/tests/test_mathtext.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,12 @@ def test_operator_space(fig_test, fig_ref):
344344
fig_ref.text(0.1, 0.8, r"$\mathrm{log}_2$")
345345

346346

347+
@check_figures_equal(extensions=["png"])
348+
def test_inverted_delimiters(fig_test, fig_ref):
349+
fig_test.text(.5, .5, r"$\left)\right($", math_fontfamily="dejavusans")
350+
fig_ref.text(.5, .5, r"$)($", math_fontfamily="dejavusans")
351+
352+
347353
def test_mathtext_fallback_valid():
348354
for fallback in ['cm', 'stix', 'stixsans', 'None']:
349355
mpl.rcParams['mathtext.fallback'] = fallback

0 commit comments

Comments
 (0)