3

I'm trying center specific cells with tabularray, but I don't see how to override the alignment from siunitx. I want the cells on the second row from the top to be centered, but they are currently aligned at the decimal point.

\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}

which gives me

table of numbers and the cells in the second row are not centered

The most obvious misalignment is the cell containing "0.0250 km2/s2". I want that text centered in the column.

3
  • 2
    Whatever else you do, please replace the utterly unidiomatic definition \newcommand{\unitE}{\ensuremath{\text{km}^{2}/\text{s}^{2}}} with \newcommand\unitE{\unit{\kilo\meter\squared\per\second\squared}}.
    – Mico
    Dec 8, 2023 at 2:42
  • @mico, thank you for this -- I'm still learning siunitx. That command returns "km2s-2". I've found that I can get "km2/s2" using \newcommand\unitE{\unit[per-mode=symbol]{\kilo\meter\squared\per\second\squared}}
    – troy
    Dec 8, 2023 at 15:37
  • You're welcome. Glad you figured out how to get the "per" symbol typeset as a slash. Do also observe that the answer I posted features the instruction \sisetup{per-mode=symbol}, which sets the specific form of the per-mode globally.
    – Mico
    Dec 8, 2023 at 16:16

2 Answers 2

3

You should change booktabs preamble to:

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

Explanation:
By adding guard to specification of the second row, this row not obey siunitx specifications for columns and text in cells in this row are just centered, i.e. after this addition you will get:

enter image description here

However, I would write your table on the following, to my opinion simpler and more in tabularray spirit, way where is considered @Mico comment too:

\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}
4
  • 1
    It would appear that you've added the guard option to both rows 1 and 2, rather than to just row 2, right? Aside: You may also want to change {6.4f} to {-5.4}, and 0.025 \,\unitE{} to 0.025\,\unitE{}, i.e., eliminate the space after "0.025".
    – Mico
    Dec 8, 2023 at 5:03
  • 2
    @Mico, it works also without adding guard to the first row (what I'm a bit surprised, I should check documentation again why) . I didn't interfere in the way how the table content is wrote. To my opinion is quit strange.
    – Zarko
    Dec 8, 2023 at 5:13
  • 1
    @Mico, see edited answer
    – Zarko
    Dec 8, 2023 at 5:21
  • +1. :-) Thanks for applying these changes.
    – Mico
    Dec 8, 2023 at 5:56
3

Here's a solution that employs an old-fashioned [!] tabular environment. The advantage of pursuing this route is that it's trivially easy to center-set the contents of an individual cell in an S-type column: just encase the cell contents in curly braces.

enter image description here

\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}

You must log in to answer this question.

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