8000 mathtext now supports \text · matplotlib/matplotlib@f31b488 · GitHub
[go: up one dir, main page]

Skip to content

Commit f31b488

Browse files
committed
mathtext now supports \text
1 parent 73394f2 commit f31b488

18 files changed

+3480
-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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,7 @@ def csnames(group, names):
18571857
p.optional_group = Forward()
18581858
p.sqrt = Forward()
18591859
p.subsuper = Forward()
1860+
p.text = Forward()
18601861
p.token = Forward()
18611862
p.underset = Forward()
18621863

@@ -1907,6 +1908,8 @@ def csnames(group, names):
19071908
r"\underset",
19081909
p.optional_group("annotation") + p.optional_group("body"))
19091910

1911+
p.text <<= cmd(r"\text", QuotedString('{', '\\', endQuoteChar="}"))
1912+
19101913
p.placeable <<= (
19111914
p.accent # Must be before symbol as all accents are symbols
19121915
| p.symbol # Must be second to catch all named symbols and single
@@ -1922,6 +1925,7 @@ def csnames(group, names):
19221925
| p.underset
19231926
| p.sqrt
19241927
| p.overline
1928+
| p.text
19251929
)
19261930

19271931
p.simple <<= (
@@ -2022,6 +2026,15 @@ def non_math(self, s, loc, toks):
20222026

20232027
float_literal = staticmethod(pyparsing_common.convertToFloat)
20242028

2029+
def text(self, s, loc, toks):
2030+
self.push_state()
2031+
state = self.get_state()
2032+
state.font = 'rm'
2033+
s = toks[1].replace(r'\$', '$').replace(r'\{', '{').replace(r'\}', '}')
2034+
hlist = Hlist([Char(c, st 832E ate) for c in s])
2035+
self.pop_state()
2036+
return [hlist]
2037+
20252038
def _make_space(self, percentage):
20262039
# In TeX, an em (the unit usually used to measure horizontal lengths)
20272040
# is not the width of the character 'm'; it is the same in different
Binary file not shown.
Loading

0 commit comments

Comments
 (0)
0