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

Skip to content

Commit 7c6ac68

Browse files
committed
mathtext now supports text
1 parent 612cfae commit 7c6ac68

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
``mathtext`` now supports ``\text``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
``\text`` can be used to obtain upright text within an equation.
5+
6+
.. code-block:: python
7+
8+
import matplotlib.pyplot as plt
9+
plt.text(0.1, 0.5, r"$a = \sin(\phi) \text{ such that } \phi = \frac{x}{y}$")
10+
plt.draw()

lib/matplotlib/_mathtext.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,7 @@ def __init__(self):
19931993
p.subsuperop = Forward()
19941994
p.symbol = Forward()
19951995
p.symbol_name = Forward()
1996+
p.text = Forward()
19961997
p.token = Forward()
19971998
p.underset = Forward()
19981999
p.unknown_symbol = Forward()
@@ -2132,6 +2133,11 @@ def __init__(self):
21322133
| Error("Expected \\operatorname{value}"))
21332134
)
21342135

2136+
p.text <<= Group(
2137+
Suppress(Literal(r"\text"))
2138+
- QuotedString('{', '\\', endQuoteChar="}")
2139+
)
2140+
21352141
p.placeable <<= (
21362142
p.accentprefixed # Must be before accent so named symbols that are
21372143
# prefixed with an accent name work
@@ -2150,6 +2156,7 @@ def __init__(self):
21502156
| p.sqrt
21512157
| p.overline
21522158
| p.operatorname
2159+
| p.text
21532160
)
21542161

21552162
p.simple <<= (
@@ -2294,6 +2301,18 @@ def non_math(self, s, loc, toks):
22942301
self.get_state().font = mpl.rcParams['mathtext.default']
22952302
return [hlist]
22962303

2304+
def text(self, s, loc, toks):
2305+
self.pop_state()
2306+
s = toks[0][0].replace(r'\$', '$')
2307+
s = s.replace(r'\{', '{')
2308+
s = s.replace(r'\}', '}')
2309+
symbols = [Char(c, self.get_state(), math=False) for c in s]
2310+
hlist = Hlist(symbols)
2311+
# We're going into math now, so set font to 'it'
2312+
self.push_state()
2313+
self.get_state().font = mpl.rcParams['mathtext.default']
2314+
return [hlist]
2315+
22972316
def _make_space(self, percentage):
22982317
# All spaces are relative to em width
22992318
state = self.get_state()

0 commit comments

Comments
 (0)