Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ Bug Fixes

- ``astropy.units``

- Added a ``latex_inline`` unit format that returns the units in LaTeX math
notation with negative exponents instead of fractions [#2622].

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is under "Bug Fixes" but it should probably be under "New Features"


- ``astropy.utils``

- ``astropy.vo``
Expand Down
7 changes: 3 additions & 4 deletions astropy/units/format/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
from .cds import CDS
from .console import Console
from .fits import Fits
from .latex import Latex
from .latex import Latex, LatexInline
from .ogip import OGIP
from .unicode_format import Unicode
from .vounit import VOUnit

from ...extern.six import string_types

__all__ = [
'Base', 'Generic', 'CDS', 'Console', 'Fits', 'Latex', 'OGIP', 'Unicode',
'Unscaled', 'VOUnit', 'get_format']

'Base', 'Generic', 'CDS', 'Console', 'Fits', 'Latex', 'LatexInline',
'OGIP', 'Unicode', 'Unscaled', 'VOUnit', 'get_format']

def get_format(format=None):
"""
Expand Down
47 changes: 35 additions & 12 deletions astropy/units/format/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from . import base
from . import utils
from ...extern.six.moves import zip


class Latex(base.Base):
Expand Down Expand Up @@ -55,6 +56,23 @@ def _format_exponential_notation(self, val):

return r" \times ".join(parts)

def _format_bases(self, unit):
positives, negatives = utils.get_grouped_by_powers(
unit.bases, unit.powers)

if len(negatives):
if len(positives):
positives = self._format_unit_list(positives)
else:
positives = '1'
negatives = self._format_unit_list(negatives)
s = r'\frac{{{0}}}{{{1}}}'.format(positives, negatives)
else:
positives = self._format_unit_list(positives)
s = positives

return s

def to_string(self, unit):
from .. import core

Expand All @@ -71,19 +89,24 @@ def to_string(self, unit):
s = self._format_exponential_notation(unit.scale) + r'\,'

if len(unit.bases):
positives, negatives = utils.get_grouped_by_powers(
unit.bases, unit.powers)
if len(negatives):
if len(positives):
positives = self._format_unit_list(positives)
else:
positives = '1'
negatives = self._format_unit_list(negatives)
s += r'\frac{{{0}}}{{{1}}}'.format(positives, negatives)
else:
positives = self._format_unit_list(positives)
s += positives
s += self._format_bases(unit)

elif isinstance(unit, core.NamedUnit):
s = self._latex_escape(unit.name)

return r'$\mathrm{{{0}}}$'.format(s)

class LatexInline(Latex):
"""
Output LaTeX to display the unit based on IAU style guidelines with negative
powers.

Attempts to follow the `IAU Style Manual
<http://www.iau.org/static/publications/stylemanual1989.pdf>`_ and the
`ApJ and AJ style guide
<http://aas.org/authors/manuscript-preparation-aj-apj-author-instructions>`_.
"""
name = 'latex_inline'

def _format_bases(self, unit):
return self._format_unit_list(zip(unit.bases, unit.powers))
8 changes: 8 additions & 0 deletions astropy/units/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ def test_new_style_latex():
fluxunit = u.erg / (u.cm ** 2 * u.s)
assert "{0:latex}".format(fluxunit) == r'$\mathrm{\frac{erg}{s\,cm^{2}}}$'

def test_latex_scale():
latex = '$\\mathrm{2.1798721 \\times 10^{-18}\\,\\frac{m^{2}\\,kg}{s^{2}}}$'
assert u.Ry.decompose().to_string('latex') == latex

def test_latex_inline_scale():
latex_inline = '$\\mathrm{2.1798721 \\times 10^{-18}\\,m^{2}\\,kg\\,s^{-2}}$'
assert u.Ry.decompose().to_string('latex_inline') == latex_inline

def test_format_styles():
fluxunit = u.erg / (u.cm ** 2 * u.s)
Expand All @@ -274,6 +281,7 @@ def _test_format_styles(format_spec, s):
('s', 'erg / (cm2 s)'),
('console', ' erg \n ------\n s cm^2'),
('latex', '$\\mathrm{\\frac{erg}{s\\,cm^{2}}}$'),
('latex_inline', '$\\mathrm{erg\\,s^{-1}\\,cm^{-2}}$'),
('>20s',' erg / (cm2 s)'),
]

Expand Down
14 changes: 14 additions & 0 deletions docs/units/format.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ following formats:

\mathrm{\frac{erg}{s\,cm^{2}}}

- ``"latex_inline"``: Writes units out using LaTeX math syntax using the
`IAU Style Manual
<http://www.iau.org/static/publications/stylemanual1989.pdf>`__
recommendations for unit presentation, using negative powers instead of
fractions, as required by some journals (e.g., `Apj and AJ
<http://aas.org/authors/manuscript-preparation-aj-apj-author-instructions#_Toc2.2>`_.)
Best suited for unit representation inline with text::

>>> fluxunit.to_string('latex_inline') # doctest: +SKIP

.. math::

\mathrm{erg\,s^{-1}\,cm^{-2}}

- ``"console"``: Writes a multi-line representation of the unit
useful for display in a text console::

Expand Down