Skip to content

Commit c45678e

Browse files
watkinrtQuLogic
authored andcommitted
Issue #1888: added in the \dfrac macro for displaystyle fractions (#8151)
* Feature addition: I added in the the \dfrac macro (displaystyle fraction) to the mathtex implementation. * PEP8 fixes * Documentation: Updated the docstring for the dfrac_demo Bug fix: I added a plt.show() to the end of the dfrac_demo Tests: I added in a new test for the dfrac macro (along with the corresponding comparison files) within the test_mathtex test suite. * 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. * Although Sphyinx requires "\\" to show "\" characters, I removed them as matplotlib doesn't currently support this behavior in docstrings. * Remove stray text from documentation. * With the resent merging of the sphinx-gallery branch, I went through and updated the docstring to appropriately escape the backslashes. * TST: add missing tests
1 parent c683c3e commit c45678e

18 files changed

+927
-13
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
r"""
2+
=========================================
3+
The difference between \\dfrac and \\frac
4+
=========================================
5+
6+
In this example, the differences between the \\dfrac and \\frac TeX macros are
7+
illustrated; in particular, the difference between display style and text style
8+
fractions when using Mathtex.
9+
10+
.. versionadded:: 2.1
11+
12+
.. note::
13+
To use \\dfrac with the LaTeX engine (text.usetex : True), you need to
14+
import the amsmath package with the text.latex.preamble rc, which is
15+
an unsupported feature; therefore, it is probably a better idea to just
16+
use the \\displaystyle option before the \\frac macro to get this behavior
17+
with the LaTeX engine.
18+
19+
"""
20+
21+
import matplotlib.pyplot as plt
22+
23+
fig = plt.figure(figsize=(5.25, 0.75))
24+
fig.text(0.5, 0.3, r'\dfrac: $\dfrac{a}{b}$',
25+
horizontalalignment='center', verticalalignment='center')
26+
fig.text(0.5, 0.7, r'\frac: $\frac{a}{b}$',
27+
horizontalalignment='center', verticalalignment='center')
28+
plt.show()

lib/matplotlib/mathtext.py

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,6 +2216,10 @@ class Parser(object):
22162216
The grammar is based directly on that in TeX, though it cuts a few
22172217
corners.
22182218
"""
2219+
2220+
_math_style_dict = dict(displaystyle=0, textstyle=1,
2221+
scriptstyle=2, scriptscriptstyle=3)
2222+
22192223
_binary_operators = set('''
22202224
+ * -
22212225
\\pm \\sqcap \\rhd
@@ -2301,6 +2305,7 @@ def __init__(self):
23012305
p.float_literal = Forward()
23022306
p.font = Forward()
23032307
p.frac = Forward()
2308+
p.dfrac = Forward()
23042309
p.function = Forward()
23052310
p.genfrac = Forward()
23062311
p.group = Forward()
@@ -2389,6 +2394,11 @@ def __init__(self):
23892394
- ((p.required_group + p.required_group) | Error(r"Expected \frac{num}{den}"))
23902395
)
23912396

2397+
p.dfrac <<= Group(
2398+
Suppress(Literal(r"\dfrac"))
2399+
- ((p.required_group + p.required_group) | Error(r"Expected \dfrac{num}{den}"))
2400+
)
2401+
23922402
p.stackrel <<= Group(
23932403
Suppress(Literal(r"\stackrel"))
23942404
- ((p.required_group + p.required_group) | Error(r"Expected \stackrel{num}{den}"))
@@ -2441,6 +2451,7 @@ def __init__(self):
24412451
| p.function
24422452
| p.group
24432453
| p.frac
2454+
| p.dfrac
24442455
| p.stackrel
24452456
| p.binom
24462457
| p.genfrac
@@ -3035,8 +3046,11 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
30353046
state.font, state.fontsize, state.dpi)
30363047

30373048
rule = float(rule)
3038-
num.shrink()
3039-
den.shrink()
3049+
3050+
# If style != displaystyle == 0, shrink the num and den
3051+
if style != self._math_style_dict['displaystyle']:
3052+
num.shrink()
3053+
den.shrink()
30403054
cnum = HCentered([num])
30413055
cden = HCentered([den])
30423056
width = max(num.width, den.width)
@@ -3069,35 +3083,50 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
30693083
return result
30703084

30713085
def genfrac(self, s, loc, toks):
3072-
assert(len(toks)==1)
3073-
assert(len(toks[0])==6)
3086+
assert(len(toks) == 1)
3087+
assert(len(toks[0]) == 6)
30743088

30753089
return self._genfrac(*tuple(toks[0]))
30763090

30773091
def frac(self, s, loc, toks):
3078-
assert(len(toks)==1)
3079-
assert(len(toks[0])==2)
3092+
assert(len(toks) == 1)
3093+
assert(len(toks[0]) == 2)
3094+
state = self.get_state()
3095+
3096+
thickness = state.font_output.get_underline_thickness(
3097+
state.font, state.fontsize, state.dpi)
3098+
num, den = toks[0]
3099+
3100+
return self._genfrac('', '', thickness,
3101+
self._math_style_dict['textstyle'], num, den)
3102+
3103+
def dfrac(self, s, loc, toks):
3104+
assert(len(toks) == 1)
3105+
assert(len(toks[0]) == 2)
30803106
state = self.get_state()
30813107

30823108
thickness = state.font_output.get_underline_thickness(
30833109
state.font, state.fontsize, state.dpi)
30843110
num, den = toks[0]
30853111

3086-
return self._genfrac('', '', thickness, '', num, den)
3112+
return self._genfrac('', '', thickness,
3113+
self._math_style_dict['displaystyle'], num, den)
30873114

30883115
def stackrel(self, s, loc, toks):
3089-
assert(len(toks)==1)
3090-
assert(len(toks[0])==2)
3116+
assert(len(toks) == 1)
3117+
assert(len(toks[0]) == 2)
30913118
num, den = toks[0]
30923119

3093-
return self._genfrac('', '', 0.0, '', num, den)
3120+
return self._genfrac('', '', 0.0,
3121+
self._math_style_dict['textstyle'], num, den)
30943122

30953123
def binom(self, s, loc, toks):
3096-
assert(len(toks)==1)
3097-
assert(len(toks[0])==2)
3124+
assert(len(toks) == 1)
3125+
assert(len(toks[0]) == 2)
30983126
num, den = toks[0]
30993127

3100-
return self._genfrac('(', ')', 0.0, '', num, den)
3128+
return self._genfrac('(', ')', 0.0,
3129+
self._math_style_dict['textstyle'], num, den)
31013130

31023131
def sqrt(self, s, loc, toks):
31033132
#~ print "sqrt", toks
Binary file not shown.
1.76 KB
Loading
Lines changed: 231 additions & 0 deletions
Loading
Binary file not shown.
1.85 KB
Loading

0 commit comments

Comments
 (0)