siunitx 中的粗体字体和附加符号未对齐

siunitx 中的粗体字体和附加符号未对齐

我正在尝试使用包中的 S 列对齐表格中的十进制数字siunitx,此外,我还想使用粗体字体突出显示它们。到目前为止一切顺利。

我还需要添加一些星号以在表格下方插入图例,但这会导致错位。我该如何解决这个问题?

\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}

在此处输入图片描述

答案1

你的宏充当了一道无法看透的\hl{...}屏障。siunitx

也许可以\hlx -0.123\sym{***}改用

在哪里

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

\sym修改为

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

因为siunitx可以很好地处理颜色和粗体,所以它只需要能够看到它。

这是我得到的结果

在此处输入图片描述

完整调整后的代码

\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}

答案2

看看以下解决方案是否可以接受:

\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}

在此处输入图片描述

MWE 用于表格使用talltblrtabularray包,用于着色并使这些数字变得粗体使用的etoolbox包。

由于表格很宽,您可以考虑在表格中使用较小的字体大小。

相关内容