1

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}

enter image description here

1
  • Note that your red numbers are essientially out of the 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
    – daleif
    Mar 21 at 15:39

2 Answers 2

4

Your \hl{...} macro acts as a barrier which siunitx cannot look into.

Perhaps use \hlx -0.123\sym{***} instead

Where

\NewDocumentCommand\hlx{}{\color{highlightColor}\bfseries}

and \sym is modified to

\ProvideDocumentCommand\sym{ m }{%
   \normalcolor\normalfont% <- added
    \ifmmode^{#1}\else\(^{#1}\)\fi%
 }

Because siunitx can handle color and boldface just fine, it just needs to be able to see it.

Here is when what I get

enter image description here

Full adjusted code

\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 }{%
  \normalcolor\normalfont%
    \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}
\renewrobustcmd{\bfseries}{\fontseries{b}\selectfont}
\NewDocumentCommand{\hl}{m}{\textcolor{highlightColor}{\bfseries #1}}

\NewDocumentCommand\hlx{}{\color{highlightColor}\bfseries}


% 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 & \hlx 0.108 \sym{*} &
    0.023 & \hlx 0.111\sym{**} & 0.034 & \hlx 0.164 \sym{***}\\
        & (0.035) & (0.064) & (0.031) & (0.057) & (0.027) & (0.048) \\
\lightrule
    \centeredRows{Competitive sport \(\times\) Female} & & \hlx
    -0.173\sym{**} & & \hlx -0.124 \sym{*} & & \hlx -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}
5
  • I tried to modify the code and it has resolved the problem with the minus sign, the misalignment is still there unfortunately :/ Mar 22 at 12:47
  • @EmanueleNardi exactly which misalignment are you talking about?
    – daleif
    Mar 22 at 12:54
  • BTW I also changed table-format=-1.3 to the more correct table-format=1.3
    – daleif
    Mar 22 at 12:56
  • I changed my code with your suggestions and the numbers in bold are a little bit off to the left regard to the numbers below them. Would you be so kind to update your answer with the whole code, so I can check what I'm doing wrong? Mar 22 at 13:05
  • 1
    @EmanueleNardi it will always be a little of since bold i wider, even the dot is wider and siunitx is not taking the width of the decimal point into account when it centers (at least I don't think it does)
    – daleif
    Mar 22 at 13:37
0

See, if the following solution is acceptable to you:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage[T1]{fontenc}

\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}
\sisetup{
    input-open-uncertainty =,
    input-close-uncertainty=,
    table-align-text-before=false
        }

\usepackage{etoolbox}
\newrobustcmd\B{\DeclareFontSeriesDefault[rm]{bf}{b}%           %
                \bfseries\color{red}}                          %

\begin{document}
\noindent%
    \begin{talltblr}[
caption = {caption tex},
  label = {tab:statistic?},
 remark{Note} = {We report $t$-statistics in parentheses.\\
                *, **, and *** denote significance at the
                \qty{10}{\%}, \qty{5}{\%}, and \qty{1}{\%}
                levels, respectively?}
                ]{hline{1,3,Z} = 1pt,
                 colspec = {@{}  Q[font=\bfseries]
                            *{6}{X[c, m, si={detect-weight, % <--
                                          mode=text,     % <--
                                          table-format=-1.3{**}}]}
                            @{}},
                 colsep = 3pt,
                 cell{1}{2,4,6} = {c=2}{font=\bfseries},
                 row{4-Z}  = {rowsep=0pt},
                 row{1,2}= {guard},
                 row{odd[4]} = {abovesep=1ex}
                 }
    & STEM  
        &   & {STEM\\ without medicine}
                &   & {Math-intensive\\ STEM}
                        &                                   \\
    \cmidrule[lr]{2-3}
    \cmidrule[lr]{4-5}
    \cmidrule[lr]{6-7}
    & (1)   & (2)   & (3)   & (4)   & (5)   & (6)           \\
Competitive sport
    & -0.015
        &\B 0.108\TblrNote{*}
            & 0.023
                &\B 0.111\TblrNote{**}
                    & 0.034
                        &\B 0.164\TblrNote{***} \\
    & (0.035)
        & (0.064)
            & (0.031)
                & (0.057)
                    & (0.027)
                        & (0.048)               \\
Competitive sport \(\times\) Female
    &   &\B -0.173\TblrNote{**}
            &   &\B -0.124\TblrNote{*}
                    &   &\B -0.183\TblrNote{***}\\
    &   & (0.074)
            &   & (0.066)
                    &   & (0.056)               \\
Female
    & -0.069\TblrNote{**}
        & 0.076
            & -0.169\TblrNote{**}
                & -0.065
                    & -0.158\TblrNote{***}
                        & -0.004                \\
    & (0.027)
        & (0.068)
            & (0.024)
                & (0.060)
                    & (0.020)
                        & (0.051)               \\
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)               \\
    \end{talltblr}
\end{document}

enter image description here

MWE for table use talltblr of tabularray package, for coloring and make this numbers bold is used etoolbox package.

Since table is quite wide, you may consider to use in table smaller font size.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .