siunitx:如何在表格中对齐 \qty 值数字

siunitx:如何在表格中对齐 \qty 值数字

我想制作一个表格,表格中数值后面跟着单位,数值的数字按照 -package 的“规则”排列siunitx。我可以用两列来实现(一列表示 S 中的数值,一列表示 l 中的单位),但我想将它们放在一列中。

-package新 v3 版本中值和单位的命令siunitx是: \qty[⟨options⟩]{⟨number⟩}{⟨unit⟩}

以下是我的 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}

该命令似乎\qty将数字值变成了文本数字,请参见下图中最后一行和第二列,了解其值如何受到选项table-text-alignment而不是table-number-alignment选项的影响:

\sisetup{table-text-alignment=center}表格文本对齐方式=中心

\sisetup{table-text-alignment=left}没有 makecell 设置: 表格文本对齐=左

此外,我必须makecell对其他表格使用 -package。但是,table-text-alignment=left带有 makecell 设置的选项会导致相同的文本居中\qty

\sisetup{table-text-alignment=left}设置makecell左和 makecell

有没有办法使用siunitx对齐“规则”并使用makecell包来制作值+单位列?

答案1

虽然我不推荐这样做,但可以获取单元格的内容,将其拆分为\qty第一个参数和第二个参数,然后使用手动构建单元格\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}

我曾经\llap让打印的单元“没有宽度”,因为这样\tablenum可以节省空间table-format。(如果我们不允许格式中的单元文本,我们将失去对对齐的控制。)

我已经通过检查来测试单元格内容:

  • 如果他们开始\qty\tl_if_head_eq_meaning:nTF测试)
  • 如果它们被括在括号中(\str_if_eq:eeTF测试)
  • 否则,它们将按原样使用

相关内容