使用 siunitx 突出顯示表格列?

使用 siunitx 突出顯示表格列?

我正在使用 siunitx 按小數點對齊我的列。我想突出顯示表格中的一列(編輯:使一列具有彩色背景)。我想知道 siunitx 是否有這個功能。到目前為止,我只能像這樣改變字體顏色:

\documentclass[]{article}

\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{color}

\begin{document}

\begin{tabular}{S[table-format=3.2]S[table-
format=3.2]S[table-format=3.2,color=red]}
    \toprule
    1 &       2 &       3  \\
    1.0 &     2.0 &     3.0 \\
    \bottomrule
\end{tabular}
\end{document}

產生這樣的結果:

帶有紅色文字的表。

如果有人知道一種柔和的綠色也能以灰度列印,那就太好了。

答案1

你的解決方案不起作用。簡單的解決方案是:

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage[table]{xcolor}

\begin{document}

\begin{tabular}{S[table-format=3.2]
                S[table-format=3.2]
  >{\color{red}}S[table-format=3.2]}% correct way to prescribe font color
    \toprule
    1 &       2 &       3  \\
    1.0 &     2.0 &     3.0 \\
    \bottomrule
\end{tabular}
\end{document}

在此輸入影像描述

更高級的解決方案,您可以在其中選擇哪些單元格的內容為紅色:

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{etoolbox}           % <--
\newrobustcmd{\R}{\color{red}}  % <--

\begin{document}
\begin{tabular}{S[table-format=3.2]
                S[table-format=3.2]
                S[detect-weight,% <--
                  table-format=3.2]}
    \toprule
    1   &   2   &     3  \\
    1.0 &   2.0 & \R  3.0 \\
    \bottomrule
\end{tabular}
\end{document}

在此輸入影像描述

編輯: 從您的評論來看,您實際上喜歡使用某種“軟綠色”顏色的彩色列背景。對於顏色,請查看xcolor套件的文檔,其中詳細描述了預定義顏色的名稱或如何定義自己的顏色。簡而言之,顏色你應該自己選擇,最好是使用簡單的灰色,就像我在下面的 mwe 中使用的那樣。

筆記:使用彩色列不能很好地遵守booktabs!

在此輸入影像描述

要讓列顏色達到規則,您有兩種可能性:(i) 將規則變更為\hline,或 (ii) 重新定義booktabs規則,刪除其周圍新增的垂直空間:

\setlenght\aboverulesep{0pt}
\setlength\belowrulesep{0pt}

(下面的mwe中不考慮)

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage[table]{xcolor}% <--- changed

\begin{document}

\begin{tabular}{S[table-format=3.2]
                S[table-format=3.2]
>{\columncolor{gray!20}}S[table-format=3.2]}
    \toprule
    1 &       2 &       3  \\
    1.0 &     2.0 &     3.0 \\
    \bottomrule
\end{tabular}
\end{document}

相關內容