# Horizontal centering in table

Consider the following example:

\documentclass{article}

\usepackage{booktabs,dcolumn}
\usepackage{siunitx}

\newcolumntype{d}[1]{D{.}{,}{#1}}
\newcommand*\mc[1]{\multicolumn{1}{c}{#1}}

\begin{document}

\begin{table}[htbp]
\centering
\caption{Something.}
\label{tbl:1}
\begin{tabular}{d{2.1} d{2.0} d{3.0} d{2.0} d{3.0}}
\toprule
\mc{S{\o}vnm{\ae}ngde}                           &
\multicolumn{2}{c}{Eleverne fra $9$.~A}          &
\multicolumn{2}{c}{Eleverne fra $9$.~B}                                           \\
\midrule
& \mc{Abs.} & \mc{Rel.}          & \mc{Abs.} & \mc{Rel.}          \\
\mc{\si{\hour}} & \mc{---}  & \mc{\si{\percent}} & \mc{---}  & \mc{\si{\percent}} \\
\midrule
6.5            &  1        &   4                &  0        &   0                \\
7.0            &  4        &  16                &  2        &  10                \\
7.5            &  3        &  12                &  3        &  15                \\
8.0            &  8        &  32                &  9        &  45                \\
8.5            &  5        &  20                &  3        &  15                \\
9.0            &  2        &   8                &  3        &  15                \\
9.5            &  1        &   4                &  0        &   0                \\
10.0            &  1        &   4                &  0        &   0                \\
\midrule
\mc{I alt}      & 25        & 100                & 20        & 100                \\
\bottomrule
\end{tabular}
\end{table}

\end{document}


Why are the second and third columns not horizontally centered relative to "Eleverne fra 9. A" and the fourth and fifth columns not horizontally centered relative to "Eleverne fra 9. B"? (They need to be pushed slightly to the right.)

Update

I would like the width of the second and third column to be half the width of the box containing "Eleverne fra 9. A", and the width of the fourth and fifth column to be half the width of the box containing "Eleverne fra 9. B".

Update 2

I now have the following:

\documentclass{article}

\usepackage{booktabs,dcolumn}
\usepackage{siunitx}

\newcommand*\mc[1]{\multicolumn{2}{c}{Eleverne fra $9$.~#1}}

\begin{document}

\begin{table}
\def\spc{\hspace{0.8em}}
\centering
\caption{Something.}
\label{tbl:1}
\begin{tabular}{
S[table-format = 2.1]
>{\spc}S[table-format = 2]
S[table-format = 3]
>{\spc}S[table-format = 2]
S[table-format = 3]
}
\toprule
{S{\o}vnm{\ae}ngde} & \mc{A} & \mc{B}          \\
\midrule
\si{\hour} & {Abs.} & {Rel.} & {Abs.} & {Rel.} \\
\midrule
6.5       &  1\spc &   4    &  0\spc &   0    \\
7         &  4\spc &  16    &  2\spc &  10    \\
7.5       &  3\spc &  12    &  3\spc &  15    \\
8         &  8\spc &  32    &  9\spc &  45    \\
8.5       &  5\spc &  20    &  3\spc &  15    \\
9         &  2\spc &   8    &  3\spc &  15    \\
9.5       &  1\spc &   4    &  0\spc &   0    \\
10         &  1\spc &   4    &  0\spc &   0    \\
\midrule
& 25\spc & 100    & 20\spc & 100    \\
\bottomrule
\end{tabular}
\end{table}

\end{document}


My problem is that I get a lot of warnings;

Overfull \hbox (8.00003pt too wide) detected at line XXX


How do I get rid of these?

• Do you mean horizontally centered instead of vertically? – Aditya Aug 25 '13 at 23:54
• @Aditya Of course. :) – Svend Tveskæg Aug 26 '13 at 0:05
• Perhaps you meant this \begin{tabular}{d{2.1} d{1.0} d{2.0} d{1.0} d{2.0}} for your tabular format specification. – Gonzalo Medina Aug 26 '13 at 0:45
• @GonzaloMedina Not really. I have asked a new question in a comment to Xavier's answer. – Svend Tveskæg Aug 26 '13 at 0:55
• Adjust the decimals: {|d{2.1}|d{4.2}|d{3.1}|d{3.2}|d{3.1}|} say. Will it be OK for you? – user11232 Aug 26 '13 at 5:28

Just draw vertical rules between columns (with |, i.e. {|d{2.1}|d{2.0}|d{3.0}|d{2.0}|d{3.0}|} as columns definition) to understand what's happening:
• Okay, this looks right, but then the second and third (fourth and fifth, resp.) column should have the same width. Is it possible to get this automatucally, i.e., without have to specify a fixed width using, e.g., p{2cm}? – Svend Tveskæg Aug 26 '13 at 0:54
• Actually, the width of the second and third column has to be half the width of the box containing Eleverne fra 9. A. – Svend Tveskæg Aug 26 '13 at 1:02
• @SvendTveskæg Make columns 2 to 5 to be of a (large enough) fixed width, aligned to the right (use the search function here on TeX.SX). No need to use D columns if you are not aligning on the decimal point... – Xavier Aug 26 '13 at 1:23