The siunitx package
Typesets numbers and physical quantities with consistent spacing and units: \qty, \num, \unit, and the table column S that aligns numbers at the decimal marker.
Loading the package
\usepackage{siunitx}A quantity typed by hand, such as 3.2 m/s, goes wrong in several ways: the unit turns italic in math mode, a line can break between number and unit, and the notation differs from chapter to chapter. \qty{3.2}{\meter\per\second} sets an upright unit, a thin space that does not break, and one notation for the whole document.
Units are written with macros (\kilo\gram, \meter\per\second\squared) or as literal text (kg.m/s^2). \sisetup changes the output for all quantities at once, for example fractions instead of negative exponents, or a decimal comma.
Version 3 from 2021 introduced the names \qty, \unit and \qtyrange. The older names \SI, \si and \SIrange still work in version 3.
Main commands
\qty{3.2}{\meter\per\second} | Number with unit. Older name: \SI. |
\num{6.022e23} | Number alone: groups the digits in threes and writes e23 as a power of ten. |
\unit{\kilo\gram} | Unit alone. Older name: \si. |
\qtyrange{20}{25}{\celsius} | Range of values with one unit. Older name: \SIrange. |
\ang{45} | Angle in degrees; \ang{1;2;3} gives degrees, minutes and seconds. |
\sisetup{per-mode = symbol} | Sets options for the rest of the document; here \per prints a slash instead of a negative exponent. |
\begin{tabular}{l S[table-format=3.2]} | Column type S aligns numbers at the decimal marker; table-format=3.2 reserves 3 digits before and 2 after it. |
Complete example
\documentclass{article}
\usepackage{siunitx}
\sisetup{per-mode = symbol}
\begin{document}
The car reaches \qty{27.8}{\meter\per\second} after \qty{9.3}{\second}.
The resistor has \qty{4.7}{\kilo\ohm}, the room \qtyrange{20}{25}{\celsius}.
Avogadro constant: \num{6.022e23}. Angle: \ang{45}.
Torque is measured in \unit{\newton\meter}.
\begin{tabular}{l S[table-format=3.2]}
Sample & {Mass in \unit{\gram}} \\ \hline
A & 12.5 \\
B & 104.25 \\
C & 7.03 \\
\end{tabular}
\end{document}Compiled with pdfLaTeX from TeX Live 2023.
Pitfalls
\qtyand\unitdo not exist before version 3. On an older TeX installation they give Undefined control sequence; use\SIand\sithere.- The
physicspackage defines its own\qty. When it is loaded,siunitxdoes not define\qty, and you have to write\SI. - The first argument has to be a number. Text in it, and a header cell of an
Scolumn without braces, give Invalid number '1,250,000'. Put header text in braces:{Mass}. - For German documents,
\sisetup{locale = DE}prints a decimal comma and a centred dot before the power of ten.output-decimal-marker = {,}changes the decimal marker alone.