I'm trying to align decimal numbers inside a table using S columns from siunitx
package, furthermore I wanted to highlight them using a bold font. So far, so good.
I also need to add some asterisks to insert a legend below the table, but this causes a misalignment. How can I solve this problem?
\documentclass[a4paper, margin=5mm]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage{booktabs, multirow}
\usepackage{siunitx}
\sisetup{detect-weight=true}
\usepackage{xparse}
\ProvideDocumentCommand{\centeredRows}{m}{%
\multirow{2}{*}{#1}%
}
\ProvideDocumentCommand\sym{ m }{%
\ifmmode^{#1}\else\(^{#1}\)\fi%
}
\setlength{\lightrulewidth}{0.1pt}
\providecommand{\lightrule}{%
\arrayrulecolor{black!30}%
\midrule[\lightrulewidth]%
\arrayrulecolor{black}}
\usepackage{graphicx}
% NOTE command to highligths numbers
\definecolor{highlightColor}{RGB}{214, 31, 31}
% https://tex.stackexchange.com/a/352028/127473
\renewrobustcmd{\bfseries}{\fontseries{b}\selectfont}
\NewDocumentCommand{\hl}{m}{\textcolor{highlightColor}{\bfseries #1}}
% https://tex.stackexchange.com/questions/540135/use-siunitx-to-report-percentages-in-parentheses-behind-values
% https://tex.stackexchange.com/questions/327667/misalignment-in-a-siunitx-column-with-asterisks-and-different-integer-parts
\begin{document}
\begin{tabular}{@{} >{\bfseries}l *{6}{
S[table-format=1.3,
table-number-alignment = center,
table-space-text-pre = {$^{***}$},
table-space-text-post = {$^{***}$},
input-symbols={()},%
]
} @{}}
\toprule
& \multicolumn{2}{c}{\textbf{{STEM}}}
& \multicolumn{2}{c}{\textbf{{STEM without medicine}}}
& \multicolumn{2}{c}{\textbf{{Math-intensive STEM}}}\\
\cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}
& {(1)} & {(2)} & {(3)} & {(4)} & {(5)} & {(6)} \\
\midrule
\centeredRows{Competitive sport} & -0.015 & \hl{0.108}\sym{*} & 0.023 & \hl{0.111}\sym{**} & 0.034 & \hl{0.164}\sym{***}\\
& (0.035) & (0.064) & (0.031) & (0.057) & (0.027) & (0.048) \\
\lightrule
\centeredRows{Competitive sport \(\times\) Female} & & \hl{-0.173}\sym{**} & & \hl{-0.124}\sym{*} & & \hl{-0.183}\sym{***}\\
& & (0.074) & & (0.066) & & (0.056) \\
\lightrule
\centeredRows{Female} & -0.069\sym{**} & 0.076 & -0.169\sym{***} & -0.065 & -0.158\sym{***} & -0.004 \\
& (0.027) & (0.068) & (0.024) & (0.060) & (0.020) & (0.051) \\
\lightrule
\centeredRows{Sport} & -0.004 & -0.010 & -0.007 & -0.011 & -0.008 & -0.014 \\
& (0.028) & (0.028) & (0.025) & (0.025) & (0.021) & (0.021) \\
\bottomrule
\end{tabular}%
\end{document}
siunitx
processing (you can see that on the hyphen, not minus), might not be a good idea to use\hl{...}
but rather a macro that sets color and bold and the unboldify the stars