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

Skip to content

Commit debb5fc

Browse files
committed
add - to binary operators, do not space binary operators at start of string
1 parent 7c1993d commit debb5fc

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

lib/matplotlib/mathtext.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,7 +2083,7 @@ class Parser(object):
20832083
corners.
20842084
"""
20852085
_binary_operators = set('''
2086-
+ *
2086+
+ * -
20872087
\\pm \\sqcap \\rhd
20882088
\\mp \\sqcup \\unlhd
20892089
\\times \\vee \\unrhd
@@ -2499,10 +2499,22 @@ def symbol(self, s, loc, toks):
24992499
raise ParseFatalException(s, loc, "Unknown symbol: %s" % c)
25002500

25012501
if c in self._spaced_symbols:
2502-
return [Hlist( [self._make_space(0.2),
2503-
char,
2504-
self._make_space(0.2)] ,
2505-
do_kern = True)]
2502+
# iterate until we find previous character, needed for cases
2503+
# such as ${ -2}$, $ -2$, or $ -2$.
2504+
i = 1
2505+
prev_char = s[loc-i]
2506+
while prev_char == ' ' and i <= loc:
2507+
i += 1
2508+
prev_char = s[loc-i]
2509+
# Binary operators at start of string should not be spaced
2510+
if (c in self._binary_operators and
2511+
(len(s[:loc].split()) == 0 or prev_char == '{')):
2512+
return [char]
2513+
else:
2514+
return [Hlist( [self._make_space(0.2),
2515+
char,
2516+
self._make_space(0.2)] ,
2517+
do_kern = True)]
25062518
elif c in self._punctuation_symbols:
25072519
return [Hlist( [char,
25082520
self._make_space(0.2)] ,

lib/matplotlib/tests/test_mathtext.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@
102102
r'${xyz}^k{x}_{k}{x}^{p}{y}^{p-2} {d}_{i}^{j}{b}_{j}{c}_{k}{d} {x}^{j}_{i}{E}^{0}{E}^0_u$',
103103
r'${\int}_x^x x\oint_x^x x\int_{X}^{X}x\int_x x \int^x x \int_{x} x\int^{x}{\int}_{x} x{\int}^{x}_{x}x$',
104104
r'testing$^{123}$',
105-
' '.join('$\\' + p + '$' for p in sorted(mathtext.Parser._snowflake))
105+
' '.join('$\\' + p + '$' for p in sorted(mathtext.Parser._snowflake)),
106+
r'$6-2$; $-2$; $ -2$; ${-2}$; ${ -2}$; $20^{+3}_{-2}$',
106107
]
107108

108109
digits = "0123456789"

0 commit comments

Comments
 (0)