@@ -29,7 +29,7 @@ def __init__(self):
2929
3030config = _Config ()
3131
32- superscripts = ['⁰ ' , '¹ ' , '² ' , '³ ' , '⁴ ' , '⁵ ' , '⁶ ' , '⁷ ' , '⁸ ' , '⁹ ' ]
32+ superscripts = ['⁰ ' , '¹ ' , '² ' , '³ ' , '⁴ ' , '⁵ ' , '⁶ ' , '⁷ ' , '⁸ ' , '⁹ ' ]
3333
3434def superscript (val ):
3535 # TODO: use a regexp:
@@ -82,7 +82,7 @@ def format_units(udict):
8282def 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
0 commit comments