[Tex/LaTex] SIunits package – separating value and unit

siunitsspacingunits

Can the space between the value and the unit be added automatically, when units are written using the SIunits package?

The package gives the following two options to add units:

$500\kelvin$
$\unit{500}{\kelvin}$

enter image description here

Only the latter adds seperation between value and unit. Adding the \unit{...}{...} parameter is too cumbersome and doesn't give any ease of use compared to the manual \;\mathrm{...} method.

Using the former notation to avoid this, the space can be added manually with \; of course, 500\;\kelvin, but I am now looking for the possibility of never having to add anything but the unit.

I have looked into the siunitx package as well, which only replaces the \unit{...}{...} with \SI{...}{...} (and apparently makes it mandatory!). This therefore gives no help.

Is it possible to automatically/generally add a \; before every single unit (and unit group) in the document?

Best Answer

One could achieve this in SIunits by patching an appropriate internal

\documentclass{article}
\usepackage{SIunits}
\usepackage{xpatch}
\makeatletter
\xpretocmd\SI@fstyle{\;}{}{}
\makeatother
\begin{document}
$500\;\mathrm{K}$

$500\kelvin$

\end{document}

You can also do this in siunitx by activating 'free standing' units and 'pre-spacing'

\documentclass{article}
\usepackage{siunitx}
\sisetup{free-standing-units, space-before-unit, number-unit-product = \;}
\begin{document}

$500\;\mathrm{K}$

$500\kelvin$

\end{document}

(Using \qty is recommended as there are outcomes that require a more structured mark-up, but one design aim has always been to support a range of input forms.)


For these approaches to work you have to have the unit as a single command: chaining things together such as \volt\per\kelvin won't work as there is no way to 'know' what marks the end of the unit. For siunitx we can declare a new unit which will then only add a space at the start

\documentclass{article}
\usepackage{siunitx}
\sisetup{free-standing-units, space-before-unit, number-unit-product = \;}
\DeclareSIUnit{\voltperkelvin}{\volt\per\kelvin}
\begin{document}

$500\;\mathrm{V}\,\mathrm{K}^{-1}$

$500\voltperkelvin$

\end{document}

(The need to define units 'up front' for 'pre-spacing' to work is one reason the siunitx documentation is primarily focussed on logic mark up.)

This is much harder to achieve with SIunits as the unit commands are not context-sensitive. One might add appropriate switching but this becomes a significant change in the way the code works: probably crosses into 'development' (which as the SIunits documentation says is now done in siunitx.)