summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Teichmann <martin.teichmann@xfel.eu>2017-12-05 09:43:53 +0100
committerMartin Teichmann <martin.teichmann@xfel.eu>2017-12-05 09:43:53 +0100
commit82f662ff0ac2b8bb9ac2d24128923cc40a6d1b7e (patch)
treea9c98ad2f9eb2c9ab649534dd34ccf23a2f2f8fb
parent46c8ff45d503a0049e082c32c4032839018f3947 (diff)
downloadpint-82f662ff0ac2b8bb9ac2d24128923cc40a6d1b7e.tar.gz
respect default format also for LaTeX and HTML
when the user has set the default format to be short ("~"), one should expect that this also happens when formatting to LaTeX or HTML.
-rw-r--r--pint/quantity.py10
-rw-r--r--pint/testsuite/test_quantity.py5
-rw-r--r--pint/testsuite/test_unit.py4
-rw-r--r--pint/unit.py10
4 files changed, 25 insertions, 4 deletions
diff --git a/pint/quantity.py b/pint/quantity.py
index a3cb82e..6604ed4 100644
--- a/pint/quantity.py
+++ b/pint/quantity.py
@@ -219,10 +219,16 @@ class _Quantity(SharedRegistryObject):
# IPython related code
def _repr_html_(self):
- return self.__format__('H')
+ if "~" in self.default_format:
+ return "{:~H}".format(self)
+ else:
+ return "{:H}".format(self)
def _repr_latex_(self):
- return "$" + self.__format__('L') + "$"
+ if "~" in self.default_format:
+ return "${:~L}$".format(self)
+ else:
+ return "${:L}$".format(self)
@property
def magnitude(self):
diff --git a/pint/testsuite/test_quantity.py b/pint/testsuite/test_quantity.py
index a05387b..57b0c70 100644
--- a/pint/testsuite/test_quantity.py
+++ b/pint/testsuite/test_quantity.py
@@ -174,6 +174,11 @@ class TestQuantity(QuantityTestCase):
self.assertEqual(x._repr_latex_(),
r'$3.5\ \frac{\mathrm{kilogram} \cdot '
r'\mathrm{meter}^{2}}{\mathrm{second}}$')
+ ureg.default_format = "~"
+ self.assertEqual(x._repr_html_(), "3.5 kg m<sup>2</sup>/s")
+ self.assertEqual(x._repr_latex_(),
+ r'$3.5\ \frac{\mathrm{kg} \cdot '
+ r'\mathrm{m}^{2}}{\mathrm{s}}$')
def test_to_base_units(self):
x = self.Q_('1*inch')
diff --git a/pint/testsuite/test_unit.py b/pint/testsuite/test_unit.py
index 599bb62..11275c2 100644
--- a/pint/testsuite/test_unit.py
+++ b/pint/testsuite/test_unit.py
@@ -69,6 +69,10 @@ class TestUnit(QuantityTestCase):
self.assertEqual(x._repr_html_(), "kilogram meter<sup>2</sup>/second")
self.assertEqual(x._repr_latex_(), r'$\frac{\mathrm{kilogram} \cdot '
r'\mathrm{meter}^{2}}{\mathrm{second}}$')
+ ureg.default_format = "~"
+ self.assertEqual(x._repr_html_(), "kg m<sup>2</sup>/s")
+ self.assertEqual(x._repr_latex_(),
+ r'$\frac{\mathrm{kg} \cdot \mathrm{m}^{2}}{\mathrm{s}}$')
def test_unit_mul(self):
x = self.U_('m')
diff --git a/pint/unit.py b/pint/unit.py
index 894ba1d..c8a3495 100644
--- a/pint/unit.py
+++ b/pint/unit.py
@@ -112,10 +112,16 @@ class _Unit(SharedRegistryObject):
# IPython related code
def _repr_html_(self):
- return self.__format__('H')
+ if "~" in self.default_format:
+ return "{:~H}".format(self)
+ else:
+ return "{:H}".format(self)
def _repr_latex_(self):
- return "$" + self.__format__('L') + "$"
+ if "~" in self.default_format:
+ return "${:~L}$".format(self)
+ else:
+ return "${:L}$".format(self)
@property
def dimensionless(self):