Skip to content
Prev Previous commit
Next Next commit
Documentation update: I updated the dfrac_demo docstring to be a raw …
…string with the backslashes escaped.

Feature modification: I added in a dictionary to the Parser class of mathtex to link math style names to their TeX numerical values. I used a dictionary instead of hardcoding specific math style (ex. DISPLAYSTYLE=0) as I will use the dictionary in my implementation of the math styles in the future.
  • Loading branch information
Ryan Watkins committed Mar 2, 2017
commit 0d10078c59909858b8a674f290c6ba1b0027260d
16 changes: 8 additions & 8 deletions examples/text_labels_and_annotations/dfrac_demo.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
"""
=======================================
The difference between \dfrac and \frac
=======================================
r"""
=========================================
The difference between \\dfrac and \\frac
=========================================

In this example, the differences between the \dfrac and \frac TeX macros are
In this example, the differences between the \\dfrac and \\frac TeX macros are
illustrated; in particular, the difference between display style and text style
fractions when using Mathtex.
fractions when using Mathtex. aasdsads
Copy link
Copy Markdown
Contributor

@anntzer anntzer Apr 16, 2017

Choose a reason for hiding this comment

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

mathtext
remove whatever aasdsads is


.. versionadded:: 2.1

.. note::
To use \dfrac with the LaTeX engine (text.usetex : True), you need to
To use \\dfrac with the LaTeX engine (text.usetex : True), you need to
import the amsmath package with the text.latex.preamble rc, which is
an unsupported feature; therefore, it is probably a better idea to just
use the \displaystyle option before the \frac macro to get this behavior
use the \\displaystyle option before the \\frac macro to get this behavior
with the LaTeX engine.

"""
Expand Down
18 changes: 13 additions & 5 deletions lib/matplotlib/mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2224,6 +2224,10 @@ class Parser(object):
The grammar is based directly on that in TeX, though it cuts a few
corners.
"""

_math_style_dict = dict(displaystyle=0, textstyle=1,
scriptstyle=2, scriptscriptstyle=3)

_binary_operators = set('''
+ * -
\\pm \\sqcap \\rhd
Expand Down Expand Up @@ -3052,7 +3056,7 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
rule = float(rule)

# If style != displaystyle == 0, shrink the num and den
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think these warrant some constants instead of magic numbers.

if style:
if style != self._math_style_dict['displaystyle']:
num.shrink()
den.shrink()
cnum = HCentered([num])
Expand Down Expand Up @@ -3101,7 +3105,8 @@ def frac(self, s, loc, toks):
state.font, state.fontsize, state.dpi)
num, den = toks[0]

return self._genfrac('', '', thickness, 1, num, den)
return self._genfrac('', '', thickness,
self._math_style_dict['textstyle'], num, den)

def dfrac(self, s, loc, toks):
assert(len(toks) == 1)
Expand All @@ -3112,21 +3117,24 @@ def dfrac(self, s, loc, toks):
state.font, state.fontsize, state.dpi)
num, den = toks[0]

return self._genfrac('', '', thickness, 0, num, den)
return self._genfrac('', '', thickness,
self._math_style_dict['displaystyle'], num, den)

def stackrel(self, s, loc, toks):
assert(len(toks) == 1)
assert(len(toks[0]) == 2)
num, den = toks[0]

return self._genfrac('', '', 0.0, 1, num, den)
return self._genfrac('', '', 0.0,
self._math_style_dict['textstyle'], num, den)

def binom(self, s, loc, toks):
assert(len(toks) == 1)
assert(len(toks[0]) == 2)
num, den = toks[0]

return self._genfrac('(', ')', 0.0, 1, num, den)
return self._genfrac('(', ')', 0.0,
self._math_style_dict['textstyle'], num, den)

def sqrt(self, s, loc, toks):
#~ print "sqrt", toks
Expand Down