Units (siunitx)

Setting a quantity like “9.81 m/s²” correctly involves a handful of conventions: units in upright (roman) type, a thin space between the number and the unit, and consistent spacing for the products and quotients of compound units. Following all of this by hand is tedious and drifts. The siunitx package is the standard tool that sets numbers, units, and quantities automatically by one consistent set of rules. This page sorts out the three central commands of the current version (v3) — \num, \unit, and \qty — and the S column type for aligning numbers on the decimal point in tables, and shows how they differ from the v2 names \SI and \si you will still meet in older documents.

Why a units package

Scientific notation follows an international convention (ISO 80000-2 / JIS Z 8000-2 — see the separate page). One of its key points is that unit symbols are set upright. m, kg, and s are not variables, so they must not be set in the variable typeface, italic. But in LaTeX math mode letters are slanted by default, so writing $kg$ literally makes the unit look like a product of an italic k and g.

A second point is spacing. Between the number and the unit you put neither nothing nor too much, but a thin space (the equivalent of the math \,): not 9.81m, and not 9.81 m. And for a compound unit like m/s you usually want one consistent treatment across the whole document — how division is shown (a slash, or a negative power s⁻¹), and whether a product such as kg·m carries a middle dot. Hand-writing each of these as \mathrm{m} and so on tends to leave the spacing, powers, and products inconsistent from one place to the next.

siunitx takes all of this on at once: it sets units upright, inserts the correct space before them, and renders products, quotients, and powers by one consistent rule — and a single setting change switches the look across the whole document. You enable it by loading \usepackage{siunitx} in the preamble.

Formatting a number — `\num`

\num{…} formats a number alone. Give it a number and it sets it upright, sorting out digit grouping, exponents, and signs automatically. Especially handy is its reading of exponents: it recognises e, E, d, and D as “×10 to a power.” So \num{1.234e5} typesets 1.234 × 10⁵ (it does not print a literal e5). It accepts either . or , as the decimal marker and, if needed, adds a leading 0 before the point.

latex
\num{12345}      % → 12 345(桁区切りが入る / digit grouping)
\num{1.234e5}    % → 1.234 × 10^5
\num{0,1234}     % → 0.1234(カンマも小数点として解釈 / comma as decimal)
\num{.5}         % → 0.5(先頭に 0 を補う / leading zero added)
\num{3.45d-4}    % → 3.45 × 10^{-4}

Here 12345 gains a thin grouping space to become 12 345, 1.234e5 becomes 1.234 × 10⁵, and .5 becomes 0.5. Regional conventions (grouping with a comma, a comma decimal marker, and so on) can be switched with options, but to begin with it is enough to remember “pass numbers through \num rather than writing bare digits.” Note that to set a product of numbers you use the dedicated \numproduct{1.2 x 3.4} (entries separated by x); in v3, \num itself does not form products.

Setting a unit — `\unit`

\unit{…} sets a unit alone. There are two ways to give the input. One is the literal style, where you type the unit symbols directly, as in \unit{kg.m/s^2}. Here the period . is an inter-unit product, / is a quotient, and ^ and _ give superscripts and subscripts. siunitx sets this upright with the correct spacing: \unit{kg.m/s^2} comes out as kg m/s² (a thin space between kg and m, a superscript 2 on s).

The other is the interpreted (macro) style, where you build the unit from commands for each unit, prefix, and power — much like reading the unit out in English. The table lists the building blocks.

CommandMeaningOutput
gram\gram / \metre / \secondBase units (SI base-unit names)g / m / s
kilo\kilo \milli \microPrefixes (placed before a unit)k / m / µ
kilogram\kilogramA predefined prefixed unitkg
per\perPuts the next unit in the denominator… ⁻¹ or /…
squared\squared / \cubedSquares / cubes the preceding unit (postfix)m² / m³
square\square / \cubicSquares / cubes the following unit (prefix)m² / m³
latex
\unit{\kilo\gram\per\cubic\metre}   % → kg/m^3 (密度 / density)
\unit{\metre\per\second\squared}     % → m/s^2 (加速度 / acceleration)
\unit{\kilo\gram}                     % → kg
\unit{\kilogram}                      % → kg(定義済みの一語 / predefined)

For a prefixed “kilogram,” writing the prefix and unit in sequence as \kilo\gram, or using the predefined single word \kilogram, both give the same kg. \cubic\metre is a prefix form for “cubic metre, m³,” and \metre\per\second\squared uses the postfix \squared for “metre per second squared, m/s².” How \per looks (a negative power s⁻¹ or a slash /s) is switched with the per-mode option described below.

The literal and interpreted styles can produce the same result, but the interpreted (macro) style has advantages: the appearance of powers, prefixes, and products/quotients can be controlled wholesale by options, and you can define your own new unit macros. The literal style, meanwhile, is convenient for dashing off a quick unit. Either way, the unit is upright and the spacing is correct.

Number and unit together — `\qty`

