36

It’s great that Quantity can utilize Wolfram Alpha to interpret unit strings that it doesn’t recognize, but I need my code to work on machines that do not have Internet access.

Is there a complete list of built-in (i.e. canonical) unit strings recognized by Quantity?

Better yet, is there a programmatic way to produce such a list?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
SWB
  • 625
  • 4
  • 8
  • 4
    Good question! I hope Wolfram notices this question and intends to make the Quantity capability stand alone (I often use compound units and almost always run into this issue). – Cassini Apr 19 '13 at 21:25

3 Answers3

34

This is certainly the optimal way of obtaining the list you are looking for

Quantity;QuantityUnits`Private`$UnitReplacementRules[[1, All, 1]]

EDIT for v10 (thanks @DavidCreech)

In v10 this undocumented variable format has been changed into an association, whose keys are the units.

Quantity; Keys[QuantityUnits`Private`$UnitReplacementRules]
Rojo
  • 42,601
  • 7
  • 96
  • 188
  • 2
    Haha, I just came here to post this! Damn you fellow spelunker! – rm -rf Jan 26 '13 at 20:03
  • FWIW, this seems to me the optimal way to obtain this list, which incidentally is appropriate. – Oleksandr R. Jan 27 '13 at 00:55
  • @OleksandrR. I trust your judgement, editing – Rojo Jan 27 '13 at 01:10
  • 2
    Haha, nice! The Dispatch object from which this list is extracted is actually built from the downvalues of CalculateUnits`UnitCommonSymbols`KnownUnit0Q, which is the canonical arbiter of whether a unit is valid or not. But since this doesn't change after the QuantityUnits` package is loaded, recovering the list as you do here is surely the most direct approach. – Oleksandr R. Jan 27 '13 at 01:21
  • That is awesome. Thanks! – SWB Jan 27 '13 at 06:06
  • 3
    @Rojo Note that in version 10 an association is used, so use Quantity; Keys[QuantityUnitsPrivate$UnitReplacementRules] if you are using 10. – David Creech Aug 26 '14 at 20:25
  • In v12.2, it seems QuantityUnits\$UnitList` is simpler? – Fidel I. Schaposnik Mar 08 '21 at 18:16
9

As a partial answer the documentation says:

Supported units include all those specified by NIST Special Publication 811.

This is repeated in Unit Discovery. It also states:

Unit interpretation requires internet connectivity, and can entail additional evaluation time. If speed is a concern, it is advisable to use the canonical unit specification, which can be found using InputForm.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
4

For Mathematica 12.2.

These two are equivalent

Sort@Keys[QuantityUnits`Private`$UnitReplacementRules] == 
 Sort@QuantityUnits`$UnitList
(*True*)

There are also

QuantityUnits`$UnitAlternateStandardNameReplacements

contains something like Celsius which is not contained in $UnitList. Most of these units are readily usable in Quantity except few cases need internet connection for interpretation.

There are also UnitShortName information in QuantityUnits`$UnitTable

shortUnitAssoc = QuantityUnits`$UnitTable // Query[;; , "UnitShortName"] // 
 Select[# =!= None &];

which contains "Milligrams" -> "mg", "Kilograms" -> "kg", "Meters" -> "m" etc. Some of these short unit are readily usable like Quantity[1,"mg"].

matheorem
  • 17,132
  • 8
  • 45
  • 115