如何在表格中製作彩色標題?

如何在表格中製作彩色標題?

我有下表

\newcommand{\headrow}{\rowcolor{black!20}}
\definecolor{Gray}{gray}{0.90}
\begin{table}[ht]\rowcolors{1}{Gray}{white}
\begin{center}
\caption{Test Table}
 \begin{tabular}{l|cccccccc}
\hline
\headrow
\multirow{2}{1.7cm}{\textbf{Approach}}& \multicolumn{8}{c}{\centering \textbf{Test}}\\
\cline{2-9} 
& \multicolumn{1}{c}{\textbf{A}} & \multicolumn{1}{c}{\textbf{B}}& \multicolumn{1}{c}{\textbf{C}} & \multicolumn{1}{c}{\textbf{D}}& \multicolumn{1}{c}{\textbf{E}} & \multicolumn{1}{c}{\textbf{F}}
& \multicolumn{1}{c}{\textbf{G}}& \multicolumn{1}{c}{\textbf{H}}\\
    \hline

   App1 & &&  && &&&
    \\ 
  App2 && &&& &&&
    \\ 
   \hline
    \end{tabular}
   
 \end{center}
\end{table}

輸出如下

在此輸入影像描述

正如您所注意到的,標題行有兩種灰色,一種形式為 \headrow,一種來自我對灰色的定義。

那麼,我如何才能使標題的顏色與 \headrow 中的標題顏色相同並使“Approch”一詞可見?

請注意,當使用nicematrix時,我收到以下錯誤 在此輸入影像描述

答案1

使用,您將在 PDF 檢視器中獲得近乎完美的結果(例如,您將不會看到使用 MuPDF(如 SumatraPDF)的檢視器中看到的細白線){NiceTabular}nicematrix

但是,您需要進行多次編譯。

\documentclass{article}
\usepackage{nicematrix}

\begin{document}
\definecolor{Gray}{gray}{0.90}
\begin{table}[ht]
\caption{Test Table}
\begin{NiceTabular}{l|cccccccc}%
  [code-before = \rowcolors{3}{}{Gray} \rowcolor{black!20}{1,2}]
\hline
\Block{2-1}{\textbf{Approach}} & \multicolumn{8}{c}{\textbf{Test}}\\
\cline{2-9} 
& \textbf{A} & \textbf{B} & \textbf{C} & \textbf{D} & \textbf{E} & \textbf{F}
& \textbf{G} & \textbf{H} \\
\hline
App1 \\
App2 \\
\hline
\end{NiceTabular}
\end{table}
\end{document}

上述程式碼的輸出

答案2

一個簡單的解決方案包括\multirow{-2}在以下行的開頭添加並 \cellcolor{black!20}在 \multiro 內添加。無關:我添加了該caption包以在標題和表格之間留出一定的間距。我還刪除了\multicolumn{1}{c}{…}標題行中所有不必要的內容。最後備註:不要center在表格內使用:它會在表格環境的正常間距中新增不必要的垂直間距。環境。

\documentclass{article}
\usepackage{multirow, caption}
\usepackage[table]{xcolor}
\newcommand{\headrow}{\rowcolor{black!20}}

\begin{document}

\definecolor{Gray}{gray}{0.90}
\begin{table}[ht]\rowcolors{3}{}{Gray}
\setlength{\extrarowheight}{2pt}
\centering
\caption{Test Table}
 \begin{tabular}{l|*{8}{c}}
\hline
\headrow & \multicolumn{8}{c}{\centering \textbf{Test}}\\
\cline{2-9}
\headrow \multirow{-2}{1.7cm}{\textbf{Approach}} & \textbf{A} & \textbf{B}& \textbf{C} & \textbf{D}& \textbf{E} & \textbf{F}
& \textbf{G}& \textbf{H}\\
    \hline

   App1 & && && &&&
    \\
  App2 && &&& &&&
    \\
   \hline
    \end{tabular}
\end{table}
\end{document} 

在此輸入影像描述

相關內容