Difference between revisions of "Units"
Tea.wanderer (talk | contribs) |
m (Text replacement - "pragma-ade.com" to "pragma-ade.nl") |
||
(13 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
< [[Math]] [[Physics]] > | < [[Math]] [[Physics]] > | ||
− | The units module comes with ConTeXt's default distribution, and | + | The units module comes with ConTeXt's default distribution. In MkIV the units module comes built-in, however there is still the option to access the old module and its commands via {{cmd|usemodule}}[units] |
− | + | Additional documentation about units in MkIV is available from the [[http://www.pragma-ade.nl/general/manuals/units-mkiv.pdf Units manual]] | |
+ | |||
+ | ===Using the built-in command=== | ||
+ | |||
+ | In MkIV you can use the built-in {{cmd|unit}} command to parse units. Some examples of this are shown below: | ||
+ | |||
+ | <texcode> | ||
+ | 10 \unit{km/h} | ||
+ | \unit{10 km/h} | ||
+ | \unit{~1 km/h} | ||
+ | \unit{KiloMeter/Hour} | ||
+ | \unit{10 cubic meter / second} | ||
+ | \unit{kilo pascal square meter / second} | ||
+ | \unit{kilo pascal square meter / kelvin second }$ | ||
+ | \unit{10 AC} | ||
+ | $\frac{10 \unit{m/s}}{20 \unit{m/s}}$ | ||
+ | {\ss 30 \unit{kilo pascal square meter / second kelvin}} | ||
+ | \unit{123.22^-3 km/s} | ||
+ | \unit{123.22e-3 km/s} | ||
+ | </texcode> | ||
+ | |||
+ | ConTeXt will take care of spacing and formatting. The parser is fairly flexible, and the following all work and give the same output. | ||
+ | |||
+ | {| | ||
+ | ! width="45%"| | ||
+ | ! width="10%"| | ||
+ | ! width="45%"| | ||
+ | |- | ||
+ | | <texcode> | ||
+ | \unit{10 kg} | ||
+ | \unit{10kg} | ||
+ | \unit{10 kilo gram} | ||
+ | \unit{10 kilogram} | ||
+ | \unit{10 Kilo Gram} | ||
+ | \unit{10 KiloGram} | ||
+ | </texcode> | ||
+ | | | ||
+ | |<context mode=mkiv> | ||
+ | \startlines | ||
+ | \unit{10 kg} | ||
+ | \unit{10kg} | ||
+ | \unit{10 kilo gram} | ||
+ | \unit{10 kilogram} | ||
+ | \unit{10 Kilo Gram} | ||
+ | \unit{10 KiloGram} | ||
+ | \stoplines | ||
+ | </context> | ||
+ | |} | ||
+ | |||
+ | The command works in text as well as in math mode and spaces are normally ignored. You can define your own command: | ||
+ | |||
+ | <texcode> | ||
+ | \defineunits[un][alternative=text,separator=medium] | ||
+ | </texcode> | ||
+ | |||
+ | Valid separators are <code>cdot</code>, <code>big</code>, <code>medium</code>, and <code>space</code> with the first being the default. You can later tune the rendering with | ||
+ | |||
+ | <texcode> | ||
+ | \setupunits[un][separator=cdot] | ||
+ | </texcode> | ||
+ | |||
+ | In due time there will be a few more options. Numbers are formatted cf the digits mechanism (not discussed here). | ||
+ | |||
+ | ===Using the old units module=== | ||
The following is copied from the source file [[source:m-units.tex|m-units.tex]] and only slightly modified: | The following is copied from the source file [[source:m-units.tex|m-units.tex]] and only slightly modified: | ||
Line 21: | Line 84: | ||
− | * This runtime loadable module implements a way of defining units. The core macro is | + | * This runtime loadable module implements a way of defining units. The core macro is {{cmd|dimension}}, a rather clever one that is able to cooperate with some other dimension related macros. As said, this module enables user to enter: |
<context source="yes"> | <context source="yes"> | ||
Line 28: | Line 91: | ||
</context> | </context> | ||
− | (Note that the space after | + | (Note that the space after {{cmd|Second}} needs to be explicitly specified; otherwise, it will get eaten.) |
− | * Units act upon signals that they insert in the text, in case no quantity is given you need to start the unit sequence using | + | * Units act upon signals that they insert in the text, in case no quantity is given you need to start the unit sequence using {{cmd|Unit}} |
<context source="yes"> | <context source="yes"> | ||
Line 45: | Line 108: | ||
</texcode> | </texcode> | ||
− | This definition means that we can ask for the meaning of a unit using | + | This definition means that we can ask for the meaning of a unit using {{cmd|unitmeaning}} and get a list of used units by saying {{cmd|placelistofunits}}. |
− | We have to use the command | + | We have to use the command {{cmd|unitmeaning}} instead of {{cmd|meaning}}, simply because the latter is a TeX primitive we don't want to lose. We use the label text mechanism for translations. |
---- | ---- | ||
Line 54: | Line 117: | ||
External link: | External link: | ||
− | [[ | + | [[https://www.easyunitconverter.com/ Easy Unit Converter]] |
− | * Prefixes: | + | * Prefixes: {{cmd|Atto}} a, {{cmd|Femto}} f, {{cmd|Pico}} p, {{cmd|Nano}} n, {{cmd|Micro}} μ, {{cmd|Milli}} m, {{cmd|Centi}} c, {{cmd|Deci}} d, {{cmd|Hecto}} h, {{cmd|Kilo}} k, {{cmd|Mega}} M, {{cmd|Giga}} G, {{cmd|Tera}} T, {{cmd|Peta}} P, {{cmd|Exa}} E (missing: {{cmd|Zetta}} Z, {{cmd|Yotta}} Y, {{cmd|Zepto}} z, {{cmd|Yocto}} y) |
− | * Suffixes: | + | * Suffixes: {{cmd|Linear}} <sup>1</sup>, {{cmd|Square}} <sup>2</sup>, {{cmd|Cubic}} <sup>3</sup>, {{cmd|Inverse}} or {{cmd|ILinear}} <sup>-1</sup>, {{cmd|ISquare}} <sup>-2</sup>, {{cmd|ICubic}} <sup>-3</sup> |
− | * Fraction: | + | * Fraction: {{cmd|Per}}, {{cmd|Percent}}, {{cmd|Permille}}, {{cmd|Promille}} |
− | * Time: | + | * Time: {{cmd|Second}}, {{cmd|Minute}}, {{cmd|Hour}}, {{cmd|Day}}, {{cmd|Month}}, {{cmd|Year}} |
− | * Length, Area, Volume: | + | * Length, Area, Volume: {{cmd|Meter}}, {{cmd|Angstrom}}, {{cmd|Foot}}, {{cmd|Inch}}, {{cmd|Liter}} |
− | * Mass, Force and Pressure: | + | * Mass, Force and Pressure: {{cmd|Gram}}, {{cmd|Atom}}, {{cmd|Newton}}, {{cmd|Pascal}}, {{cmd|Mol}}, {{cmd|At}}, {{cmd|Atm}}, {{cmd|Bar}}, {{cmd|Force}} |
− | * Energy, Electricity and Magnetism: | + | * Energy, Electricity and Magnetism: {{cmd|Joule}}, {{cmd|Watt}}, {{cmd|Cal}}, {{cmd|Farad}}, {{cmd|Henry}}, {{cmd|Ohm}}, {{cmd|Siemens}}, {{cmd|Ampere}}, {{cmd|Coulomb}}, {{cmd|Volt}}, {{cmd|Tesla}}, {{cmd|Gauss}}, {{cmd|VoltAC}}, {{cmd|VoltDC}}, {{cmd|EVolt}}, {{cmd|Weber}} |
− | * Temperature: | + | * Temperature: {{cmd|Degrees}}, {{cmd|Kelvin}}, {{cmd|Celsius}}, {{cmd|Fahrenheit}} |
− | * Angle: | + | * Angle: {{cmd|Rad}}, {{cmd|Deg}}, {{cmd|Sterant}} |
− | * Frequency, Optics and Acoustics: | + | * Frequency, Optics and Acoustics: {{cmd|Hertz}}, {{cmd|RevPerSec}}, {{cmd|RevPerMin}}, {{cmd|Candela}}, {{cmd|Lux}}, {{cmd|Bell}} |
− | * Chemistry and Radiation: | + | * Chemistry and Radiation: {{cmd|Atom}}, {{cmd|Mol}}, {{cmd|Molair}}, {{cmd|Equivalent}}, {{cmd|Bequerel}}, {{cmd|Sievert}}, {{cmd|Gray}} |
− | * Informatics: | + | * Informatics: {{cmd|Baud}}, {{cmd|Bit}}, {{cmd|Byte}} |
* some old dutch (not listed) | * some old dutch (not listed) | ||
− | === | + | ===Define a new unit=== |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | = | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | <context source="yes"> | ||
+ | \usemodule[units] | ||
+ | \unit[Mynewunit]{Nu}{New Unit} | ||
+ | 10 \Mynewunit | ||
+ | </context> | ||
− | [[Category: | + | [[Category:Sciences]] |
− | |||
[[Category:Modules]] | [[Category:Modules]] |
Latest revision as of 08:28, 2 July 2022
The units module comes with ConTeXt's default distribution. In MkIV the units module comes built-in, however there is still the option to access the old module and its commands via \usemodule[units]
Additional documentation about units in MkIV is available from the [Units manual]
Contents
Using the built-in command
In MkIV you can use the built-in \unit command to parse units. Some examples of this are shown below:
10 \unit{km/h} \unit{10 km/h} \unit{~1 km/h} \unit{KiloMeter/Hour} \unit{10 cubic meter / second} \unit{kilo pascal square meter / second} \unit{kilo pascal square meter / kelvin second }$ \unit{10 AC} $\frac{10 \unit{m/s}}{20 \unit{m/s}}$ {\ss 30 \unit{kilo pascal square meter / second kelvin}} \unit{123.22^-3 km/s} \unit{123.22e-3 km/s}
ConTeXt will take care of spacing and formatting. The parser is fairly flexible, and the following all work and give the same output.
\unit{10 kg} \unit{10kg} \unit{10 kilo gram} \unit{10 kilogram} \unit{10 Kilo Gram} \unit{10 KiloGram} |
The command works in text as well as in math mode and spaces are normally ignored. You can define your own command:
\defineunits[un][alternative=text,separator=medium]
Valid separators are cdot
, big
, medium
, and space
with the first being the default. You can later tune the rendering with
\setupunits[un][separator=cdot]
In due time there will be a few more options. Numbers are formatted cf the digits mechanism (not discussed here).
Using the old units module
The following is copied from the source file m-units.tex and only slightly modified:
- Scientific units can be typeset in math mode pretty well, but occasionally one has to take care of spacing.
Furthermore, entering these units is not that natural as wanted. Therefore this module presents a more natural way of doing things, like:
\usemodule[units] 1.23 \Cubic \Meter \Per \Second
This example shows that we use the order in which we say things, instead of typeset things. There is no dedicated manual for this module, but an article (in dutch) has been published in [Maps#21], the journal of the NTG.
- This runtime loadable module implements a way of defining units. The core macro is \dimension, a rather clever one that is able to cooperate with some other dimension related macros. As said, this module enables user to enter:
\usemodule[units] Some 10 \Square \Meter \Per \Second\ or more.
(Note that the space after \Second needs to be explicitly specified; otherwise, it will get eaten.)
- Units act upon signals that they insert in the text, in case no quantity is given you need to start the unit sequence using \Unit
\usemodule[units] Units of measurement is \Unit \Milli \Meter
The units itself are implemented as synonyms.
\definesynonyms [unit] [units] [\unitmeaning] \setupsynonyms [unit] [textstyle=\dimension]
This definition means that we can ask for the meaning of a unit using \unitmeaning and get a list of used units by saying \placelistofunits.
We have to use the command \unitmeaning instead of \meaning, simply because the latter is a TeX primitive we don't want to lose. We use the label text mechanism for translations.
Macros defined in units
External link: [Easy Unit Converter]
- Prefixes: \Atto a, \Femto f, \Pico p, \Nano n, \Micro μ, \Milli m, \Centi c, \Deci d, \Hecto h, \Kilo k, \Mega M, \Giga G, \Tera T, \Peta P, \Exa E (missing: \Zetta Z, \Yotta Y, \Zepto z, \Yocto y)
- Suffixes: \Linear 1, \Square 2, \Cubic 3, \Inverse or \ILinear -1, \ISquare -2, \ICubic -3
- Fraction: \Per, \Percent, \Permille, \Promille
- Time: \Second, \Minute, \Hour, \Day, \Month, \Year
- Length, Area, Volume: \Meter, \Angstrom, \Foot, \Inch, \Liter
- Mass, Force and Pressure: \Gram, \Atom, \Newton, \Pascal, \Mol, \At, \Atm, \Bar, \Force
- Energy, Electricity and Magnetism: \Joule, \Watt, \Cal, \Farad, \Henry, \Ohm, \Siemens, \Ampere, \Coulomb, \Volt, \Tesla, \Gauss, \VoltAC, \VoltDC, \EVolt, \Weber
- Temperature: \Degrees, \Kelvin, \Celsius, \Fahrenheit
- Angle: \Rad, \Deg, \Sterant
- Frequency, Optics and Acoustics: \Hertz, \RevPerSec, \RevPerMin, \Candela, \Lux, \Bell
- Chemistry and Radiation: \Atom, \Mol, \Molair, \Equivalent, \Bequerel, \Sievert, \Gray
- Informatics: \Baud, \Bit, \Byte
- some old dutch (not listed)
Define a new unit
\usemodule[units] \unit[Mynewunit]{Nu}{New Unit} 10 \Mynewunit