Skip to content

Commit 9baf3bc

Browse files
committed
parentheses in latex are off by default. added latex information to tutorial
1 parent f9c43c2 commit 9baf3bc

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

quantities/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,27 @@
213213
>>> print pq.constants.proton_mass.simplified
214214
1.672621637e-27 kg
215215
+/-8.3e-35 kg (1 sigma)
216+
217+
A Latex representation of the dimensionality may be obtained in the following fashion::
218+
219+
>>> g = pq.Quantity(9.80665,'m/s**2')
220+
>>> mass = 50 * pq.kg
221+
>>> weight = mass*g
222+
>>> print weight.dimensionality.latex
223+
$\mathrm{\frac{kg{\cdot}m}{s^{2}}}$
224+
>>> weight.units = pq.N
225+
>>> print weight.dimensionality.latex
226+
$\mathrm{N}$
227+
228+
The Latex output is compliant with the MathText subset used by Matplotlib. To add
229+
formatted units to the axis label of a Matplotlib figure, one could use::
230+
231+
>>> ax.set_ylabel('Weight ' + weight.dimensionality.latex)
232+
233+
Greater customization is available via the markup.format_units_latex function. It allows
234+
the user to modify the font, the multiplication symbol, or to encapsulate the latex
235+
string in parentheses. Due to the complexity of CompoundUnits, the latex rendering
236+
of CompoundUnits will utilize the latex \frac{num}{den} construct.
216237
217238
Although it is not illustrated in this guide, unicode symbols can be
218239
used to provide a more compact representation of the units. This

quantities/markup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def format_units_unicode(udict):
8787
return res
8888

8989

90-
def format_units_latex(udict,font='mathrm',mult=r'\cdot',paren=True):
90+
def format_units_latex(udict,font='mathrm',mult=r'\cdot',paren=False):
9191
'''
9292
Replace the units string provided with an equivalent latex string.
9393
@@ -100,8 +100,8 @@ def format_units_latex(udict,font='mathrm',mult=r'\cdot',paren=True):
100100
'mathit', 'mathsf', and 'mathtt'.
101101
102102
Multiplication (*) are replaced with the symbol specified by the mult argument.
103-
By default this is a blank string (no multiplication symbol). Other useful
104-
options may be r'\cdot' or r'*'
103+
By default this is the latex \cdot symbol. Other useful
104+
options may be '' or '*'.
105105
106106
If paren=True, encapsulate the string in '\left(' and '\right)'
107107

0 commit comments

Comments
 (0)