4

I would like to align numbers using the siunitx package. And, as some of the numbers are in parentheses, I looked in section 9.9 of the documentation, where I get the impression that the code below would do what I want. But as you can see from the screenshot below, the opening parentheses overlap with the first digits in the second row of the table body.

What to do?

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

Output

5
  • I do think you need to couint in space for the separator into the column specifier, aka 6.0 not 5.0 for the first column. But it gets worse with the others. This out to be something Joseph needs to look at. It looks fine if yuo jsut bump each "size" by one.
    – daleif
    Commented Nov 7, 2023 at 14:04
  • 3
    S[table-format={(}5.0{)}] S[table-format={(}6.0{)}] S[table-format={(}7.0{)}] S[table-format={(}8.0{)}]? Commented Nov 7, 2023 at 14:11
  • @daleif But with the "bump" the columns instead become a bit too wide and the centered headings look unbalanced (which admittedly is a smaller problem than the one shown in the question).
    – Fredrik P
    Commented Nov 7, 2023 at 14:29
  • @Qrrbrbirlbel It works well! Transform Your comment in an answer. Commented Nov 7, 2023 at 14:30
  • @Qrrbrbirlbel That works! However, the parentheses are then part of the width of the "column model". But I can't really decide if that's a good or a bad thing :-/
    – Fredrik P
    Commented Nov 7, 2023 at 14:30

1 Answer 1

5

Code

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

Output

enter image description here

You must log in to answer this question.

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