Skip to content

Commit f55733e

Browse files
committed
mathtext now supports \text
1 parent c8aaa90 commit f55733e

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
``mathtext`` now supports ``\text``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
``\text`` can be used to obtain upright text within an equation.
5+
6+
.. plot::
7+
:include-source: true
8+
:alt: Illustration of the newly added \text command, showing that it renders as normal text, including spaces, despite being part of an equation. Also show that a dash is not rendered as a minus when part of a \text command.
9+
10+
import matplotlib.pyplot as plt
11+
plt.text(0.1, 0.5, r"$a = \sin(\phi) \text{ such that } \phi = \frac{x}{y}$")
12+
plt.text(0.1, 0.3, r"$\text{dashes (-) are retained}$")

galleries/users_explain/text/mathtext.py

+4
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@
272272
that far fewer symbols will be available, but it can be useful to make math
273273
expressions blend well with other text in the plot.
274274
275+
For compatibility with popular packages, ``\text{...}`` is available and uses the
276+
``\mathrm{...}`` font, but otherwise retains spaces and renders - as a dash
277+
(not minus).
278+
275279
Custom fonts
276280
~~~~~~~~~~~~
277281
mathtext also provides a way to use custom fonts for math. This method is

lib/matplotlib/_mathtext.py

+12
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,14 @@ 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+
hlist = Hlist([Char(c, state) for c in toks[1]])
2034+
self.pop_state()
2035+
return [hlist]
2036+
20252037
def _make_space(self, percentage):
20262038
# In TeX, an em (the unit usually used to measure horizontal lengths)
20272039
# is not the width of the character 'm'; it is the same in different

lib/matplotlib/tests/test_mathtext.py

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@
130130
r'$x \overset{f}{\rightarrow} \overset{f}{x} \underset{xx}{ff} \overset{xx}{ff} \underset{f}{x} \underset{f}{\leftarrow} x$', # github issue #18241
131131
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
132132
r'$1.$ $2.$ $19680801.$ $a.$ $b.$ $mpl.$',
133+
r'$\text{text}_{\text{sub}}^{\text{sup}} + \text{\$foo\$} + \frac{\text{num}}{\mathbf{\text{den}}}\text{with space, curly brackets \{\}, and dash -}$',
134+
133135
]
134136

135137
digits = "0123456789"

0 commit comments

Comments
 (0)