Skip to content

Commit c459dc5

Browse files
committed
added support for \operatorname (closes matplotlib#553)
I started with the version of the related code written by Freddie Witherden and committed to (the now abandoned?) mathtex spinoff project which didn't makes its way back to matplotlib. However, that code: http://code.google.com/p/mathtex/source/detail?r=75 allowed only to lower- and upper-case letters to be used inside of \operatorname. I have improved it to allow the usage of any simple character - including the insertion of spaces - such that $\operator{arg\,max}$ actually inserts a space between arg max
1 parent 2bb91ce commit c459dc5

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2011-10-25 added support for \operatorname to mathtext,
2+
including the ability to insert spaces, such as
3+
$\operatorname{arg\,max}$ - PI
4+
15
2011-08-18 Change api of Axes.get_tightbbox and add an optional
26
keyword parameter *call_axes_locator*. - JJL
37

lib/matplotlib/mathtext.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,6 +2269,12 @@ def __init__(self):
22692269
+ (group | Error("Expected \overline{value}"))
22702270
).setParseAction(self.overline).setName("overline")
22712271

2272+
operatorname = Group(
2273+
Suppress(Literal(r"\operatorname"))
2274+
+ ((start_group + ZeroOrMore(simple) + end_group)
2275+
| Error("Expected \operatorname{value}"))
2276+
).setParseAction(self.operatorname).setName("operatorname")
2277+
22722278
placeable <<(function
22732279
^ (c_over_c | symbol)
22742280
^ accent
@@ -2279,6 +2285,7 @@ def __init__(self):
22792285
^ genfrac
22802286
^ sqrt
22812287
^ overline
2288+
^ operatorname
22822289
)
22832290

22842291
simple <<(space
@@ -2578,6 +2585,18 @@ def function(self, s, loc, toks):
25782585
hlist.function_name = toks[0]
25792586
return hlist
25802587

2588+
def operatorname(self, s, loc, toks):
2589+
self.push_state()
2590+
state = self.get_state()
2591+
state.font = 'rm'
2592+
# Change the font of Chars, but leave Kerns alone
2593+
for c in toks[0]:
2594+
if isinstance(c,Char):
2595+
c.font = 'rm'
2596+
c._update_metrics()
2597+
self.pop_state()
2598+
return Hlist(toks[0])
2599+
25812600
def start_group(self, s, loc, toks):
25822601
self.push_state()
25832602
# Deal with LaTeX-style font tokens

0 commit comments

Comments
 (0)