Skip to content

Commit

Permalink
Quantities: Add a fix for the Celsius
Browse files Browse the repository at this point in the history
  • Loading branch information
JelteF committed Aug 17, 2016
1 parent 292be93 commit 594911b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Fixed
- Setting the ``lmodern`` keyword argument of `.Document` to false will not
cause invalid LaTeX code anymore.
- `.Quantity` now correctly splits prefix and unit into seperate commands.
- `.Quantity` can now handle Celsius.


1.0.0_ - `docs <../v1.0.0/>`__ - 2015-11-25
Expand Down
27 changes: 23 additions & 4 deletions pylatex/quantities.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# -*- coding: utf-8 -*-
"""
This module implements the classes that deals with quantities objects.
This module implements classes that deal with quantities.
It requires the latex package SIunitx.
It converts the objects from the quantities package to latex strings that
display them using the SIunitx package. Not all units work because of name
differences between quantities and SIunitx. If you find one that doesn't work
please create a pull request that adds it to the ``UNIT_NAME_TRANSLATIONS``
dictionary.
.. :copyright: (c) 2015 by Björn Dahlgren.
:license: MIT, see License for more details.
Expand All @@ -15,6 +19,12 @@
from .utils import NoEscape, escape_latex


# Translations for names used in the quantities package to ones used by SIunitx
UNIT_NAME_TRANSLATIONS = {
'Celsius': 'celsius',
}


def _dimensionality_to_siunitx(dim):
import quantities as pq

Expand All @@ -33,11 +43,20 @@ def _dimensionality_to_siunitx(dim):
for prefix in prefixes:
# Split unitname into prefix and actual name if possible
if unit.name.startswith(prefix):
substring += '\\' + prefix + '\\' + unit.name[len(prefix):]
substring += '\\' + prefix
name = unit.name[len(prefix)]
break
else:
# Otherwise simply use the full name
substring += '\\' + unit.name
name = unit.name

try:
# Check if the name is different in SIunitx
name = UNIT_NAME_TRANSLATIONS[name]
except KeyError:
pass

substring += '\\' + name

if power > 1:
substring += r'\tothe{' + str(power) + '}'
Expand Down

0 comments on commit 594911b

Please sign in to comment.