使用 tabularray 进行列对齐

使用 tabularray 进行列对齐

我正在尝试使用 tabularray 将特定单元格居中,但我不知道如何覆盖 siunitx 的对齐方式。我希望第二行的单元格居中,但它们目前在小数点处对齐。

\documentclass{standalone}
\usepackage{siunitx}
\usepackage{tabularray}
\usepackage{amsmath}
\UseTblrLibrary{booktabs,siunitx,amsmath}

\newcommand{\deltav}[1]{\ensuremath{\Delta V#1}}
\newcommand{\unitE}{\ensuremath{\text{km}^{2}/\text{s}^{2}}}
\providecommand\degrees[1]{\ensuremath{#1^\circ}} % for degrees symbol
\providecommand{\ccc}{\ensuremath{\text{C3}}}
\providecommand{\rla}{\ensuremath{\text{RLA}}}
\providecommand{\dla}{\ensuremath{\text{DLA}}}

\NewColumnType{H}[2]{S[table-format=#1, round-mode=places, round-precision = #2, scientific-notation = true, exponent-mode = fixed, fixed-exponent = 0]}
\NewColumnType{K}{H{6.4f}{4}}

\begin{document}
\begin{booktabs}{
    colspec = {c K K K},
    row{1} = {font=\bfseries},
    cell{1-2}{1-4} = {}{c},
  }
  \toprule
                         & \ccc                  & \rla                   & \dla                   \\
  Shift                  & 0.025\, \unitE{}      & 0.005\degrees{}        & 0.008\degrees{}        \\
  \midrule
  B.T km                 & 14028.09419155        & 982.72457669           & -13477.32067085        \\
  B.R km                 & -7751.49915236        & -3700.77434474         & 3148.12003385          \\
  \midrule
  \deltav{_\text{x}} m/s & 4.774015816660711e-01 & 4.126455467703491e-02  & -4.480105352801190e-01 \\
  \deltav{_\text{y}} m/s & 6.781754913310237e-01 & -7.925658411887511e-02 & -8.241304954028742e-01 \\
  \deltav{_\text{z}} m/s & 7.649481111877549e-01 & 4.826834479224431e-01  & -1.507332008655069e-01 \\
  \midrule
  \deltav{} m/s          & 1.128264100301457     & 4.908845897939598e-01  & 9.500657929944257e-01  \\
  \midrule
  TCM \deltav{} m/s      & 4.546833189860270e-0  & 4.563499345528536e-0   & 4.486347115134618e-0   \\
  \bottomrule
\end{booktabs}
\end{document}

这让我

数字表,第二行的单元格未居中

最明显的错位是包含“0.0250 km2/s2”的单元格。我希望该文本位于列的中央。

答案1

您应该将booktabs序言改为:

    colspec = {c K K K},
    row{1} =  {c, font=\bfseries},
    row{2} =  {c, guard},  % <--- added `guard`

解释:
通过添加guard第二行的规范,该行不遵守列siunitx的规范,并且该行单元格中的文本仅居中,即添加后您将得到:

在此处输入图片描述

但是,我会按照以下方式写您的表格,在我看来更简单,更符合tabularray精神,也被认为是@Mico 的评论:

\documentclass[margin=3mm]{standalone}
\usepackage{tabularray}
\UseTblrLibrary{amsmath, booktabs, siunitx}

\begin{document}
    \begingroup
\sisetup{table-format=-5.4, 
         round-mode=places, round-precision = 4,
         }
\begin{booktabs}{
    colspec = {Q[c, mode=math] S S S},
    row{1} =  {c, font=\bfseries, guard},
    row{2} =  {c, guard, mode=text, cmd={\sisetup{round-precision=3}}},
                }
  \toprule
            & C3                        & RLA                      & DLA                    \\
Shift       & \qty{0.025}{\km^2/\s^2}   & \qty{0.005}{\degree}   & \qty{0.008}{\degree}   \\
\midrule
B.T km      & 14028.09419155            & 982.72457669           & -13477.32067085        \\
B.R km      & -7751.49915236            & -3700.77434474         & 3148.12003385          \\
\midrule
\Delta_{\mathrm{x}} m/s 
            & 4.774015816660711e-01     & 4.126455467703491e-02  & -4.480105352801190e-01 \\
\Delta_{\mathrm{y}} m/s 
            & 6.781754913310237e-01     & -7.925658411887511e-02 & -8.241304954028742e-01 \\
\Delta_{\mathrm{z}} m/s 
            & 7.649481111877549e-01     & 4.826834479224431e-01  & -1.507332008655069e-01 \\
\midrule
\Delta m/s            
            & 1.128264100301457         & 4.908845897939598e-01  & 9.500657929944257e-01  \\
\midrule
\mathrm{TCM}\;\Delta m/s       
            & 4.546833189860270e-0      & 4.563499345528536e-0   & 4.486347115134618e-0   \\
  \bottomrule
\end{booktabs}
    \endgroup
\end{document}

答案2

这是一个采用老式 [!]tabular环境的解决方案。采用此方法的优点是,可以非常轻松地将单个单元格的内容置于S-type 列的中心:只需将单元格内容括在花括号中即可。

在此处输入图片描述

\documentclass[border=2pt]{standalone}

\usepackage{booktabs,siunitx,amsmath}
\sisetup{per-mode=symbol}

\newcommand{\deltav}[1]{\ensuremath{\Delta V#1}}
%\newcommand{\unitE}{\ensuremath{\text{km}^{2}/\text{s}^{2}}}
\newcommand\unitE{\unit{\kilo\meter\squared\per\second\squared}}
\providecommand\degrees[1]{\ensuremath{#1^\circ}} % for degrees symbol
\providecommand{\ccc}{\text{C3}}
\providecommand{\rla}{\text{RLA}}
\providecommand{\dla}{\text{DLA}}

\newcolumntype{H}[2]{S[table-format=#1, scientific-notation = true, 
      round-mode=places, round-precision = #2, 
      group-digits=integer, group-minimum-digits=4, % <-- new options
      exponent-mode = fixed, fixed-exponent = 0]}
\newcolumntype{K}{H{-5.4}{4}} % not '6.4f'

\begin{document}
\begin{tabular}{@{} l K H{-4.4}{4} K @{}}
  \toprule
  & \bfseries\ccc & \bfseries\rla & \bfseries\dla  \\
  % note the additional curly braces in 3 of the 4 cells:
  Shift & {0.025\,\unitE} & {\degrees{0.005}} & {\degrees{0.008}} \\
  \midrule
  B.T, km                   & 14028.09419155        &   982.72457669         & -13477.32067085        \\
  B.R, km                   & -7751.49915236        & -3700.77434474         &   3148.12003385        \\
  \midrule
  \deltav{_\mathrm{x}}, m/s & 4.774015816660711e-01 &  4.126455467703491e-02 & -4.480105352801190e-01 \\
  \deltav{_\mathrm{y}}, m/s & 6.781754913310237e-01 & -7.925658411887511e-02 & -8.241304954028742e-01 \\
  \deltav{_\mathrm{z}}, m/s & 7.649481111877549e-01 &  4.826834479224431e-01 & -1.507332008655069e-01 \\
  \addlinespace %\midrule
  \deltav{}, m/s            & 1.128264100301457     & 4.908845897939598e-01  &  9.500657929944257e-01 \\
  \midrule
  TCM \deltav{}, m/s        & 4.546833189860270e-0  & 4.563499345528536e-0   &  4.486347115134618e-0  \\
  \bottomrule
\end{tabular}
\end{document}

相关内容