Skip to content

Commit 8f44615

Browse files
authored
Allow named units to have a LaTeX representation (WIP) (#1069)
* Allow named units to have a LaTeX representation (WIP) * Add some LaTeX representations and a test for this
1 parent 06ef418 commit 8f44615

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/amuse/test/suite/core_tests/test_console.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,7 @@ def test10(self):
242242
"], ["+tmp+", "+tmp+"]] au")
243243
self.assertEqual(str(pi), "3.14 none")
244244
set_printing_strategy("default")
245+
246+
def test_latex(self):
247+
self.assertEqual(str(units.MSun), "MSun")
248+
self.assertEqual(str(units.MSun.latex()), r"M_{\odot}")

src/amuse/units/core.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class unit(object):
6464
6565
Units can also be named, by creating a named unit.
6666
"""
67+
68+
_latex_representation = None
6769
__array_priority__ = 100
6870

6971
def __mul__(self, other):
@@ -142,6 +144,12 @@ def __ne__(self, other):
142144
def __hash__(self):
143145
return self._hash
144146

147+
def latex(self):
148+
if self._latex_representation is not None:
149+
return self._latex_representation
150+
else:
151+
return self.symbol
152+
145153
@late
146154
def _hash(self):
147155
return hash(id(self))
@@ -853,15 +861,16 @@ class named_unit(unit):
853861
>>> (20.0 | (60.0 * si.s)).as_quantity_in(minute)
854862
quantity<20.0 min>
855863
"""
856-
def __init__(self, name, symbol, unit):
864+
865+
def __init__(self, name, symbol, unit, latex=None):
857866
self.name = name
858867
self.symbol = symbol
859868
self.local_unit = unit
869+
self._latex_representation = latex
860870

861871
def __str__(self):
862872
return self.symbol
863873

864-
865874
def reference_string(self):
866875
return self.to_simple_form().reference_string()
867876

src/amuse/units/units.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
Mpc = named("mega parsec", "Mpc", 10**6 * parsec)
5151
Gpc = named("giga parsec", "Gpc", 10**9 * parsec)
5252
lightyear = named("light year", "ly", 9460730472580.8 * km)
53-
LSun = named("solar luminosity", "LSun", 3.839e26 * W)
54-
MSun = named("solar mass", "MSun", 1.98892e30 * kg)
55-
RSun = named("solar radius", "RSun", 6.955e8 * m)
53+
LSun = named("solar luminosity", "LSun", 3.839e26 * W, latex=r"L_{\odot}")
54+
MSun = named("solar mass", "MSun", 1.98892e30 * kg, latex=r"M_{\odot}")
55+
RSun = named("solar radius", "RSun", 6.955e8 * m, latex=r"R_{\odot}")
5656
MJupiter = named("jupiter mass", "MJupiter", 1.8987e27 * kg)
5757
RJupiter = named("jupiter radius", "RJupiter", 71492.0 * km)
5858
MEarth = named("earth mass", "MEarth", 5.9722e24 * kg)

0 commit comments

Comments
 (0)