3

I have adapted a table format recently suggested on t.SE, which works well except for two issues. MWE is below.

\usepackage{array}       
\usepackage{dcolumn}
\usepackage{tabularx}
\usepackage{booktabs}
\newcolumntype{d}[1]{D{.}{.}{#1}}    
\usepackage{siunitx}
\begin{document}
\begin{table}
\begin{tabularx}{\textwidth}{p{2.5cm}X S[table-format=3,table-column-width=8mm] 
                 *{6}{S[table-format=2.2,table-column-width=15mm]}
                  }
    \toprule
\multicolumn{2}{c}{}  &\multicolumn{2}{c}{AGE}  &  \multicolumn{2}{c}{BDI} &   \multicolumn{2}{c}{STAIG} \\
    \cmidrule(lr){3-4}\cmidrule(lr){5-6}\cmidrule(lr){7-8}
\multicolumn{1}{c}{GROUP}  & \multicolumn{1}{c}{n} & \multicolumn{1}{c}{mean} & \multicolumn{1}{c}{sd} & \multicolumn{1}{c}{mean} & \multicolumn{1}{c}{sd} &   \multicolumn{1}{c}{mean} &   \multicolumn{1}{c}{sd}       \\
    \midrule
        \addlinespace
\multicolumn{1}{c}{females}   &   \multicolumn{5}{c}{}                      \\
    \addlinespace
\multicolumn{1}{c}{(NS)} & 97 & 46.22 & 11.83 & 20.82 & 10.93  & 55.27 & 11.42    \\
\multicolumn{1}{c}{(DW)} & 47 & 41.55 & 11.07 & 24.30 & 9.74  &  60.02 & 10.69    \\
\multicolumn{1}{c}{(SI)} & 124 & 39.53 & 11.73 & 27.21 & 9.77  &  60.77 & 8.64    \\
\multicolumn{1}{c}{(NVA)} & 58 & 40.44 & 11.72 & 25.90 & 11.88  &  58.55 & 11.50    \\
\multicolumn{1}{c}{(VA)}  & 40 & 39.88 & 11.47 & 28.38 & 11.76  & 57.83 & 11.19    \\
    \bottomrule
\end{tabularx}
\end{table}

When the table is rendered one sees that:

  1. the column headed n (containing integers) is right justified rather than aligned by the decimal point.

  2. The subheadings mean and sd under headings AGE and BDI are shifted left slightly.

I looked at a similar t.SE post:

How to get siunitx to centre columns of integers correctly

but what was suggested there doesn't improve my table alignment.

I spent spare moments yesterday trying to tweak the table to get rid of these problems but without really solving them. It's most likely my lack of knowledge regarding the use of tabularx and siunitx, but can anyone suggest (a)solution(s)?

EDIT: Editing the existing code (see egreg's answer) to the following solved the white space issue.

\begin{tabular}{
  >{\centering}c
  S[table-format=3] 
  *{6}{S[table-format=2.2]}
4
  • tabularx is all about line breaking within table cells it really shouldn't be used for numeric tables where there is no possibility of line breaking. – David Carlisle May 3 '16 at 8:07
  • I guess my error then is seeking ways of formatting a table without being critical enough about the solutions offered. Perhaps in this case tabular* would have been better. – Steve Hodgkinson May 3 '16 at 8:37
  • Personally I'd use tabular. egreg answers the problem as asked but the only thing tabularx is doing there is separating the left hand column from the data, so making the table harder to read, it would be easier to read if the table was just natural width and centred. – David Carlisle May 3 '16 at 8:42
  • @DavidCarlisle I agree and indeed I provided two alternative inputs without tabularx. – egreg May 3 '16 at 9:03
3

You're overspecificating: stating column widths is usually unnecessary; you also have p{2.5cm}X which means two column specifiers.

If you want that the first column takes all the available space, use X; for getting centered entries, >{\centering}X.

\documentclass{article}
\usepackage{array}       
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}

\begin{table}
\begin{tabularx}{\textwidth}{
  >{\centering}X
  S[table-format=3] 
  *{6}{S[table-format=2.2]}
}
\toprule
  & & \multicolumn{2}{c}{AGE}  &  \multicolumn{2}{c}{BDI} &   \multicolumn{2}{c}{STAIG} \\
\cmidrule(lr){3-4}\cmidrule(lr){5-6}\cmidrule(lr){7-8}
\multicolumn{1}{c}{GROUP}  & $n$ & {mean} & {sd} & {mean} & {sd} & {mean} & {sd} \\
\midrule
\addlinespace
females & \\
\addlinespace
(NS)  &  97 & 46.22 & 11.83 & 20.82 & 10.93 & 55.27 & 11.42 \\
(DW)  &  47 & 41.55 & 11.07 & 24.30 &  9.74 & 60.02 & 10.69 \\
(SI)  & 124 & 39.53 & 11.73 & 27.21 &  9.77 & 60.77 &  8.64 \\
(NVA) &  58 & 40.44 & 11.72 & 25.90 & 11.88 & 58.55 & 11.50 \\
(VA)  &  40 & 39.88 & 11.47 & 28.38 & 11.76 & 57.83 & 11.19 \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

enter image description here

The same with tabular so the excess space is outside the table: there's no need the table is as wide as the page block.

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

\usepackage{lipsum} % just for the example

\begin{document}

\lipsum[2]

\begin{table}[htp]
\centering

\caption{Some caption to the table}
\medskip

\begin{tabular}{
  c
  S[table-format=3] 
  *{6}{S[table-format=2.2]}
}
\toprule
  & & \multicolumn{2}{c}{AGE}  &  \multicolumn{2}{c}{BDI} &   \multicolumn{2}{c}{STAIG} \\
\cmidrule(lr){3-4}\cmidrule(lr){5-6}\cmidrule(lr){7-8}
\multicolumn{1}{c}{GROUP}  & $n$ & {mean} & {sd} & {mean} & {sd} & {mean} & {sd} \\
\midrule
\addlinespace
females & \\
\addlinespace
(NS)  &  97 & 46.22 & 11.83 & 20.82 & 10.93 & 55.27 & 11.42 \\
(DW)  &  47 & 41.55 & 11.07 & 24.30 &  9.74 & 60.02 & 10.69 \\
(SI)  & 124 & 39.53 & 11.73 & 27.21 &  9.77 & 60.77 &  8.64 \\
(NVA) &  58 & 40.44 & 11.72 & 25.90 & 11.88 & 58.55 & 11.50 \\
(VA)  &  40 & 39.88 & 11.47 & 28.38 & 11.76 & 57.83 & 11.19 \\
\bottomrule
\end{tabular}

\end{table}

\lipsum[3]

\end{document}

enter image description here

With tabular* this particular table doesn't look bad, but only because its natural width is not so shorter than the text width.

\begin{tabular*}{\textwidth}{
  @{\extracolsep{\fill}}
  c
  S[table-format=3]
  *{6}{S[table-format=2.2]}
}
[...]
\end{tabular*}

enter image description here

4
  • Ok. I guess defining the column widths was my blunt attempt to reduce the space between the first and second columns. With my earlier version this space appears disproportionatly large (similar to your rendering) in my extended (manuscript) version. Is it possible to reduce the white space left and right from column 1 within the existing column format? – Steve Hodgkinson May 3 '16 at 8:28
  • Taking David's suggestion above and accepting egreg's answer was based on my limited Information, I changed tabularx to tabular and the X column format to c (with centering but no textwidth specification. – Steve Hodgkinson May 3 '16 at 8:58
  • @SteveHodgkinson I added it, too, together with a tabular* proposal. But my advice is just leaving tables to their natural width, unless you have a series of them, with slightly different widths. Narrow space between columns eases reading. – egreg May 3 '16 at 9:04
  • yep, the tabular 'solution' seems to work best. – Steve Hodgkinson May 3 '16 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.