Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mathtext: Add - to spaced symbols, and do not space symbols at start of string #5020

Merged
merged 3 commits into from Sep 8, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/matplotlib/mathtext.py
Expand Up @@ -2083,7 +2083,7 @@ class Parser(object):
corners.
"""
_binary_operators = set('''
+ *
+ * -
\\pm \\sqcap \\rhd
\\mp \\sqcup \\unlhd
\\times \\vee \\unrhd
Expand Down Expand Up @@ -2499,10 +2499,22 @@ def symbol(self, s, loc, toks):
raise ParseFatalException(s, loc, "Unknown symbol: %s" % c)

if c in self._spaced_symbols:
return [Hlist( [self._make_space(0.2),
char,
self._make_space(0.2)] ,
do_kern = True)]
# iterate until we find previous character, needed for cases
# such as ${ -2}$, $ -2$, or $ -2$.
i = 1
prev_char = s[loc-i]
while prev_char == ' ' and i <= loc:
i += 1
prev_char = s[loc-i]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be more pythonic to use six.xrange for this loop: i.e.:

for i in six.xrange(1, loc + 1):

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely, I wasn't very happy with that loop

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will need a break then:

for i in six.moves.xrange(1, loc + 1):
    prev_char = s[loc-i]
    if prev_char != ' ':
        break

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that's better.

# Binary operators at start of string should not be spaced
if (c in self._binary_operators and
(len(s[:loc].split()) == 0 or prev_char == '{')):
return [char]
else:
return [Hlist( [self._make_space(0.2),
char,
self._make_space(0.2)] ,
do_kern = True)]
elif c in self._punctuation_symbols:
return [Hlist( [char,
self._make_space(0.2)] ,
Expand Down
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.