# 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 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.

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

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:

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 found you can also add a few interword spaces ~ to the column header to prevent stars from overlapping into neighbouring columns. In addition, the mbox is not necessary and alignment to the decimal point also work by simply entering the stars in math mode $^{***}$. – Paul Rougieux Feb 28 '17 at 17:08

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.

Had a similar need to align odds ratios with asterisks by decimal point + standard errors in parentheses. siunitx seemed to work best.

I have many such tables, therefore, I was looking for a solution where I could deal with all the asterisks in one fell swoop, not one by one.

I am a newbie and had to spend a few hours on that, so I hope this might save time for someone else:

\documentclass{article}
\usepackage{siunitx}% align by decimal, asterisks and parentheses
\sisetup{
input-symbols=(),
group-digits=false,
table-align-text-post=false,
explicit-sign
}
\begin{document}
\begin{table}[ht]
\begin{tabular}
{l                           % column with left aligned cells
*{2}{S[table-format = 1.5]}}% 2 columns, formatted with 1 digit before "."
% and 5 digits after "." space for ***.
\hline
\multicolumn{1}{c}{Variables}&
\multicolumn{1}{c}{Model 1}&
\multicolumn{1}{c}{Model 2} \\
\hline
Var$1$ & 1.45*** & 1.44 \\
& (0.01) & (0.81)  \\
Var$2$ & 111.79*** & 3.80**  \\
& (0.74) & (0.08)  \\
\hline
\multicolumn{3}{l}{ ***p$<$0.01, **p$<$0.05} \\
\end{tabular}
\end{table}
\end{document}

• Anna S, I took liberty and slightly corrected your answer, however please make your answer as small complete document which we can copy and compile in our computers. Welcome to TeX.SE! – Zarko Feb 8 '17 at 18:37

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.