Skip to content

Commit 4958414

Browse files
committed
unit test and updates to latex output
1 parent 1390579 commit 4958414

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

quantities/dimensionality.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def unicode(self):
5050

5151
@property
5252
def latex(self):
53-
return markup.format_units_latex(self,font="mathrm",mult="")
53+
return markup.format_units_latex(self.simplified,font='mathrm',mult=r'\cdot')
5454

5555
def __hash__(self):
5656
res = hash(unit_registry['dimensionless'])

quantities/markup.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self):
2929

3030
config = _Config()
3131

32-
superscripts = ['', '¹', '²', '³', '', '', '', '', '', '']
32+
superscripts = ['⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹']
3333

3434
def superscript(val):
3535
# TODO: use a regexp:
@@ -82,7 +82,7 @@ def format_units(udict):
8282
def format_units_unicode(udict):
8383
res = format_units(udict)
8484
res = superscript(res)
85-
res = res.replace('**', '^').replace('*','·')
85+
res = res.replace('**', '^').replace('*','·')
8686

8787
return res
8888

@@ -94,23 +94,26 @@ def format_units_latex(ustr,font='mathrm',mult=''):
9494
9595
Exponentiation (m**2) will be replaced with superscripts (m^{2})
9696
97+
The latex is set with the font argument, and the default is the normal,
98+
non-italicized font mathrm. Other useful options include 'mathnormal',
99+
'mathit', 'mathsf', and 'mathtt'.
100+
97101
Multiplication (*) are replaced with the symbol specified by the mult argument.
98102
By default this is a blank string (no multiplication symbol). Other useful
99103
options may be r'\cdot' or r'\*'
100104
101-
The latex is set with the font argument, and the default is the normal,
102-
non-italicized font mathrm. Other useful options include 'mathnormal',
103-
'mathit', 'mathsf', and 'mathtt'.
105+
Restrictions:
106+
With ambiguous units (having more than one division symbol), this routine
107+
will likely produce undesirable results. It is recommended that you first
108+
simplify the dimensionality before running it through this function.
104109
'''
105110
res = format_units(ustr)
106-
# Replace the first last parentheses with larger ones
107-
res = re.sub(r'^\(',r'\\left(',res)
108-
res = re.sub(r'\)$',r'\\right)',res)
109111
# Replace division (num/den) with \frac{num}{den}
110-
res = re.sub(r'(?P<num>\w+)/(?P<den>\w+)','\\\\frac{\g<num>}{\g<den>}',res)
112+
res = re.sub(r'(?P<num>.+)/(?P<den>.+)','\\\\frac{\g<num>}{\g<den>}',res)
111113
# Replace exponentiation (**exp) with ^{exp}
112-
res = re.sub(r'\*\*(?P<exp>\d+)',r'^{\g<exp>}',res)
114+
res = re.sub(r'\*{2,2}(?P<exp>\d+)',r'^{\g<exp>}',res)
113115
# Remove multiplication signs
114-
res = re.sub(r'\*',mult,res)
115-
return r'$\%s{%s}$' % (font,res)
116+
res = re.sub(r'\*','{'+mult+'}',res)
117+
res = r'$\%s{%s}$' % (font,res)
118+
return res
116119

quantities/tests/test_dimensionality.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
centimeter_str = 'cm'
1313
joule = Dimensionality({pq.kg: 1, pq.m: 2, pq.s: -2})
1414
joule_str = 'kg*m**2/s**2'
15-
joule_uni = 'kg·m²/s²'
15+
joule_uni = 'kg·m²/s²'
16+
joule_tex = r'$\mathrm{\frac{kg{\cdot}m^{2}}{s^{2}}}$'
1617
Joule = Dimensionality({pq.J: 1})
1718
Joule_str = 'J'
1819

@@ -23,6 +24,7 @@ def test_dimensionality_str(self):
2324
self.assertEqual(str(meter), meter_str)
2425
self.assertEqual(joule.string, joule_str)
2526
self.assertEqual(joule.unicode, joule_uni)
27+
self.assertEqual(joule.latex, joule_tex)
2628
self.assertEqual(Joule.string, 'J')
2729

2830
def test_equality(self):
@@ -136,6 +138,7 @@ def test_inplace_power(self):
136138
def test_simplification(self):
137139
self.assertEqual(Joule.simplified.string, 'kg*m**2/s**2')
138140
self.assertEqual(Joule.simplified, joule)
141+
139142

140143
def test_gt(self):
141144
self.assertTrue(joule > meter)

0 commit comments

Comments
 (0)