Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

mathtext \left and \right delimiters not working #715

Merged
merged 2 commits into from

2 participants

@moble

Mathtext doesn't seem to understand all delimiters when using \left and \right. For example, I can do

import matplotlib.pyplot as plt
plt.plot([0,1,4,9])
plt.ylabel(r'$\left\{ \frac{a}{b} \right\}$')
plt.show()

The result is a big ugly traceback ending in

matplotlib.pyparsing.ParseFatalException: Expected a delimiter
$\left\{ \frac{a}{b} \right\}$ (at char 0), (line:1, col:1)

Using \left{ does work (but that's incorrect TeX), while \left[ doesn't work either. When I set

matplotlib.rcParams['text.usetex'] = True

Everything works as expected. But I'd prefer to be able to use mathtext.

I am using matplotlib version 1.1.0 with python 2.7.2 on Mac OS X 10.6.8. I tried this on a new user account with no matplotlibrc or pythonrc. My default backend is MacOSX, but I get the same thing with Agg.

@mdboom mdboom Fix a couple of problems with auto-sized delimiters:
  \left{ should be \left\{ etc.

  If the delimiters have no content, they were getting shrinked down to nothing.
5add058
@mdboom
Owner

Does the attached pull request resolve the issue for you?

@moble

Almost. It gets the curly braces working, but square brackets should not have a backslash in front of them. (With the backslashes, that's actually an old-fashioned TeX command that has no place in labels.) So if I take those backslashes out of your lines 2125 and 2127, it works. Thanks!

[Sorry if I should be doing something fancier here -- like adding my own diff. I'm not new to git, but I am new to github.]

@mdboom
Owner

Oh, yes... just an oversight. Will fix and then merge.

@mdboom mdboom merged commit bf81821 into matplotlib:master
@mdboom mdboom deleted the mdboom:mathtext-delimiters branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 24, 2012
  1. @mdboom

    Fix a couple of problems with auto-sized delimiters:

    mdboom authored
      \left{ should be \left\{ etc.
    
      If the delimiters have no content, they were getting shrinked down to nothing.
Commits on Feb 27, 2012
  1. @mdboom

    Fix auto-sized square brackets

    mdboom authored
This page is out of date. Refresh to see the latest.
Showing with 17 additions and 9 deletions.
  1. +17 −9 lib/matplotlib/mathtext.py
View
26 lib/matplotlib/mathtext.py
@@ -771,7 +771,11 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
('\leftbrace', '{'),
('\rightbrace', '}'),
('\leftbracket', '['),
- ('\rightbracket', ']')]:
+ ('\rightbracket', ']'),
+ (r'\{', '{'),
+ (r'\}', '}'),
+ (r'\[', '['),
+ (r'\]', ']')]:
_size_alternatives[alias] = _size_alternatives[target]
def get_sized_alternatives_for_symbol(self, fontname, sym):
@@ -970,6 +974,9 @@ def _map_virtual_font(self, fontname, font_class, uniindex):
_size_alternatives = {}
def get_sized_alternatives_for_symbol(self, fontname, sym):
+ fixes = {'\{': '{', '\}': '}', '\[': '[', '\]': ']'}
+ sym = fixes.get(sym, sym)
+
alternatives = self._size_alternatives.get(sym)
if alternatives:
return alternatives
@@ -1829,7 +1836,7 @@ class AutoHeightChar(Hlist):
fonts), the correct glyph will be selected, otherwise this will
always just return a scaled version of the glyph.
"""
- def __init__(self, c, height, depth, state, always=False):
+ def __init__(self, c, height, depth, state, always=False, factor=None):
alternatives = state.font_output.get_sized_alternatives_for_symbol(
state.font, c)
@@ -1841,7 +1848,8 @@ def __init__(self, c, height, depth, state, always=False):
if char.height + char.depth >= target_total:
break
- factor = target_total / (char.height + char.depth)
+ if factor is None:
+ factor = target_total / (char.height + char.depth)
state.fontsize *= factor
char = Char(sym, state)
@@ -2114,9 +2122,9 @@ class Parser(object):
| \| / \backslash \uparrow \downarrow \updownarrow \Uparrow
\Downarrow \Updownarrow .""".split())
- _left_delim = set(r"( [ { < \lfloor \langle \lceil".split())
+ _left_delim = set(r"( [ \{ < \lfloor \langle \lceil".split())
- _right_delim = set(r") ] } > \rfloor \rangle \rceil".split())
+ _right_delim = set(r") ] \} > \rfloor \rangle \rceil".split())
def __init__(self):
# All forward declarations are here
@@ -2772,8 +2780,6 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
ldelim = '.'
if rdelim == '':
rdelim = '.'
- elif rdelim == r'\}':
- rdelim = '}'
return self._auto_sized_delimiter(ldelim, result, rdelim)
return result
@@ -2883,16 +2889,18 @@ def _auto_sized_delimiter(self, front, middle, back):
if len(middle):
height = max([x.height for x in middle])
depth = max([x.depth for x in middle])
+ factor = None
else:
height = 0
depth = 0
+ factor = 1.0
parts = []
# \left. and \right. aren't supposed to produce any symbols
if front != '.':
- parts.append(AutoHeightChar(front, height, depth, state))
+ parts.append(AutoHeightChar(front, height, depth, state, factor=factor))
parts.extend(middle)
if back != '.':
- parts.append(AutoHeightChar(back, height, depth, state))
+ parts.append(AutoHeightChar(back, height, depth, state, factor=factor))
hlist = Hlist(parts)
return hlist
Something went wrong with that request. Please try again.