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}.
Result
The compiled example

Open in the editor

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}.
Result
The compiled example

Open in the editor

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}

Open in the editor

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}.
Result
The compiled example

Open in the editor

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}.
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages