Right-align a multicolumn table header that stretched over a left-aligned column

I am trying to right-align a multicolumn table heading, where one of the columns it straddles is aligned on a decimal point with dcolumn, and the other is left-aligned. In the example below, I would like the closing bracket of "Difference (p-value)" to be flush with the closing brackets of the column below it. I have fiddled with spacing between columns and creative mixing of the general alignment "rcl" etc. Does anyone have other tips?

\documentclass{article}
\usepackage{dcolumn}
\newcolumntype{Q}{D{.}{.}{2}}

\begin{document}
\begin{tabular}{lQQQ@{\hspace{0.1em}}l}
\hline
Variable & \multicolumn{1}{c}{Period 1} & \multicolumn{1}{c}{Period 2} & \multicolumn{2}{r}{Difference (p-value)}  \\
\hline
var0 & 6.00 & 8.00 & 2.00 & (0.00) \\
var1 & 0.61 & 0.73 & 0.12 & (0.00) \\
var2 & 359.00 & 404.00 & 44.43 & (0.03) \\

\hline
\end{tabular}
\end{document}

• Welcome to TeX SX! Do you have specific reasons for such a thin space between columns 3 and 4? Oct 5 '19 at 13:04
• Welcome to TeX.SE! What meaning has numbers in the last(fifth) column? Are they uncertainty of values in the fourth column? If they are, they notation is wrong, on such a way they can not be decimal numbers. With decimal numbers you can write as for example 2.00 \pm 0.00. Oct 5 '19 at 13:04
• Thanks for the questions. The numbers in parantheses are p-values from a t-test of the difference between the columns. I do not want them to be separate columns because there are a few tables where I will have more than one of these difference(p-value) columns. Oct 5 '19 at 16:40

1 Answer

Some guessing what you like to have (with use of the siunitx package instead of dcolumn):

\documentclass{article}
\usepackage{siunitx}

\begin{document}
\begin{tabular}{l*{2}{S[table-format=3.2]}
S[table-format=2.2(1),
table-alignment=right,
separate-uncertainty,
]
}
\hline
Variable  & {Period 1} & {Period 2} & {Difference (p-value)}  \\
\hline
var0       &   6.00    &   8.00    &  2.00(0)   \\
var1       &   0.61    &   0.73    &  0.12(0)   \\
var2       & 359.00    & 404.00    & 44.43(3)   \\
\hline
\end{tabular}

\bigskip
\begin{tabular}{l*{2}{S[table-format=3.2]}
S[table-format=2.2(1),
table-alignment=right,
]
}
\hline
Variable  & {Period 1} & {Period 2} & {Difference (p-value)}  \\
\hline
var0       &   6.00    &   8.00    &  2.00(0)   \\
var1       &   0.61    &   0.73    &  0.12(0)   \\
var2       & 359.00    & 404.00    & 44.43(3)   \\
\hline
\end{tabular}

\bigskip
\begin{tabular}{l*{2}{S[table-format=3.2]}
S[table-format=8.0,
]
%@{\hskip-2em}
S[table-format=1.2,
table-space-text-pre=\,(,
table-space-text-post=),
input-symbols = {( )}
]
}
\hline
Variable  & {Period 1} & {Period 2} & \multicolumn{2}{r}{Difference (p-value)}  \\
\hline
var0       &   6.00    &   8.00    &  2.00 & (0.00)   \\
var1       &   0.61    &   0.73    &  0.12 & (0.00)   \\
var2       & 359.00    & 404.00    & 44.43 & (0.03)   \\
\hline
\end{tabular}
\end{document}


Correct notations are in the first two examples.

However, I would wrote p value as separate column:

\documentclass{article}
\usepackage{siunitx}

\begin{document}
\begin{tabular}{l *{2}{ S[table-format=3.2] }
S[table-format=2.2]
S[table-format=1.2,
table-space-text-pre={\,(},
table-space-text-post=),
input-symbols = {( )}]
}
\hline
Variable  & {Period 1} & {Period 2} & {Difference} & {(p-value)}  \\
\hline
var0       &   6.00    &   8.00    &  2.00 & (0.00)   \\
var1       &   0.61    &   0.73    &  0.12 & (0.00)   \\
var2       & 359.00    & 404.00    & 44.43 & (0.03)   \\
\hline
\end{tabular}
\end{document}

• Thank you! Versions 2 and 3 get to what I want. Oct 5 '19 at 17:07