Skip to content

Commit b49c314

Browse files
committed
Add mathtext support for \middle
1 parent 9b1fcf6 commit b49c314

File tree

6 files changed

+49
-10
lines changed

6 files changed

+49
-10
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
``mathtext`` supports ``\middle``
2+
---------------------------------
3+
4+
The ``\middle`` latex command is now supported by `.mathtext`. It is a
5+
complement to ``\left`` and ``\right`` and used to add a middle sized
6+
separator. Currently, only a single ``\middle`` between ``\left`` and
7+
``\right`` is supported.
8+
9+
.. plot::
10+
:include-source: true
11+
12+
import matplotlib.pyplot as plt
13+
plt.figure(figsize=(2, 1))
14+
plt.figtext(
15+
0.05, 0.55,
16+
r"$\left\{\sum_{i=0}^x a_i \middle | x \in \mathbb{N}\right\}$",
17+
size=20, math_fontfamily='cm')

lib/matplotlib/_mathtext.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,7 +1950,10 @@ def csnames(group, names):
19501950

19511951
p.auto_delim <<= (
19521952
r"\left" - (p.delim("left") | Error("Expected a delimiter"))
1953-
+ ZeroOrMore(p.simple | p.auto_delim)("mid")
1953+
+ ZeroOrMore(p.simple | p.auto_delim)("mid1")
1954+
+ Optional(r"\middle" - (p.delim("mid") |
1955+
Error("Expected a delimiter")))("middle")
1956+
+ ZeroOrMore(p.simple | p.auto_delim)("mid2")
19541957
+ r"\right" - (p.delim("right") | Error("Expected a delimiter"))
19551958
)
19561959

@@ -2560,11 +2563,14 @@ def overline(self, s, loc, toks):
25602563
hlist = Hlist([rightside])
25612564
return [hlist]
25622565

2563-
def _auto_sized_delimiter(self, front, middle, back):
2566+
def _auto_sized_delimiter(self, front, mid1, back, middle=".", mid2=None):
2567+
if mid2 is None:
2568+
mid2 = []
25642569
state = self.get_state()
2565-
if len(middle):
2566-
height = max(x.height for x in middle)
2567-
depth = max(x.depth for x in middle)
2570+
content = mid1 + mid2
2571+
if len(content):
2572+
height = max(x.height for x in content)
2573+
depth = max(x.depth for x in content)
25682574
factor = None
25692575
else:
25702576
height = 0
@@ -2575,7 +2581,11 @@ def _auto_sized_delimiter(self, front, middle, back):
25752581
if front != '.':
25762582
parts.append(
25772583
AutoHeightChar(front, height, depth, state, factor=factor))
2578-
parts.extend(middle)
2584+
parts.extend(mid1)
2585+
if middle != ".":
2586+
parts.append(
2587+
AutoHeightChar(middle, height, depth, state, factor=factor))
2588+
parts.extend(mid2)
25792589
if back != '.':
25802590
parts.append(
25812591
AutoHeightChar(back, height, depth, state, factor=factor))
@@ -2585,6 +2595,9 @@ def _auto_sized_delimiter(self, front, middle, back):
25852595
def auto_delim(self, s, loc, toks):
25862596
return self._auto_sized_delimiter(
25872597
toks["left"],
2588-
# if "mid" in toks ... can be removed when requiring pyparsing 3.
2589-
toks["mid"].asList() if "mid" in toks else [],
2590-
toks["right"])
2598+
# if "mid*" in toks ... can be removed when requiring pyparsing 3.
2599+
toks["mid1"].asList() if "mid1" in toks else [],
2600+
toks["right"],
2601+
toks["mid"] if "mid" in toks else ".",
2602+
toks["mid2"].asList() if "mid2" in toks else []
2603+
)

lib/matplotlib/_mathtext_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@
526526
'succnsim' : 8937,
527527
'gimel' : 8503,
528528
'vert' : 124,
529-
'|' : 124,
529+
'|' : 8214,
530530
'varrho' : 1009,
531531
'P' : 182,
532532
'approxident' : 8779,
Loading

lib/matplotlib/tests/test_mathtext.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
r'$x \overset{f}{\rightarrow} \overset{f}{x} \underset{xx}{ff} \overset{xx}{ff} \underset{f}{x} \underset{f}{\leftarrow} x$', # github issue #18241
130130
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
131131
r'$1.$ $2.$ $19680801.$ $a.$ $b.$ $mpl.$',
132+
r'$\left(\frac{x_i}{\sum x_i}\middle| x_i \in \mathcal{S}\right)$'
132133
]
133134

134135
digits = "0123456789"

tutorials/text/mathtext.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@
124124
125125
\left(\frac{5 - \frac{1}{x}}{4}\right)
126126
127+
There is also a ``\middle`` version for using with a middle delimiter.::
128+
129+
r'$\left\{x \rightarrow \frac{1}{x} \middle| x \in \mathbb{N}^+ \right\}$'
130+
131+
.. math::
132+
133+
\left\{x \rightarrow \frac{1}{x} \middle| x \in \mathbb{N}^+ \right\}
134+
127135
Radicals
128136
--------
129137
Radicals can be produced with the ``\sqrt[]{}`` command. For example::

0 commit comments

Comments
 (0)