# Align numbers with numbers and asterisks in table by decimal point

I have created the following table with numbers and asterisks in it.

Now I would like to align the numbers by decimal points. I included the package dcolumn and defined a new column type: \newcolumntype{d}[1]{D{.}{.}{#1}}.

Then I replaced {ccccc} by {d{2.0}d{1.2}d{2.2}d{2.3}d{1.2}} and ended up with this code:

\documentclass{article}

%tables
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{dcolumn} %align numbers by decimal point
\newcolumntype{d}[1]{D{.}{.}{#1}}

\begin{document}

\begin{longtable}[]{d{2.0}d{1.2}d{2.2}d{2.3}d{1.2}}
\caption{} \label{}\\
\toprule
\textbf{ Item } & \textbf{ \textit{b} } & \textbf{ \textit{t} } & \textbf{ \textit{F} } & \textbf{ \textit{R$^2$} }\\
\midrule
\toprule
\textbf{ Item } & \textbf{ \textit{b} } & \textbf{ \textit{t} } & \textbf{ \textit{F} } & \textbf{ \textit{R$^2$} }\\
\midrule
\bottomrule
\endfoot
\endlastfoot
1 & -1.45$^{***}$ & -7.44$^{***}$ & 55.34 & .18  \\
11 & -.86$^{*}$ & -2.09$^{*}$ & 4.36 & .01  \\
12 & -1.79$^{***}$ & -3.80$^{***}$ & 14.47 & .05  \\
13 & -.56 & -1.34 & 1.80 & .00  \\
61 & -1.79$^{***}$ & -5.85$^{***}$ & 34.20 & .12  \\
62 & -1.00$^{**}$ & -3.17$^{**}$ & 10.05 & .04  \\
63 & -.27 & -.65 & .43 & .00  \\
64 & -.37 & -.97 & .94 & .00  \\
65 & -.34 & -.85 & .73 & .00  \\
66 & 1.05$^{**}$ & 2.70$^{**}$ & 7.30 & .02  \\
67 & -1.12$^{**}$ & -2.90$^{**}$ & 8.40 & .03  \\
72 & -.41 & -1.20 & 1.43 & .00  \\
73 & -.27 & -.82 & .67 & .00  \\
74 & 2.05$^{***}$ & 4.99$^{***}$ & 24.88 & .09  \\
75 & 1.61$^{***}$ & 4.41$^{***}$ & 19.46 & .07  \\
76 & .29 & .91 & .83 & .00  \\
\bottomrule
\end{longtable}

\end{document}


But I get the following error when I run the code:

! Missing $inserted. <inserted text>$
l.24 1 & -1.45$^ {***}$ & -7.44$^{***}$ & 55.34 & .18  \\


Apparently dcolumns doesn't like the math code for the asterisks. What am I missing? Is there a better way to add asterisks?

I also tried siunitx, but I had even more trouble with it (e.g. it also formated the numbers .00 -> 0.00 and the column header was not centered properly). I guess this package is a little overpowered for my usecase.

-

The siunitx approach would be something like

\documentclass{article}

%tables
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{siunitx} %align numbers by decimal point

\begin{document}

\sisetup{table-format = -1.2}
\begin{longtable}[]{S[table-format=2.0]SSSS[table-format = 0.2]}
\caption{} \label{}\\
\toprule
{\textbf{Item}} & {\textbf{\textit{b}}} & {\textbf{\textit{t}}} & {\textbf{\textit{F}}} & {\textbf{\textit{R$^2$}}}\\
\midrule
\toprule
{\textbf{Item}} & {\textbf{\textit{b}}} & {\textbf{\textit{t}}} & {\textbf{\textit{F}}} & {\textbf{\textit{R$^2$}}}\\
\midrule
\bottomrule
\endfoot
\endlastfoot
1 & -1.45{$^{***}$} & -7.44{$^{***}$} & 55.34 & .18  \\
11 & -.86{$^{*}$} & -2.09{$^{*}$} & 4.36 & .01  \\
12 & -1.79{$^{***}$} & -3.80{$^{***}$} & 14.47 & .05  \\
13 & -.56 & -1.34 & 1.80 & .00  \\
61 & -1.79{$^{***}$} & -5.85{$^{***}$} & 34.20 & .12  \\
62 & -1.00{$^{**}$} & -3.17{$^{**}$} & 10.05 & .04  \\
63 & -.27 & -.65 & .43 & .00  \\
64 & -.37 & -.97 & .94 & .00  \\
65 & -.34 & -.85 & .73 & .00  \\
66 & 1.05{$^{**}$} & 2.70{$^{**}$} & 7.30 & .02  \\
67 & -1.12{$^{**}$} & -2.90{$^{**}$} & 8.40 & .03  \\
72 & -.41 & -1.20 & 1.43 & .00  \\
73 & -.27 & -.82 & .67 & .00  \\
74 & 2.05{$^{***}$} & 4.99{$^{***}$} & 24.88 & .09  \\
75 & 1.61{$^{***}$} & 4.41{$^{***}$} & 19.46 & .07  \\
76 & .29 & .91 & .83 & .00  \\
\bottomrule
\end{longtable}

\end{document}


which requires a little alteration of the data positioning in the table, and appropriate application of the table-format option. If you want the final column not to add in the leading zero, set add-integer-zero = false.

-
Thanks for your help! The add-integer-zero = false worked like a charm. However, my attempt to set table-format properly wasn't too successfull: {S[table-format=2.0]S[table-format=+1.2]S[table-format=+2.2]S[table-format=+2.2‌​]S[table-format=0.2]}. The columns are still pretty close to each other and the column headers are not centered accurately. –  deboerk Aug 3 '11 at 23:16
@deboerk: I very deliberately used '-' not '+' in the table-format setting: a '+' will usually be dropped, whereas a '-' is not. For the header centring, the example as-written works for me. Note the use of braces around the header items, so that they are 'hidden' from siunitx. If that fails, you can always use \multicolumn for your headers to really exclude them from processing. –  Joseph Wright Aug 4 '11 at 8:46
Thanks again, now it works out nicely! –  deboerk Aug 4 '11 at 22:34

The dcolumn package already uses math mode to typeset its numbers. Which will give you things like proper minus signs and so on. So your $ for the superscript are actually interpreted as leaving math mode. I suggest fixing the document in the following way: 1. Get rid of all the $ around your stars, e.g.

1 & -1.45^{***} & -7.44^{***} & 55.34 & .18  \\

2. Treat stars like digits in the space computation:

\begin{longtable}[]{d{2.0}d{2.5}d{2.5}d{2.2}d{0.2}}

3. Use multicolumn to specify a different format for your headers:

\multicolumn{1}{c}{\textbf{Item}} &
\multicolumn{1}{c}{\textbf{b}} &
\multicolumn{1}{c}{\textbf{t}} &
\multicolumn{1}{c}{\textbf{F}} &
\multicolumn{1}{c}{\textbf{R$^2$}} \\


The result looks like this:

-
This is the simplest solution, thank you. –  Christopher Poile Jul 26 '14 at 18:02

You could place all the stars in an \mbox{...}:

...
1 & -1.45\mbox{$^{***}$} & -7.44\mbox{$^{***}$} & 55.34 & .18  \\
11 & -.86\mbox{$^{*}$} & -2.09\mbox{$^{*}$} & 4.36 & .01  \\
12 & -1.79\mbox{$^{***}$} & -3.80\mbox{$^{***}$} & 14.47 & .05  \\
13 & -.56 & -1.34 & 1.80 & .00  \\
61 & -1.79\mbox{$^{***}$} & -5.85\mbox{$^{***}$} & 34.20 & .12  \\
62 & -1.00\mbox{$^{**}$} & -3.17\mbox{$^{**}$} & 10.05 & .04  \\
63 & -.27 & -.65 & .43 & .00  \\
64 & -.37 & -.97 & .94 & .00  \\
65 & -.34 & -.85 & .73 & .00  \\
66 & 1.05\mbox{$^{**}$} & 2.70\mbox{$^{**}$} & 7.30 & .02  \\
67 & -1.12\mbox{$^{**}$} & -2.90\mbox{$^{**}$} & 8.40 & .03  \\
72 & -.41 & -1.20 & 1.43 & .00  \\
73 & -.27 & -.82 & .67 & .00  \\
74 & 2.05\mbox{$^{***}$} & 4.99\mbox{$^{***}$} & 24.88 & .09  \\
75 & 1.61\mbox{$^{***}$} & 4.41\mbox{$^{***}$} & 19.46 & .07  \\
76 & .29 & .91 & .83 & .00  \\
...


However, the column width is maintained according to the column specification d{X.Y}. Therefore, you'll find that the stars overlap into neighbouring columns:

So, you could correct for this by modifying the header columns:

\multicolumn{1}{c}{\textbf{Item}} &
\multicolumn{1}{p{4em}}{\centering\textbf{\textit{b}}} &
\multicolumn{1}{p{4em}}{\centering\textbf{\textit{t}}} &
\multicolumn{1}{c}{\textbf{\textit{F}}} &
\multicolumn{1}{c}{\textbf{\textit{R$^2$}}} \\


This is the eventual output:

-
Thanks to your answer I even managed to make this appoach work! –  deboerk Aug 4 '11 at 22:36

I prefer not to invoke additional packages when I don't need them, so my method is to use r@{}l instead of c for the column alignments. The @{} compresses the space between the columns. Putting the figures ahead of the . in the first column and those after in the second column should produce the desired result. The headings have to span two columns, but that's standard stuff. I often use a macro for the column headers.

Here's how I'd code your example. I've used \hline instead of the various rules because those gave me errors.

\begin{tabular}{cr@{}lr@{}lr@{}lr@{}lr@{}l}
\hline
\textbf{ Item } &
\multicolumn{2}{c}{\textbf{ \textit{b} }} &
\multicolumn{2}{c}{\textbf{ \textit{t} }} &
\multicolumn{2}{c}{\textbf{ \textit{F} }} &
\multicolumn{2}{c}{\textbf{ \textit{R$^2$} }}\\
\hline

1 & -1&.45{$^{***}$} & -7&.44{$^{***}$} & 55&.34 & &.18  \\
11 &  -&.86{$^{*}$} & -2&.09{$^{*}$} & 4&.36 & &.01  \\
12 & -1&.79{$^{***}$} & -3&.80{$^{***}$} & 14&.47 & &.05  \\
13 &  -&.56 & -1&.34 & 1&.80 & &.00  \\
61 & -1&.79{$^{***}$} & -5&.85{$^{***}$} & 34&.20 & &.12  \\
62 & -1&.00{$^{**}$} & -3&.17{$^{**}$} & 10&.05 & &.04  \\
63 &  -&.27 & -&.65 & &.43 & &.00  \\
64 &  -&.37 & -&.97 & &.94 & &.00  \\
65 &  -&.34 & -&.85 & &.73 & &.00  \\
66 &  1&.05{$^{**}$} & 2&.70{$^{**}$} & 7&.30 & &.02  \\
67 & -1&.12{$^{**}$} & -2&.90{$^{**}$} & 8&.40 & &.03  \\
72 &  -&.41 & -1&.20 & 1&.43 & &.00  \\
73 &  -&.27 & -&.82 & &.67 & &.00  \\
74 &  2&.05{$^{***}$} & 4&.99{$^{***}$} & 24&.88 & &.09  \\
75 &  1&.61{$^{***}$} & 4&.41{$^{***}$} & 19&.46 & &.07  \\
76 &   &.29 & &.91 & &.83 & &.00  \\
\hline
\end{table}


Here's what my output looked like.

To give credit where it's due, I originally got this from the LaTeX Wikibook.

-

Putting the asterix in an \mbox seems to run without errors, but the results are not very good. So instead of $^{**}$ use \mbox{$^{**}$}. Adding additional column spacing should do the trick.

-