Units and numbers in LaTeX with siunitx (qty, unit, num)
The siunitx package typesets numbers with units: \qty{9.81}{\metre\per\second\squared} prints the number, a thin space and the unit in upright letters. \unit prints a unit alone and \num a number alone.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{siunitx}Number with unit
The first argument of \qty is the number, the second the unit. A unit is built from commands: a prefix and a unit such as \kilo\gram, \per for division, and \squared or \cubed after the unit they belong to. Short forms such as \km and \kg exist for common units. The commands work in text and in math mode.
The acceleration is \qty{9.81}{\metre\per\second\squared},
the mass \qty{1.5}{\kilo\gram} and the distance \qty{5}{\km}.A unit or a number alone
\unit{...} prints a unit without a number. \num{...} formats a number: it separates the digits of long numbers into groups of three and turns e into a power of ten.
Energy is billed in \unit{\kilo\watt\hour}.
The values are \num{12345.678} and \num{1.2e-5}.Decimal comma for German texts
\sisetup{locale = DE} in the preamble prints a comma as the decimal marker and a centred dot in front of powers of ten. You can type the numbers with a point or with a comma; the setting decides what is printed.
This goes into the preamble.
\sisetup{locale = DE}Show division as a slash or a fraction
\per prints a negative exponent by default. The option per-mode = symbol prints a slash and per-mode = fraction a fraction. Set it for one quantity in square brackets or for the whole document with \sisetup{...}.
Exponent: \qty{9.81}{\metre\per\second\squared},
slash: \qty[per-mode = symbol]{9.81}{\metre\per\second\squared},
fraction: \qty[per-mode = fraction]{9.81}{\metre\per\second\squared}.Ranges, lists, uncertainties and angles
\qtyrange{from}{to}{unit} prints a range and \qtylist{a;b;c}{unit} a list with the unit on every value. An uncertainty goes in parentheses after the digits it applies to. \ang{...} prints an angle in degrees.
Between \qtyrange{10}{20}{\celsius}, at \qtylist{1;2;3}{\metre},
measured \qty{1.234(5)}{\volt}, at an angle of \ang{45}.Pitfalls
\SI,\siand\SIrangeare the command names ofsiunitxversion 2. Version 3 still accepts them, but new documents should use\qty,\unitand\qtyrange.- The
physicspackage has a command\qtyof its own. When both packages are loaded,siunitxdoes not define\qtyand writes a warning to the log;\qty{9.81}{\metre}then fails. Add\AtBeginDocument{\RenewCommandCopy\qty\SI}to the preamble, or write\SI. - A number that
siunitxcannot read stops with Invalid number '1,250,000'. - For a table column of numbers aligned at the decimal marker see Align numbers at the decimal point in LaTeX tables.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
siunitx - Package
physics - Error Invalid number '1,250,000'
- How-to Degree symbol in LaTeX
- How-to Align numbers at the decimal point in LaTeX tables