In real documents, numbers and units almost always appear together. \qty{…}{…} takes the number as its first argument and the unit as its second, and sets them as a unit. It combines \num and \unit: the number part is written exactly as for \num, and the unit part exactly as for \unit. The value of a quantity is “number × unit,” with the space between them standing for the multiplication — and the command is exactly that idea made concrete.

latex
重力加速度は \qty{9.81}{\metre\per\second\squared} である。
The acceleration of gravity is \qty{9.81}{\metre\per\second\squared}.

\qty{1.234e5}{\kilo\gram\per\cubic\metre}  % → 1.234 × 10^5 kg/m^3
\qty{2.998e8}{m/s}                          % リテラル単位もそのまま使える

Here a thin space follows 9.81 and the upright m/s² comes after it, so “9.81 m/s²” is set as a single quantity. You can pass an exponent-bearing number straight in, as in \qty{1.234e5}{…}. The unit part may be a macro (\metre\per\second\squared) or a literal (m/s).

How \per looks is chosen with the per-mode option: the default is a negative power (m s⁻¹), per-mode=symbol gives a slash (m/s), and per-mode=fraction builds a stacked fraction (numerator over denominator). Options can be given individually in square brackets on each command, or set document-wide in the preamble with \sisetup{…}.

latex
\qty{1.99}{\per\kilogram}                 % 既定 → 1.99 kg^{-1}
\qty[per-mode = symbol]{1.99}{\per\kilogram}    % → 1.99 /kg
\qty[per-mode = fraction]{1.345}{\coulomb\per\mole}  % → 縦組みの分数 C/mol

% 文書全体に方針を適用 / set a policy document-wide
\sisetup{per-mode = symbol}

Angles have their own \ang{…}. \ang{45} gives 45°, and an “arc format” with semicolons for degrees, minutes, and seconds is available too: \ang{45;30;15} sets 45°30′15″. Angles take no SI prefixes, so you use this dedicated command rather than \qty.

v2 vs v3 — `\SI` / `\si` are the old names

The \num, \unit, and \qty above are the central commands of siunitx version 3. In older documents and web answers you will meet the version 2 commands instead. The correspondence is as follows.

v2 (old name)v3 (current)Role
SI\SI{9.81}{\metre\per\second\squared}\qty{9.81}{...}Number + unit
si\si{\kilo\gram}\unit{\kilo\gram}Unit alone
num\num{1.234e5}\num{1.234e5} (unchanged)Number alone
SIrange\SIrange{10}{20}{\metre}\qtyrange{10}{20}{\metre}A range (10 m to 20 m)
SIlist\SIlist{10;20;30}{\metre}\qtylist{10;20;30}{\metre}A list (10 m, 20 m, 30 m)

The official manual states that v3 revised the document-command names to be “more descriptive,” and that **\SI, \si, \SIrange, and \SIlist remain available but are not recommended for new documents (use the \qty… family instead)**. The name \num is unchanged between v2 and v3. If your aim is to compile an old document, you can invoke the legacy behaviour with \usepackage{siunitx}[=v2], but **for new writing, use v3’s \qty, \unit, and \num**.

v3 also made the input a little stricter. A prefix cannot be used on its own (it must accompany a unit), and products are written with dedicated commands: \numproduct for numbers, \qtyproduct for quantities with units. In many cases old option names are mapped internally to the new ones, with a note to that effect in the log.

Aligning numbers in tables — the `S` column

In a table of numbers, columns are hard to read unless the digits line up. For tabular, siunitx provides the **S column type**: write S in the column specification in place of c, l, or r, and the numbers in that column are aligned on the decimal point. Their contents are parsed by the same engine as \num, so exponent-bearing values (1.2e3) are handled correctly too.

latex
\begin{tabular}{@{}S@{}}
  \toprule
  {Some Values} \\   % 見出しは波括弧で保護(数値と誤認させない)
  \midrule
      2.3456 \\
     34.2345 \\
     -6.7835 \\
   5642.5   \\
      1.2e3  \\
      e4     \\
  \bottomrule
\end{tabular}

In this table the numbers align at the decimal point, 1.2e3 is set as 1.2 × 10³, and e4 as 10⁴. One important caveat: any **text such as a column heading must be wrapped in braces {…}**. Without the protection of {Some Values}, siunitx tries to read it as part of a number and the alignment breaks.

To fix the digit layout and place the column to taste, the table-format option tells siunitx the “integer digits.decimal digits.” S[table-format=2.4] means “2 integer digits, 4 decimal places,” which steadies the column’s overall look. And when you need aligned numbers inside \multicolumn or \multirow, there is a macro version, \tablenum[table-format=…]{…}, that stands in for the S column.

latex
\begin{tabular}{@{}S[table-format=4.1]@{}}
  \toprule
  {Mass / \unit{\gram}} \\
  \midrule
  1234.5 \\
    12.0 \\
     6.7 \\
  \bottomrule
\end{tabular}

Here the column heading uses \unit{\gram} to show “Mass / g” with an upright unit, while the numbers align to 4 integer digits and 1 decimal place. With the S column you can divide the work — \qty for quantities in the prose, the S column for numbers in tables — and keep a consistent numeric style across the whole document.