siunitx
allows us to print numbers, units and quantities, the later understood as a combination of numbers and units. For that we have, respectively, \num
, \unit
and \qty
.
Using \qty{<number>}{<unit>}
is not the same as just juxtaposing \num{<number>}\unit{<unit>}
, since \qty
takes care of proper spacing between number and unit, as per the option quantity-product
. So far, so good.
However, I'd like to be able to split the number from the unit and still get the spacing from quantity-product
. The use case is a table, where I'd like to use an S
column, and pass the unit in the table's preamble to all numbers in the column.
Now, I know most everyone here will tell me I should not add the unit to each number but should rather just give it once in the table's header. I agree this is usually best practice. But I have a case where the header is already crammed, and the numbers and units are short, so I think my table would look better if I did it this way. So, regardless of table formatting best practices and taste, I'm looking for a technical solution for the situation.
The MWE below summarizes the situation:
\documentclass{article}
\usepackage{siunitx}
\usepackage{array}
\begin{document}
\begin{tabular}{
S[table-format = 4.0, group-digits = false]
S[table-format = 1.1{\unit{\percent}}]<{{\unit{\percent}}}
}
\hline
& \multicolumn{1}{c}{$\frac{\text{imports}}{\text{apparent consumption}}$} \\
\hline
1924 & 3,7 \\
1925 & 3,4 \\
1926 & 3,6 \\
1927 & 4,0 \\
1928 & 3,8 \\
1929 & 4,0 \\
1930 & 4,5 \\
1935 & 4,7 \\
1936 & 4,4 \\
1937 & 3,3 \\
\hline
\end{tabular}
\end{document}
The above example, of course, is missing the space between the numbers and the units in the second column. So I'm trying to get a, let's say, \qtyunit
macro to use instead of \unit
there.
I have tried a couple of alternatives, but I'm not really confident in or satisfied with either. The first one was:
\NewDocumentCommand\qtyunit{m}{\qty[print-unity-mantissa=false]{1}{#1}}
This looks nice, and gets the job done. But I'm not really sure if I should rely on passing 1
as number with print-unity-mantissa=false
actually printing the quantity-product
. This seems very much as a side-effect, which might change (one could argue it should not be printed), and the effect is not documented.
(siunitx
does allow to pass an empty number argument to \qty
, but the case is treated specially, and quantity-product
is skipped).
So I went to siunitx.sty
and cooked (based on the definition of \unit
):
\ExplSyntaxOn
\NewDocumentCommand \qtyunit { O { } > { \TrimSpaces } m }
{
\mode_leave_vertical:
\group_begin:
\siunitx_unit_options_apply:n {#2}
\keys_set:nn { siunitx } {#1}
% print quantity-product spacing, from ‘\siunitx_quantity_print’
\tl_use:N \l__siunitx_quantity_product_tl
\bool_if:NTF \l__siunitx_quantity_break_bool
{ \penalty \binoppenalty }
{ \nobreak }
% now print the unit proper
\siunitx_unit_format:nN {#2} \l__siunitx_tmp_tl
\siunitx_print_unit:V \l__siunitx_tmp_tl
\group_end:
}
\ExplSyntaxOff
But this, while it works, leaves me the sensation of being much overkill for this purpose.
So, the question is, am I missing something in siunitx
from which I could get this in a more straightforward and robust way? If not, does anyone see an alternative which would be better than either of the two above?
Edit: I came up with a third alternative:
\NewDocumentCommand\qtyunit{m}{\qty[parse-numbers=false]{\relax}{#1}}
Looks promising, but it still feels like cheating somewhat.