将数字与括号中的组分隔符对齐

将数字与括号中的组分隔符对齐

我想使用siunitx包对齐数字。由于有些数字在括号中,所以我查看了文档第 9.9 节,我感觉下面的代码可以实现我想要的效果。但是从下面的截图中可以看到,左括号与表体第二行的第一位数字重叠。

该怎么办?

\documentclass{article}
\usepackage{booktabs,siunitx}
\begin{document}
  \begin{table}
    \centering
    \caption{Regression tables\label{tab:regression}}
    \sisetup{
      input-open-uncertainty=,
      input-close-uncertainty=,
      table-align-text-before=false,
      group-minimum-digits=3,
      group-separator={,},
    }
    \begin{tabular}{
      @{}
      S[table-format=5.0]
      S[table-format=6.0]
      S[table-format=7.0]
      S[table-format=8.0]
      @{}
    }
      \toprule
      {Header} & {Header} & {Header} & {Header} \\
      \midrule
       1234 & 12345 & 123456 & 1234567 \\
      (12345)& (123456)& (1234567) & (12345678) \\
       123 & 1234 & 12345 & 123456 \\
      (1234)& (12345)& (123456) & (1234567) \\
      \bottomrule
    \end{tabular}
  \end{table}
\end{document}

输出

答案1

代码

\documentclass{article}
\usepackage{booktabs, siunitx, calc}
\begin{document}
\sisetup{
  input-open-uncertainty=,
  input-close-uncertainty=,
  table-align-text-before=false,
  group-minimum-digits=3,
  group-separator={,},
}

Without parentheses:\par
\begin{tabular}{
  @{}
  S[table-format=5.0]
  S[table-format=6.0]
  S[table-format=7.0]
  S[table-format=8.0]
  @{}
}
  \toprule
  {Header} & {Header} & {Header} & {Header} \\ \midrule
  1234     & 12345    & 123456   & 1234567  \\
  12345    & 123456   & 1234567  & 12345678 \\
  123      & 1234     & 12345    & 123456   \\
  1234     & 12345    & 123456   & 1234567  \\ \bottomrule
\end{tabular}
\medskip

Specify extra digit (but only in the front?):\par
\begin{tabular}{
  @{}
  S[table-format=6.0]
  S[table-format=7.0]
  S[table-format=8.0]
  S[table-format=9.0]
  @{}
}
  \toprule
    {Header} & {Header} & {Header}  & {Header}   \\ \midrule
    1234     & 12345    & 123456    & 1234567    \\
    (12345)  & (123456) & (1234567) & (12345678) \\
    123      & 1234     & 12345     & 123456     \\
    (1234)   & (12345)  & (123456)  & (1234567)  \\ \bottomrule
\end{tabular}
\medskip

Add \verb|{(}| and \verb|{)}| to \verb|table-format| and smaller \verb|\tabcolsep|:\par
{% \widthof needs calc package
 % extra set of {} to scope change to \tabcolsep
  \setlength\tabcolsep{\tabcolsep-\widthof{(}}
  \begin{tabular}{
    @{}
    S[table-format={(}5.0{)}]
    S[table-format={(}6.0{)}]
    S[table-format={(}7.0{)}]
    S[table-format={(}8.0{)}]
    @{}
  }
    \toprule
    {Header} & {Header} & {Header}  & {Header}   \\ \midrule
    1234     & 12345    & 123456    & 1234567    \\
    (12345)  & (123456) & (1234567) & (12345678) \\
    123      & 1234     & 12345     & 123456     \\
    (1234)   & (12345)  & (123456)  & (1234567)  \\ \bottomrule
  \end{tabular}
}% back to normal \tabcolsep
\medskip

Add \verb|{(}| and \verb|{)}| to \verb|table-format| and normal \verb|\tabcolsep|:\par
\begin{tabular}{
  @{}
  S[table-format={(}5.0{)}]
  S[table-format={(}6.0{)}]
  S[table-format={(}7.0{)}]
  S[table-format={(}8.0{)}]
  @{}
}
  \toprule
  {Header} & {Header} & {Header}  & {Header}   \\ \midrule
  1234     & 12345    & 123456    & 1234567    \\
  (12345)  & (123456) & (1234567) & (12345678) \\
  123      & 1234     & 12345     & 123456     \\
  (1234)   & (12345)  & (123456)  & (1234567)  \\ \bottomrule
\end{tabular}
\end{document}

输出

在此处输入图片描述

相关内容