為整個表格行著色

為整個表格行著色

我有一張桌子,我想突出顯示一些行。

我在用:

\usepackage[table]{xcolor}

和:

\rowcolor{gray!50}

其產生:

我想用純色塊突出顯示整行,

like this!

任何意見,將不勝感激!


微量元素:

\begin{table*}[ht]
  \centering
  \setlength{\tabcolsep}{1mm}
  \begin{tabular}{cccccc}
    \hline\hline
    \thead{Name} & \thead{Observing\\Frequency\\(MHz)} & \thead{Mean Standard Deviation of\\On-Pulse Phase Bins} & \thead{Peak Systematic\\Variability} & \thead{Average Systematic\\Variability} & \thead{Noisy\\Variability} \\
    \hline
\rowcolor{gray!50}
B1747 & 1400 & 8.83 & 281.00 & 0.95 & 8.68 \\
B1747 & 2030 & 3.61 & 6.15 & 0.09 & 3.66 \\
B1747 & 1500 & 2.37 & 5.47 & 0.19 & 2.35 \\
B1347 & 820 & 1.44 & 4.59 & 0.09 & 1.43 \\
\rowcolor{gray!50}
B1921 & 820 & 5.46 & 34.23 & 2.56 & 3.87 \\
C1921 & 1500 & 5.16 & 12.00 & 1.12 & 4.86 \\
D1921 & 1400 & 4.93 & 22.66 & 1.84 & 3.96 \\
B1921 & 2030 & 2.82 & 14.01 & 1.29 & 2.25 \\
\hline
  \end{tabular}
\caption{caption}
\label{var_table}
\end{table*}

答案1

要使用表中的列之間新增空間,\rowcolor您應該使用\setlength{\tabcolsep}{5mm}統一的空間變更。如果變更不應該一致,您可以插入僅插入空格的附加列(使用<{\hspace{5mm}})。在後一種情況下,您必須使用額外的&s 來新增空白列。

\documentclass[]{scrartcl}

\usepackage[table]{xcolor}

\begin{document}
Wrong:
\begin{tabular}[]{@{\hspace{5mm}}c@{\hspace{10mm}}c@{\hspace{5mm}}}
    \hline
    \rowcolor{gray!50}a&b\\
    \hline
\end{tabular}
\\good:
{
\setlength{\tabcolsep}{5mm}
\begin{tabular}[]{cc}
    \hline
    \rowcolor{gray!50}a&b\\
    \hline
\end{tabular}
}
\\not uniform:
{
\setlength{\tabcolsep}{0mm}
\begin{tabular}[]{c<{\hspace{7mm}}c<{\hspace{12mm}}c<{\hspace{5mm}}}
    \hline
    \rowcolor{gray!50}&a&b\\
    \hline
\end{tabular}
}
\end{document}

在第一個表中它不起作用,而第二個和第三個表給出了所需的結果:

結果

答案2

有了tabularray包包就簡單了:

\documentclass[twocolumn]{article}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}

\begin{document}
    \begin{table*}[ht]
\centering
\begin{talltblr}[
    note{a} = {Mean Standard Deviation}
            ]{
    hline{1,Z}=1pt, hline{2}=0.6pt,
    colspec = {X[l,m]   X[c,m, si={table-format=4}]
                        X[c,m, si={table-format=1.2}]
                        X[c,m, si={table-format=3.2}]
                        X[c,m, si={table-format=1.2}]
                        X[c,m, si={table-format=1.2}]
               },
    row{1} = {font=\small\bfseries},
            }
Name    & {{{Observing Frequency (MHz)}}}
                & {{{MSD\TblrNote{a} of On-Pulse Phase Bins}}}
                        & {{{Peak Systematic Variability}}}
                                    & {{{Average Systematic Variability}}}
                                            & {{{ Noisy Variability }}}     \\
\SetRow{bg=gray8}
B1747   & 1400  & 8.83  & 281.00    & 0.95  & 8.68  \\
B1747   & 2030  & 3.61  &   6.15    & 0.09  & 3.66  \\
B1747   & 1500  & 2.37  &   5.47    & 0.19  & 2.35  \\
B1347   &  820  & 1.44  &   4.59    & 0.09  & 1.43  \\
\SetRow{gray8}
B1921   &  820  & 5.46 &  34.23 & 2.56  & 3.87  \\
C1921   & 1500  & 5.16 &  12.00 & 1.12  & 4.86  \\
D1921   & 1400  & 4.93 &  22.66 & 1.84  & 3.96  \\
B1921   & 2030  & 2.82 &  14.01 & 1.29  & 2.25  \\
\end{talltblr}
\end{table*}

\end{document}

在此輸入影像描述

答案3

{NiceTabular}該套件的環境nicematrix提供(當密鑰colortbl-like有效時)類似於經典的給colortbl行、列和單元格著色的命令。

即使您使用@{\hspace{...}增加兩列之間的空間,您也會獲得預期的輸出。

此外,無論您使用什麼 PDF 檢視器,您都不會看到細白線。

\documentclass{article}
\usepackage{nicematrix}
\usepackage{makecell}

\begin{document}

\begin{table*}[ht]
  \centering
  \setlength{\tabcolsep}{1mm}
  \begin{NiceTabular}{c@{\hspace{2cm}}ccccc}[colortbl-like]
    \hline\hline 
    \RowStyle{\bfseries}
    \thead{Name} & \thead{Observing\\Frequency\\(MHz)} & \thead{Mean Standard Deviation of\\On-Pulse Phase Bins} & \thead{Peak Systematic\\Variability} & \thead{Average Systematic\\Variability} & \thead{Noisy\\Variability} \\
    \hline
\rowcolor{gray!50}
B1747 & 1400 & 8.83 & 281.00 & 0.95 & 8.68 \\
B1747 & 2030 & 3.61 & 6.15 & 0.09 & 3.66 \\
B1747 & 1500 & 2.37 & 5.47 & 0.19 & 2.35 \\
B1347 & 820 & 1.44 & 4.59 & 0.09 & 1.43 \\
\rowcolor{gray!50}
B1921 & 820 & 5.46 & 34.23 & 2.56 & 3.87 \\
C1921 & 1500 & 5.16 & 12.00 & 1.12 & 4.86 \\
D1921 & 1400 & 4.93 & 22.66 & 1.84 & 3.96 \\
B1921 & 2030 & 2.82 & 14.01 & 1.29 & 2.25 \\
\hline
  \end{NiceTabular}
\caption{caption}
\label{var_table}
\end{table*}

\end{document}

您需要多次編譯(因為nicematrix在背景使用 PGF/Tikz 節點)。

上述程式碼的輸出

相關內容