如何使用 siunitx 获取格式化为固定数量的有效数字的数字以在表格中对齐?

如何使用 siunitx 获取格式化为固定数量的有效数字的数字以在表格中对齐?

我有包含不同量级的数字的表格。有些数字像 30.3542,而另一些像 -145283.68447。我希望能够使用与下面示例中相同的表格设置来格式化这些数字。我想限制有效数字的数量,因此 30.3542 将显示为 30.35,而 -145283.68447 将显示为 -142800。我对尾随小数点没有偏好——带或不带小数点对我来说都可以。

编辑 2023-Mar-29:我想\newcolumntype对两个表使用相同的定义。因此,我希望\newcolumntype定义不包含任何参数。

我认为这些数字试图居中,但我更希望它们排列在小数点的位置。如果你能让它们在标题下排列得更整齐,那就太好了!如果这意味着显示小数点,这对我来说不是问题。

我宁愿不必指定table-column-width,但这只是次要偏好。最后,我知道我的示例有些问题,因为我收到警告,说我的水平盒子已满。

\documentclass{article}
\usepackage[landscape]{geometry}

\usepackage{siunitx}
\usepackage{booktabs}
\usepackage[font={bf}]{caption}

\newcommand{\bdr}{\ensuremath{B\cdot R}}
\newcommand{\bdt}{\ensuremath{B\cdot T}}
\newcommand{\ccc}{\ensuremath{\text{C3}}}
\newcommand{\dla}[0]{\ensuremath{\text{DLA}}}
\newcommand{\rla}[0]{\ensuremath{\text{RLA}}}
\newcommand{\lscaption}[3][1ex]{\caption[#2]{#2. \textnormal{#3}\\ \vspace{#1}}}

\newcolumntype{J}{S[table-column-width=48pt, round-mode=figures, round-precision = 4, scientific-notation = false, drop-zero-decimal=false]}
\newcolumntype{N}{S[scientific-notation = true, round-mode = figures, round-precision = 3, table-format=2.2e-1]}

\begin{document}

\begin{table}[htbp]
\lscaption{Differences 3DOF/S vs 6DOF/S}{
comment
}
\centering
\begin{tabular}{c JJN JJN }
\toprule
  & \multicolumn{3}{c}{\rla{} (deg)} & \multicolumn{3}{c}{\dla{} (deg)} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
{LP} & {3DOF/S} & {6DOF/S} & {{$\Gamma$}} & {3DOF/S} & {6DOF/S} & {{$\Gamma$}} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
5 & 350.713178054129741 & 240.210890365454204 & -0.006065115120141 & 30.791196854506090 & 10.874535168638029 & 0.005444809880985 \\
7 & 928.260620916046306 & 171.151157688540609 & -0.006205963124654 & 90.523715806238274 & 60.963950626382041 & 0.000799818279179 \\
76 & 654.530016288355160 & 294.415387871340319 & -0.009662137107410 & 76.008951214678270 & 21.007840353605254 & 0.000242347374590 \\
75 & 156.612347494337982 & 362.604374274472301 & -0.060612906449607 & 48.264341469864183 & 36.951974409626670 & -0.000640833734826 \\
25 & 122.284569715601792 & 266.533453573082600 & -0.000142667548177 & 48.790634972518796 & 13.919652659616654 & 0.006431814046446 \\
\bottomrule
\end{tabular}
\label{tbl:diffc7}
\end{table}

\begin{table}[htbp]
\lscaption{Differences 3DOF/S vs 6DOF/S}{
comment
}
\small
\centering
\begin{tabular}{c JJN JJN }
\toprule
  & \multicolumn{3}{c}{\bdt{} (km)} & \multicolumn{3}{c}{\bdr{} (km)} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
{LP} & {3DOF/S} & {6DOF/S} & {{$\Gamma$}} & {3DOF/S} & {6DOF/S} & {{$\Gamma$}} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
2 & -68366.072508240821589 & -47940.593376982723440 & 33425.690144507067478 & 25106.437204604970658 & 3735.270218521665689 & -8135.119969136036299 \\
4 & -43224.208042785342132 & -82570.875759750888465 & 71790.620372230230829 & 24070.884249127772596 & 14813.315829007065580 & -9215.266000230855832 \\
92 & -93558.194401149489926 & -10061.267567114861153 & 8883.660889289663099 & 56694.540725077379747 & 84976.987983216390581 & -5522.007654219346814 \\
93 & -709072.534593535004289 & -68379.506859346867373 & 15207.051039394738680 & 23861.831954689827338 & 41609.705691851832597 & -9424.584740471677877 \\
95 & -979112.989370272739695 & -324434.742112474223309 & 4950.783283617139435 & 41097.048921156019871 & 69835.190677535146849 & -7379.565733537314698 \\
\bottomrule
\end{tabular}
\label{tbl:diffbdt}
\end{table}


\end{document}

LaTeX 表格输出

@User23456234 提到使用tabularray。这是一个工作示例tabularray——这是一个普遍的改进,但它并没有减轻我对格式的担忧。

\documentclass{article}
\usepackage[landscape]{geometry}

\usepackage{tabularray}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage[font={bf}]{caption}
\UseTblrLibrary{booktabs,siunitx}

\newcommand{\bdr}{\ensuremath{B\cdot R}}
\newcommand{\bdt}{\ensuremath{B\cdot T}}
\newcommand{\ccc}{\ensuremath{\text{C3}}}
\newcommand{\dla}[0]{\ensuremath{\text{DLA}}}
\newcommand{\rla}[0]{\ensuremath{\text{RLA}}}
\newcommand{\lscaption}[3][1ex]{\caption[#2]{#2. \textnormal{#3}\\ \vspace{#1}}}

\newcolumntype{J}{S[table-column-width=48pt, round-mode=figures, round-precision = 4, scientific-notation = false, drop-zero-decimal=false]}
\newcolumntype{N}{S[scientific-notation = true, round-mode = figures, round-precision = 3, table-format=2.2e-1]}

\begin{document}

\begin{table}[htbp]
\lscaption{Differences 3DOF/S vs 6DOF/S}{
comment
}
\centering
\begin{tblr}{
    colspec={
      l
      S[table-column-width=48pt, round-mode=figures, round-precision = 4, scientific-notation = false, drop-zero-decimal=false]
      S[table-column-width=48pt, round-mode=figures, round-precision = 4, scientific-notation = false, drop-zero-decimal=false]
      S[scientific-notation = true, round-mode = figures, round-precision = 3, table-format=2.2e-1]
      S[table-column-width=48pt, round-mode=figures, round-precision = 4, scientific-notation = false, drop-zero-decimal=false]
      S[table-column-width=48pt, round-mode=figures, round-precision = 4, scientific-notation = false, drop-zero-decimal=false]
      S[scientific-notation = true, round-mode = figures, round-precision = 3, table-format=2.2e-1]
      },
    cell{1}{2,5} = {c=3}{c},
    }
\toprule
  & {{{\rla{} (deg)}}} & &  & {{{\dla{} (deg)}}} & &  \\
\cmidrule[lr]{2-4} \cmidrule[lr]{5-7}
{{{LP}}} & {{{3DOF/S}}} & {{{6DOF/S}}} & {{{$\Gamma$}}} & {{{3DOF/S}}} & {{{6DOF/S}}} & {{{$\Gamma$}}} \\
\cmidrule[lr]{2-4} \cmidrule[lr]{5-7}
5 & 350.713178054129741 & 240.210890365454204 & -0.006065115120141 & 30.791196854506090 & 10.874535168638029 & 0.005444809880985 \\
7 & 928.260620916046306 & 171.151157688540609 & -0.006205963124654 & 90.523715806238274 & 60.963950626382041 & 0.000799818279179 \\
76 & 654.530016288355160 & 294.415387871340319 & -0.009662137107410 & 76.008951214678270 & 21.007840353605254 & 0.000242347374590 \\
75 & 156.612347494337982 & 362.604374274472301 & -0.060612906449607 & 48.264341469864183 & 36.951974409626670 & -0.000640833734826 \\
25 & 122.284569715601792 & 266.533453573082600 & -0.000142667548177 & 48.790634972518796 & 13.919652659616654 & 0.006431814046446 \\
\bottomrule
\end{tblr}
\label{tbl:diffc7}
\end{table}

\begin{table}[htbp]
\lscaption{Differences 3DOF/S vs 6DOF/S}{
comment
}
\small
\centering
\begin{tblr}{
    colspec={
      l
      S[table-column-width=48pt, round-mode=figures, round-precision = 4, scientific-notation = false, drop-zero-decimal=false]
      S[table-column-width=48pt, round-mode=figures, round-precision = 4, scientific-notation = false, drop-zero-decimal=false]
      S[scientific-notation = true, round-mode = figures, round-precision = 3, table-format=2.2e-1]
      S[table-column-width=48pt, round-mode=figures, round-precision = 4, scientific-notation = false, drop-zero-decimal=false]
      S[table-column-width=48pt, round-mode=figures, round-precision = 4, scientific-notation = false, drop-zero-decimal=false]
      S[scientific-notation = true, round-mode = figures, round-precision = 3, table-format=2.2e-1]
      },
    cell{1}{2,5} = {c=3}{c},
    }
\toprule
  & {{{\bdt{} (km)}}} & & & {{{\bdr{} (km)}}} & &  \\
\cmidrule[lr]{2-4} \cmidrule[lr]{5-7}
{{{LP}}} & {{{3DOF/S}}} & {{{6DOF/S}}} & {{{$\Gamma$}}} & {{{3DOF/S}}} & {{{6DOF/S}}} & {{{$\Gamma$}}} \\
\cmidrule[lr]{2-4} \cmidrule[lr]{5-7}
2 & -68366.072508240821589 & -47940.593376982723440 & 33425.690144507067478 & 25106.437204604970658 & 3735.270218521665689 & -8135.119969136036299 \\
4 & -43224.208042785342132 & -82570.875759750888465 & 71790.620372230230829 & 24070.884249127772596 & 14813.315829007065580 & -9215.266000230855832 \\
92 & -93558.194401149489926 & -10061.267567114861153 & 8883.660889289663099 & 56694.540725077379747 & 84976.987983216390581 & -5522.007654219346814 \\
93 & -709072.534593535004289 & -68379.506859346867373 & 15207.051039394738680 & 23861.831954689827338 & 41609.705691851832597 & -9424.584740471677877 \\
95 & -979112.989370272739695 & -324434.742112474223309 & 4950.783283617139435 & 41097.048921156019871 & 69835.190677535146849 & -7379.565733537314698 \\
\bottomrule
\end{tblr}
\label{tbl:diffbdt}
\end{table}


\end{document}

我的表格带有 tabularray

例如,数字“3735”与其下方的数字不对齐。我希望“3735”的个位刚好位于“14810”的个位上方

这就是我要找的。黄色框只是为了突出显示我更改的列。

突出显示的期望结果

答案1

您不应该使用密钥table-column-width=48pt,而是指定您需要的数字。

\documentclass[draft]{article}
\usepackage[landscape]{geometry}

\usepackage{siunitx}
\usepackage{booktabs}
\usepackage[font={bf}]{caption}

\newcommand{\bdr}{\ensuremath{B\cdot R}}
\newcommand{\bdt}{\ensuremath{B\cdot T}}
\newcommand{\ccc}{\ensuremath{\text{C3}}}
\newcommand{\dla}[0]{\ensuremath{\text{DLA}}}
\newcommand{\rla}[0]{\ensuremath{\text{RLA}}}
\newcommand{\lscaption}[2]{\caption[#1]{#1. \textnormal{#2}}}

\newcolumntype{J}[1]{S[
  table-format=#1,
  round-mode=figures,
  round-precision = 4,
  scientific-notation = false,
  drop-zero-decimal=false
]}
\newcolumntype{N}[1]{S[
  scientific-notation = true,
  round-mode = figures,
  round-precision = 3,
  table-format=#1
]}

\begin{document}

\begin{table}[htbp]
\lscaption{Differences 3DOF/S vs 6DOF/S}{
comment
}
\centering
\begin{tabular}{
  c
  J{3.1}
  J{3.1}
  N{-1.2e-1}
  J{2.2}
  J{2.2}
  N{-1.2e-1}
}
\toprule
  & \multicolumn{3}{c}{\rla{} (deg)} & \multicolumn{3}{c}{\dla{} (deg)} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
{LP} & {3DOF/S} & {6DOF/S} & {{$\Gamma$}} & {3DOF/S} & {6DOF/S} & {{$\Gamma$}} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
 5 & 350.713178054129741 & 240.210890365454204 & -0.006065115120141 & 30.791196854506090 & 10.874535168638029 & 0.005444809880985 \\
 7 & 928.260620916046306 & 171.151157688540609 & -0.006205963124654 & 90.523715806238274 & 60.963950626382041 & 0.000799818279179 \\
76 & 654.530016288355160 & 294.415387871340319 & -0.009662137107410 & 76.008951214678270 & 21.007840353605254 & 0.000242347374590 \\
75 & 156.612347494337982 & 362.604374274472301 & -0.060612906449607 & 48.264341469864183 & 36.951974409626670 & -0.000640833734826 \\
25 & 122.284569715601792 & 266.533453573082600 & -0.000142667548177 & 48.790634972518796 & 13.919652659616654 & 0.006431814046446 \\
\bottomrule
\end{tabular}
\label{tbl:diffc7}
\end{table}

\begin{table}[htbp]
\lscaption{Differences 3DOF/S vs 6DOF/S}{
comment
}
\small
\centering
\begin{tabular}{
  c
  J{-6.0}
  J{-6.0}
  N{1.2e1}
  J{5.0}
  J{5.0}
  N{-1.2e1}
}
\toprule
  & \multicolumn{3}{c}{\bdt{} (km)} & \multicolumn{3}{c}{\bdr{} (km)} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
{LP} & {3DOF/S} & {6DOF/S} & {{$\Gamma$}} & {3DOF/S} & {6DOF/S} & {{$\Gamma$}} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
2 & -68366.072508240821589 & -47940.593376982723440 & 33425.690144507067478 & 25106.437204604970658 & 3735.270218521665689 & -8135.119969136036299 \\
4 & -43224.208042785342132 & -82570.875759750888465 & 71790.620372230230829 & 24070.884249127772596 & 14813.315829007065580 & -9215.266000230855832 \\
92 & -93558.194401149489926 & -10061.267567114861153 & 8883.660889289663099 & 56694.540725077379747 & 84976.987983216390581 & -5522.007654219346814 \\
93 & -709072.534593535004289 & -68379.506859346867373 & 15207.051039394738680 & 23861.831954689827338 & 41609.705691851832597 & -9424.584740471677877 \\
95 & -979112.989370272739695 & -324434.742112474223309 & 4950.783283617139435 & 41097.048921156019871 & 69835.190677535146849 & -7379.565733537314698 \\
\bottomrule
\end{tabular}
\label{tbl:diffbdt}
\end{table}


\end{document}

在此处输入图片描述

相关内容