pylatex.quantities

This module implements classes that deal with quantities.

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.

class Quantity(quantity, *, options=None, format_cb=None)[source]

Bases: pylatex.base_classes.command.Command

A class representing quantities.

Parameters:
  • quantity (quantities.quantity.Quantity) – The quantity that should be displayed
  • options (None, str, list or Options) – Options of the command. These are placed in front of the arguments.
  • format_cb (callable) – A function which formats the number in the quantity. By default this uses numpy.array_str.

Examples

>>> import quantities as pq
>>> speed = 3.14159265 * pq.meter / pq.second
>>> Quantity(speed, options={'round-precision': 3,
...                          'round-mode': 'figures'}).dumps()
'\\SI[round-mode=figures,round-precision=3]{3.14159265}{\meter\per\second}'

Uncertainties are also handled:

>>> length = pq.UncertainQuantity(16.0, pq.meter, 0.3)
>>> width = pq.UncertainQuantity(16.0, pq.meter, 0.4)
>>> Quantity(length*width).dumps()
'\\SI{256.0 +- 0.5}{\meter\tothe{2}}

Ordinary numbers are also supported:

>>> Avogadro_constant = 6.022140857e23
>>> Quantity(Avogadro_constant, options={'round-precision': 3}).dumps()
'\\num[round-precision=3]{6.022e23}'