0

I have a table where I need to align the numbers by the decimal point. After that I need flush left five columns (from Country to Robust S.E. in the example below) but flush right the last one (p-value). I know I need to use siunitx package but have not been able to make it work. Any help would be greatly appreciated!

Code:

\documentclass{article}
\usepackage{geometry}
\usepackage{booktabs, multirow, siunitx}

\begin{document}
    \begin{table}
\centering
\begin{tabular}{lllllr}
    \toprule
{Country}& {Predictor} & {Estimate} & {Robust Z} & {Robust S.E.} & {p-value} \\ 
    \midrule
\multirow{3}{*}{England}  
& Intercept & 4.776 & 2.511 & 1.902 & 0.012\\
& Non-PIDS & 0.566 &  10.734 & 0.053 & $<$0.001\\
& PIDS &  0.280 & 5.184 & 0.054 & $<$0.001\\
\addlinespace
\multirow{3}{*}{Germany}
 & Intercept & 9.638 & 4.114 & 2.343 & $<$0.001\\
        & Non-PIDS & 0.473 & 6.439& 0.073 & $<$0.001\\
        & PIDS & 0.203 & 3.505 & 0.058 & $<$0.001\\
\addlinespace
\multirow{3}{*}{Netherlands}
 & Intercept & 11.997 & 5.793 & 2.071 & $<$0.001\\
            & Non-PIDS &  0.460 & 7.048 & 0.065 & $<$0.001\\
            & PIDS &  0.14 & 2.380 &  & 0.017\\
\addlinespace
\multirow{3}{*}{Sweden}
 & Intercept & 11.723 & 2.452 & 4.782 & $<$0.001\\
        & Non-PIDS & 0.448 & 0.077 & 5.799 & $<$0.001\\
        & PIDS & 0.176 & 0.054 & 3.232 & 0.001\\
    \bottomrule
\end{tabular}
    \end{table}
\end{document}

The output I got (numbers are not aligned by the decimal point): enter image description here

2 Answers 2

1

A solution with tabularray package. It uses the libraries booktabs and siunitx to imitate the packages with the same names.

\begin{document}
\lipsum[1]
\begin{table}[ht]\centering
\caption{with \texttt{tabularray}.}
\begin{tblr}
{
  colspec={llQ[si={table-format=2.3},l]
             Q[si={table-format=2.3},l]
             Q[si={table-format=1.3},l]
             Q[si={table-format=<1.3},r]},
  row{1}={guard},
  row{4,7,10}={belowsep+=4pt}
}
\toprule
Country & Predictor & Estimate & Robust Z & Robust S.E. & p-value\\ 
\midrule
            & Intercept &  4.776 &  2.511 & 1.902 &  0.012\\
England     & Non-PIDS  &  0.566 & 10.734 & 0.053 & <0.001\\
            & PIDS      &  0.280 &  5.184 & 0.054 & <0.001\\
%
            & Intercept &  9.638 &  4.114 & 2.343 & <0.001\\
Germany     & Non-PIDS  &  0.473 &  6.439 & 0.073 & <0.001\\
            & PIDS      &  0.203 &  3.505 & 0.058 & <0.001\\
%
            & Intercept & 11.997 &  5.793 & 2.071 & <0.001\\
Netherlands & Non-PIDS  &  0.460 &  7.048 & 0.065 & <0.001\\
            & PIDS      &  0.14  &  2.380 &       &  0.017\\
%
            & Intercept & 11.723 &  2.452 & 4.782 & <0.001\\
Sweden      & Non-PIDS  &  0.448 &  0.077 & 5.799 & <0.001\\
            & PIDS      &  0.176 &  0.054 & 3.232 & <0.001\\
\bottomrule
\end{tblr}
\end{table}

\lipsum[2]
\end{document}

enter image description here

5
  • 1
    Thanks for the answer@Juan Castano but the 3rd to 5th columns are not flushed left
    – cliu
    Commented Jul 17, 2023 at 16:37
  • @cliu, yep, I forgot that but it's done now, see my edit Commented Jul 17, 2023 at 16:57
  • Why spacing the lines like that?
    – egreg
    Commented Jul 17, 2023 at 19:38
  • @egreg, only for visibility, I think the code is more clear Commented Jul 17, 2023 at 20:04
  • I think it’s the opposite
    – egreg
    Commented Jul 17, 2023 at 20:18
1

I'm not sure where the problem is.

I wouldn't use \multirow, which makes the table less, rather than more clear.

\documentclass{article}
\usepackage{geometry}
\usepackage{booktabs,siunitx}

\begin{document}

\begin{table}[htp]
\centering

\begin{tabular}{
  @{}
  ll
  S[table-format=2.3]
  S[table-format=2.3]
  S[table-format=1.3]
  r
  @{}
}
\toprule
Country& {Predictor} & {Estimate} & {Robust Z} & {Robust S.E.} & {p-value} \\ 
\midrule
England
& Intercept &  4.776 &  2.511 & 1.902 & 0.012\\
& Non-PIDS  &  0.566 & 10.734 & 0.053 & $<$0.001\\
& PIDS      &  0.280 &  5.184 & 0.054 & $<$0.001\\
\addlinespace
Germany
& Intercept &  9.638 &  4.114 & 2.343 & $<$0.001\\
& Non-PIDS  &  0.473 &  6.439 & 0.073 & $<$0.001\\
& PIDS      &  0.203 &  3.505 & 0.058 & $<$0.001\\
\addlinespace
Netherlands
& Intercept & 11.997 &  5.793 & 2.071 & $<$0.001\\
& Non-PIDS  &  0.460 &  7.048 & 0.065 & $<$0.001\\
& PIDS      &  0.14  &  2.380 &       & 0.017\\
\addlinespace
Sweden
& Intercept & 11.723 &  2.452 & 4.782 & $<$0.001\\
& Non-PIDS  &  0.448 &  0.077 & 5.799 & $<$0.001\\
& PIDS      &  0.176 &  0.054 & 3.232 & 0.001\\
\bottomrule
\end{tabular}

\caption{Some caption to this table, don't forget it}

\end{table}

\end{document}

enter image description here

Bear in mind that tabularray is very slow and you don't really need it for such a table.

You must log in to answer this question.

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