# center values in table in latex

I have a problem in the table I created using latex.

The problem is that the values in the columns are not centered correctly. THe values with a minus sign in front just are further right than the rest. Is it possible to center them around the decimal point so as to look neater?

-
A little tip: When you have many of the same column type, you can use \begin{tabular}{*{<number of cols>}{<column type>}}, so for your example, \begin{tabular}{L *{8}{C}}. It requires the array package, but siunitx loads array, so you don't have to add it explicitly. –  Torbjørn T. Apr 30 at 0:02
Why did you delete your MWE? –  Mico May 9 at 17:42

You are already using a package capable of that. siunitx introduces a column specifier S, which aligns the columns to the decimal point and formats the numbers to your \sisetup settings.

I optimized your code a little further:

\documentclass{article}
\usepackage{amsmath}
\usepackage{pdflscape}
\usepackage{booktabs}
\usepackage{siunitx}
\newcommand{\nm}[1]{\textnormal{#1}}
\begin{document}
\begin{landscape}
\begin{table}[h!]
\centering
\begin{tabular}{lSSSSSSSS}
\toprule
\multicolumn{1}{l}{Model} &\multicolumn{4}{r}{Model1}    &\multicolumn{2}{r}{Model2}    &\multicolumn{2}{r}{Model3}    \\
\cmidrule(lr){2-5}\cmidrule(lr){6-7}\cmidrule(lr){8-9}
& {$n=1$} & {$n=2$} & {$n=3$} & {$n=4$} & {$n=5$} & {$n=6$} & {$n=7$} & {$n=8$} \\
\midrule
Mean(alg)$^*$             & 1.281   & 1.031   & 0.779   & 0.631   & 0.886   & 0.727   & 1.324   & 1.3145  \\
Median(alg)               & 0.281   & 0.223   & 0.214   & 0.192   & 0.177   & 0.162   & 0.242   & 0.228   \\
Mean(length) in $\rho$    & 11      & 91      & 327     & 489     & 76      & 64      & 86      & 90      \\
Median(length) in  $\rho$ & 5.3     & 39      & 210     & 400     & 37      & 32      & 31.7    & 35      \\
Cor(alg,deh)              & -0.332  & -0.30   & -0.31   & -0.25   & -0.28   & -0.31   & -0.20   & -0.24   \\
\midrule
$^*$  \\
\bottomrule
\end{tabular}
\caption{Mean}
\label{beta}
\end{table}
\end{landscape}
\end{document}


# Alternative

Now every cell is left justified, but you will have to replace all - with \mi.

\documentclass{article}
\usepackage{mathtools}
\usepackage{pdflscape}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{array}
\newcolumntype{L}{>{\phantom{$\mathbin{-}$}$}l<{$}}
\newcommand{\mi}{\mathllap{\mathbin{-}}}
\newcommand{\nm}[1]{\textnormal{#1}}
\begin{document}
\begin{landscape}
\begin{table}[h!]
\centering
\begin{tabular}{lLLLLLLLL}
\toprule
\multicolumn{1}{l}{Model} &\multicolumn{4}{r}{Model1}    &\multicolumn{2}{r}{Model2}   &\multicolumn{2}{r}{Model3}    \\
\cmidrule(lr){2-5}\cmidrule(lr){6-7}\cmidrule(lr){8-9}
& n=1      & n=2     & n=3     & n=4     & n=5     & n=6     & n=7     & n=8 \\
\midrule
Mean(alg)$^*$             & 1.281    & 1.031   & 0.779   & 0.631   & 0.886   & 0.727   & 1.324   & 1.3145  \\
Median(alg)               & 0.281    & 0.223   & 0.214   & 0.192   & 0.177   & 0.162   & 0.242   & 0.228   \\
Mean(length) in $\rho$    & 11       & 91      & 327     & 489     & 76      & 64      & 86      & 90      \\
Median(length) in  $\rho$ & 5.3      & 39      & 210     & 400     & 37      & 32      & 31.7    & 35      \\
Cor(alg,deh)              & \mi0.332 & \mi0.30 & \mi0.31 & \mi0.25 & \mi0.28 & \mi0.31 & \mi0.20 & \mi0.24   \\
\midrule
$^*$ \\
\bottomrule
\end{tabular}
\caption{Mean}
\label{beta}
\end{table}
\end{landscape}
\end{document}


-
hello..this looks sometimes a bit chaotic...is there any way to just center the values in each column so that none is going to the right or left of the other values in the column ? or just all align them right so that they are in a single line to the right? –  user3069326 Apr 30 at 9:59
@user3069326 See my updated answer. –  Henri Menke Apr 30 at 16:51

Yes. The package dcolumn was designed exactly for this. See the short and easy documentation (2 pages) here: http://www.ctan.org/pkg/dcolumn

-

Here's a solution using the dcolumn package. In addition to using the d column type for the 8 columns with numbers, I would recommend using l for the first column.

\documentclass{article}
\usepackage[margin=1in]{geometry} % choose margins to suit your needs
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D..{#1}}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % a handy shortcut macro
\begin{document}
%\begin{landscape} % not needed for this example
\begin{table} [h!]
\centering
\begin{tabular}{ l *{2}{d{2.3}} *{2}{d{3.3}} *{3}{d{2.3}} d{2.4} }
\toprule
& \multicolumn{4}{c}{Model 1}
& \multicolumn{2}{c}{Model 2}
& \multicolumn{2}{c}{Model 3} \\
\cmidrule(lr){2-5} \cmidrule(lr){6-7} \cmidrule(l){8-9}
& \mc{$n=1$} & \mc{$n=2$} & \mc{$n=3$} & \mc{$n=4$}
& \mc{$n=5$} & \mc{$n=6$} & \mc{$n=7$} & \mc{$n=8$} \\
\midrule
Mean(alg)$^*$  & 1.281 & 1.031 & 0.779& 0.631 &0.886& 0.727& 1.324&1.3145\\
Median(alg) &0.281 & 0.223 & 0.214 & 0.192& 0.177&0.162 & 0.242&0.228\\
Mean(length) in $\rho$  &11 & 91 & 327 & 489 &76&64&86&90\\
Median(length) in $\rho$  &5.3 & 39 & 210 & 400&37&32&31.7&35 \\
Cor(alg,deh)  &-0.332 & -0.30 & -0.31 & -0.25 &-0.28&-0.31 & -0.20&-0.24\\
\midrule[\heavyrulewidth]
\multicolumn{9}{l}{$^*$ \footnotesize Some explanations} \\
\bottomrule
\end{tabular}
\caption{Mean} \label{beta}
\end{table}
%\end{landscape}
\end{document}

-