1

I would like to make a table with values followed by its units, in which the numbers of the values are aligned according to the "rules" of the siunitx-package. I can do this with two columns (one for the values in S and one for the units in l), but I would like to have them in one column.

The command for values and units in the new v3 version of siunitx-package is: \qty[⟨options⟩]{⟨number⟩}{⟨unit⟩}

Here follows my MWE:

\documentclass{memoir}

\usepackage{makecell}% forced line break in cell of tabular
\usepackage{siunitx}% SI units & number alignment in table

%-----------------------------------------------------------%
%                       SI UNITX

\sisetup{%
    mode=math,% math, match or text
    propagate-math-font=true,%
    text-series-to-math, text-family-to-math,%
%
    unit-optional-argument=true, free-standing-units=true,% units use outside siunitx
    use-xspace=true, space-before-unit=true,% space before units
    quantity-product=\;,% hspace number-unit
    exponent-product=\ensuremath{\cdot},%
    per-mode=fraction, sticky-per,% fraction symbol
% Uncertainty & exponent
    bracket-ambiguous-numbers=false,%
    separate-uncertainty, uncertainty-mode=separate, separate-uncertainty-units=single,% ± sign for (#)
    table-align-uncertainty=true,%
    table-align-exponent=true,%
% Alignment
    table-alignment-mode=format,%
    table-number-alignment=center,% number cells in S column
    table-text-alignment=center,% text only cells in S column
    tight-spacing=false}%

%-----------------------------------------------------------%
%                       MAKECELL SETTINGS

\makegapedcells% vsep cells
\setcellgapes{0pt}% extra vsep of rows

    \renewcommand\cellset{%
        \def\arraystretch{1.0}% vspace between rows, height row, line spacing
        \setcellgapes{0pt}%
        \setlength{\tabcolsep}{0pt}% hsep columns (text-border)
        \setlength\extrarowheight{0pt}% vsep above cell
        }%
        
%-----------------------------------------------------------%
%                       COLUMNS
%   Text-style math
    \newcolumntype{u}[1]% units
        { @{\,} >{$\textstyle} #1 <{$} }%

%   Siunitx
    \newcolumntype{J}[1]{S[table-format=#1]}%
    \newcolumntype{U}[1]{S[table-format=#1,%
        table-align-uncertainty=false,%
        table-align-exponent=false]}

%-----------------------------------------------------------%
%-----------------------------------------------------------%
\begin{document}

%---------%
\section{Siunits Tabular Uncertainty}%

Referencing tables automatically by \ref{tab:uncertain}.\par

\begin{table}[ht]
  \caption{A wide math mode tabular table with si unit columns of uncertainty}%
        \label{tab:uncertain}
  \begin{tabular}%
        { U{-1.3(2)e2} u{l} | 
          J{-1.3(2)e2} | 
          J{+1.3(2)e2} u{l} }
    \toprule
        {U \{-1.3(2)e2\}}                           & {u \{l\}} &
        {J \{-1.3(2)e2\}}                           &
        {J \{+1.3(2)e2\}}                           & {u \{l\}} \\
    \midrule
        1.1e19                                      & \unit{\coulomb \per \V} &
        \qty{-0.497(17)}{\A \per \V}                &
        2.6e21                                      & \unit{\J \per \coulomb} \\
        9.6(2)e24                                   & \unit{\m\kg \per \square\s} &
        \qty{-2.17(39)e22}{\s\kg \per \square\m}    &
        -2.17(39)e22                                & \unit{\N \per \square\m} \\
        -0.224(41)                                  & \unit{\siemens} &
        \qty{2.3(4)}{\kg}                           &
        +-6.2(2)                                    & \psi \\
        \qty{-0.224(41)}{\N \per \square\m}         & & 
        \qty{2.890(4)}{\s\kg \per \square\m}        &
        \qty{2.890(4)}{\s\kg \per \square\m}        & \\
    \bottomrule
   \end{tabular}
\end{table}

\end{document}

It seems like the \qty command makes the number value into a text number, see last row & 2nd column in the pictures below how its value is influenced by the table-text-alignment option and not the table-number-alignment option:

\sisetup{table-text-alignment=center}: table-text-alignment=center

\sisetup{table-text-alignment=left} without makecell settings: table-text-alignment=left

Furtermore, I have to use the makecell-package for other tables. However, the table-text-alignment=left option with makecell settings results in this same text centering of \qty:

\sisetup{table-text-alignment=left} with makecell settings: left and makecell

Is there a way to make value+unit columns with siunitx alignment "rules" and using the makecell-package?

2
  • I've ben looking at this, but I'm not sure exactly what input you really want: for example, your demos have some units in with the value and some separately. Also, you will need to know the widest unit to get alignment to work if it's in the same column as the value: that's probably going to be tricky.
    – Joseph Wright
    Jan 18, 2022 at 10:34
  • Thank you for your reaction! Preferably I would want to make a table with numerical values and their units in one column, where the numbers are aligned according to the siunitx "rules". So the 3rd column in my example table with \qty{}{}, where the numbers should be aligned on their uncertainty sign/decimal point/exponent. The table contains my own attempts to solve the problem with an extra column for the units, but I prefer to have the value+unit in one column.
    – Elise
    Jan 18, 2022 at 12:56

1 Answer 1

1

Although I would not recommend it, one could take the content of the cell, split it into the \qty, the first argument and the second argument, then construct the cell manually using \tablenum:

\documentclass{article}

\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{array}
\usepackage{collcell}


\sisetup{%
    propagate-math-font=true,%
    text-series-to-math, text-family-to-math,%
%
    quantity-product=\;,% hspace number-unit
    exponent-product=\ensuremath{\cdot},%
    per-mode=fraction, sticky-per,% fraction symbol
% Uncertainty & exponent
    bracket-ambiguous-numbers=false,%
    uncertainty-mode=separate,
  }

\newcolumntype{J}[1]
  {>{\sisetup{table-format = {#1}}\collectcell\mycolumn}c<{\endcollectcell}}
\newcolumntype{U}[1]
    {>{\sisetup{table-format = {#1},table-align-uncertainty=false,%
 table-align-exponent=false}\collectcell\mycolumn}c<{\endcollectcell}}

\ExplSyntaxOn

\newcommand\mycolumn[1]{
  \tl_if_head_eq_meaning:nNTF {#1} \qty
    { \mycolumnaux#1\stop }
    {
      \str_if_eq:eeTF {#1} { { \use:n #1 } }
        {#1}
        { \tablenum {#1} }
    }
}
\ExplSyntaxOff

\newcommand\mycolumnaux{}
\long\def\mycolumnaux #1\qty#2#3\stop{%
  \tablenum{#2}%
  \llap{\;\unit{#3}}%
}

\begin{document}

\section{Siunits Tabular Uncertainty}%

Referencing tables automatically by \ref{tab:uncertain}.\par

\begin{table}[ht]
  \caption{A wide math mode tabular table with si unit columns of uncertainty}%
        \label{tab:uncertain}
  \begin{tabular}%
        { 
          @{}
          U{-1.3(2)e2{\;\unit{\m\kg \per \square\s}}} | 
          J{-1.3(2)e2{\;\unit{\s\kg \per \square\m}}} | 
          J{+1.3(2)e2{\;\unit{\s\kg \per \square\m}}}
          @{}
        }
    \toprule
        {U \{-1.3(2)e2\}}                           & 
        {J \{-1.3(2)e2\}}                           &
        {J \{+1.3(2)e2\}}                           \\
    \midrule
        \qty{1.1e19}{\coulomb \per \V}              &
        \qty{-0.497(17)}{\A \per \V}                &
        \qty{2.6e21}{\J \per \coulomb}              \\
        \qty{9.6(2)e24}{\m\kg \per \square\s}       &
        \qty{-2.17(39)e22}{\s\kg \per \square\m}    &
        \qty{-2.17(39)e22}{\N \per \square\m}       \\
        \qty{-0.224(41)}{\siemens}                  &
        \qty{2.3(4)}{\kg}                           &
        \qty{+-6.2(2)}{\psi}                        \\
        \qty{-0.224(41)}{\N \per \square\m}         & 
        \qty{2.890(4)}{\s\kg \per \square\m}        &
        \qty{2.890(4)}{\s\kg \per \square\m}        \\
    \bottomrule
   \end{tabular}
\end{table}

\end{document}

I have used \llap to make the printed unit have 'no width', as \tablenum saves space based on the table-format. (If we don't allow for the unit text in the format, we loose control of alignment.)

I've tested cell content by checking:

  • If they start with \qty (the \tl_if_head_eq_meaning:nTF test)
  • If they are wrapped in braces (the \str_if_eq:eeTF test)
  • Otherwise, they are to be used as is
1
  • Thank you very much! It solves my problem perfectly! Have a nice day!
    – Elise
    Jan 18, 2022 at 16:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .