# Aligning entries of a table at dots if entry is a number, otherwise centered

I have the following table, where the entries are either numbers or a dash, possibly followed by one or more asterixes:

\documentclass[notitlepage]{article}

\usepackage{tabularx}
\usepackage{booktabs}

\begin{document}

\begin{table}
\centering

\begin{tabular}{lrrrrrr}
\toprule
& \multicolumn{2}{c}{(1)} & \multicolumn{2}{c}{(2)} & \multicolumn{1}{c}{(3)} & \multicolumn{1}{c}{(4)}\\
& Description &  & Description &  & Description & Description \\
\midrule
VarA &    1.123$^{**}$  & (1.123)  & 9.765$^{***}$ & (-22.123) & -3.321 & -4.123 \\
VarB &  -32.123$^{*}$   & (12.321) & 4.654         & (3.321)   &  6.321 &  4.123 \\
VarC &   &  & --$^{**}$ & (12.898) & -- & -- \\
\end{tabular}
\end{table}

\end{document}


I'd like to align the entries such that all numbers are aligned wrt. the dots. As the dashes don't have any dots to align after, they should just be center-aligned wrt. the column. Is that possible to do?

Using an adapted version of this answer you can achieve the following alignment with the help of the siunitx package:

\documentclass[notitlepage]{article}

\usepackage{tabularx}
\usepackage{booktabs}

\usepackage{siunitx}
\sisetup{
detect-all,
table-align-text-pre     = false,
table-align-text-post    = false,
input-signs              = + -,
input-symbols            = {*} {**} {***},
input-open-uncertainty   = ,
input-close-uncertainty  = ,
}
\begin{document}

\begin{table}
\centering
\begin{tabular}{l
S[table-format=-2.3,table-space-text-post = {$^{**}$}]
S[table-format=2.3,table-space-text-pre={*}, table-space-text-post={-*}]
S[table-format=-1.3,table-space-text-post = {$^{***}$}]
S[table-format=-2.3,,table-space-text-pre={*}, table-space-text-post={-*}]
S[table-format=-1.3]
S[table-format=-1.3]
}
\toprule
& \multicolumn{2}{c}{(1)} & \multicolumn{2}{c}{(2)} & \multicolumn{1}{c}{(3)} & \multicolumn{1}{c}{(4)}\\
& {Description} &  & {Description} &  & {Description} & {Description} \\
\midrule
VarA &    1.123$^{**}$  & (1.123)  & 9.765$^{***}$ & (-22.123) & -3.321 & -4.123 \\
VarB &  -32.123$^{*}$   & (12.321) & 4.654         & (3.321)   &  6.321 &  4.123 \\
VarC &   &  & {--$^{**}$} & (12.898) & {--} & {--} \\
\end{tabular}
\end{table}

\end{document}


Please note that the above shown table is wider than the textwidth of article.

Regarding the requested alignment of asterisks: Here is a suggestion using two columns (one for right aligned for the number and one left aligned of the asterisks) and the makecell package (to align the -- with respect to the center of the numbers).

\documentclass[notitlepage]{article}

\usepackage{tabularx}
\usepackage{booktabs}

\usepackage{makecell}
\usepackage{siunitx}
\sisetup{
detect-all,
table-align-text-pre     = false,
table-align-text-post    = false,
input-signs              = + -,
input-symbols            = {*} {**} {***},
input-open-uncertainty   = ,
input-close-uncertainty  = ,
}
\begin{document}

\begin{table}
\centering
\begin{tabular}{l
S[table-format=-2.3,table-space-text-post = {$^{**}$}]
S[table-format=2.3,table-space-text-pre={*}, table-space-text-post={-*}]
S[table-format=-1.3,table-alignment=right]@{}
@{}l
S[table-format=-2.3,,table-space-text-pre={*}, table-space-text-post={-*}]
S[table-format=-1.3]
S[table-format=-1.3]
}
\toprule
& \multicolumn{2}{c}{(1)} & \multicolumn{3}{c}{(2)} & \multicolumn{1}{c}{(3)} & \multicolumn{1}{c}{(4)}\\
& {Description} & &  \multicolumn{2}{c}{Description}  & & {Description}  & {Description} \\
\midrule
VarA &    1.123$^{**}$  & (1.123)  & 9.765 & $^{***}$ & (-22.123) & -3.321 & -4.123 \\
VarB &  -32.123$^{*}$   & (12.321) & 4.654 &          & (3.321)   &  6.321 &  4.123 \\
VarC &                  &          & {\makecell[cc]{--}}  & $^{**}$  & (12.898)  & {--}   & {--} \\
\end{tabular}
\end{table}

\end{document}

• Thanks, that is a good example. I know I asked for center-alignment of the dashes, but is it possible to align the left-most asterix of the dashes with the left-most asterix of the number? – N08 Feb 17 at 10:52
• @N08: If -- would be a number, switching table-align-text-post from false to true would give you the desired alignment, but since {--} is text, you might be better off using a separate column for the asteriks. For an MWE applying this suggestion, please see my updated answer. – leandriis Feb 17 at 11:52
• Thanks, this is what I want. But is it possible to generalize the example such that dashes with asterixes can be written in all columns and be aligned with numeric entries? Right now it is only the 4th column that supports this (?) – N08 Feb 18 at 10:01
• @N08: You can of course use the same approach for all columns if you replace the column specifier of the respective column(s) by S[table-format=-x.y.,table-alignment=right]@{} @{}l with x.y being the corresponding value for the column. You will also have to add some & and add/adjust some multicolumn commands in order to have the entries in the right columns. – leandriis Feb 18 at 10:14