[Tex/LaTex] Align numbers with numbers and asterisks in table by decimal point

dcolumnhorizontal alignmenttables

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

table

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
\endfirsthead
\toprule
\textbf{ Item } & \textbf{ \textit{b} } & \textbf{ \textit{t} } & \textbf{ \textit{F} } & \textbf{ \textit{R$^2$} }\\
\midrule
\endhead
\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 formatted the numbers .00 -> 0.00 and the column header was not centered properly). I guess this package is a little overpowered for my use case.

Best Answer

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
\endfirsthead
\toprule
{\textbf{Item}} & {\textbf{\textit{b}}} & {\textbf{\textit{t}}} & {\textbf{\textit{F}}} & {\textbf{\textit{R$^2$}}}\\
\midrule
\endhead
\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.