多行颜色

多行颜色

我遇到了与帖子中类似的问题xcolor 包;颜色多行下表。包含单词“HHeader”的四行单元格应该完全灰色。我尝试实施上述帖子中报告的解决方案,但它对我不起作用。我该如何解决这个问题?

\documentclass[a4paper]{report}

\usepackage{multirow}
\usepackage[table]{xcolor}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\usepackage{hhline}

\begin{document}

    \begin{tabular}{ ll | C{2.1cm} | C{2.1cm} | >{\centering}m{2.1cm} | C{2.1cm} | }
        \hhline{~~|----|}
        \multicolumn{2}{l|}{ } & \multicolumn{4}{|c|}{\cellcolor{lightgray}\textbf{VHeader}} \\ \hhline{~~|----|}
        \multicolumn{2}{l|}{ } & \cellcolor{lightgray}\textbf{h1} & \cellcolor{lightgray}\textbf{h2} & \cellcolor{lightgray}\textbf{h3} & \cellcolor{lightgray}\textbf{h4} \\ \hhline{|------|}
        \multicolumn{1}{|l|}{ } & \cellcolor{lightgray}\textbf{h5} & & & & \\ \hhline{|~|-----|} %\cline{2-6}
        \multicolumn{1}{|l|}{ } & \cellcolor{lightgray}\textbf{h6} & & & & \cellcolor{red!40} \\ \hhline{|~|-----|}
        \multicolumn{1}{|l|}{ } & \cellcolor{lightgray}\textbf{h7} & & & \cellcolor{red!40} & \cellcolor{red!40} \\ \hhline{|~|-----|}
        \multicolumn{1}{|l|}{\multirow{-4}[0]{*}{\cellcolor{lightgray}\textbf{HHeader}}} & \cellcolor{lightgray}\textbf{h8} & & \cellcolor{red!40} & \cellcolor{red!40} & \cellcolor{red!40} \\ \hline
    \end{tabular}

\end{document}

在此处输入图片描述

答案1

您必须为每个混合单元格添加颜色,因为不幸的是,\columncolor它对多行单元格不太有用。您还需要更改\cline而不是以\hhline避免多行单元格内的规则。

\documentclass[a4paper]{report}

\usepackage{multirow}
\usepackage[table]{xcolor}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\usepackage{hhline}

\begin{document}

    \begin{tabular}{ll | C{2.1cm} | C{2.1cm} | >{\centering}m{2.1cm} | C{2.1cm} | }
        \hhline{~~|----|}
        \multicolumn{2}{l|}{} & \multicolumn{4}{|c|}{\cellcolor{lightgray}\textbf{VHeader}} \\ \hhline{~~|----|}
        \multicolumn{2}{l|}{ } & \cellcolor{lightgray}\textbf{h1} & \cellcolor{lightgray}\textbf{h2} & \cellcolor{lightgray}\textbf{h3} & \cellcolor{lightgray}\textbf{h4} \\ \hhline{|------|}
        \multicolumn{1}{|l|}{\cellcolor{red} } & \cellcolor{lightgray}\textbf{h5} & & & & \\ \cline{2-6} 
        \multicolumn{1}{|l|}{\cellcolor{red} } & \cellcolor{lightgray}\textbf{h6} & & & & \cellcolor{red!40} \\ \cline{2-6} 
        \multicolumn{1}{|l|}{\cellcolor{red}} & \cellcolor{lightgray}\textbf{h7} & & & \cellcolor{red!40} & \cellcolor{red!40} \\ \cline{2-6}   \multicolumn{1}{|l|}{\multirow{-4}[0]{*}{\cellcolor{red}\textbf{HHeader}}} & \cellcolor{lightgray}\textbf{h8} & & \cellcolor{red!40} & \cellcolor{red!40} & \cellcolor{red!40} \\ \hline
    \end{tabular}

\end{document}

答案2

结合columncolor\cellcolor

在此处输入图片描述

\documentclass[a4paper]{report}

\usepackage{multirow}
\usepackage[table]{xcolor}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\usepackage{hhline}
\NewExpandableDocumentCommand\mcc{O{1}m}{\multicolumn{#1}{c|}{#2}}

\begin{document}
    \begin{tabular}{|*{2}{>{\columncolor{lightgray}}l|} *{4}{C{2.1cm}|} }
        \hhline{~~|----|}
\mcc[2]{}   & \mcc[4]{\cellcolor{lightgray}\textbf{VHeader}}    \\ 
        \hhline{~~|----|}
\mcc[2]{}   & \cellcolor{lightgray}{\textbf{h1}} 
                & \cellcolor{lightgray}{\textbf{h2}} 
                    & \cellcolor{lightgray}{\textbf{h3}} 
                        & \cellcolor{lightgray}{\textbf{h4}}    \\
    \hhline{|------|}
    & \textbf{h5} & & & &                       \\ 
\hhline{|>{\arrayrulecolor{lightgray}}->{\arrayrulecolor{black}}|-----|} 
    & \textbf{h6} & & & & \cellcolor{red!40}    \\
\hhline{|>{\arrayrulecolor{lightgray}}->{\arrayrulecolor{black}}|-----|} 
    & \textbf{h7}
        &   &   & \cellcolor{red!40}
                    & \cellcolor{red!40}        \\
\hhline{|>{\arrayrulecolor{lightgray}}->{\arrayrulecolor{black}}|-----|} \multirow{-4}[0]{*}{\cellcolor{lightgray}{\textbf{HHeader}}}
    &   \textbf{h8} 
        &   &   \cellcolor{red!40} 
                &   \cellcolor{red!40}
                    &   \cellcolor{red!40}      \\
    \hhline{|------|}
    \end{tabular}
\end{document}

答案3

{NiceTabular}您可以使用轻松创建该表nicematrix

\documentclass{article}
\usepackage{nicematrix}

\begin{document}
\begin{NiceTabular}{cc*{4}{wc{15mm}}}[hvlines,corners=NW] % NW : north-west
\CodeBefore
  \rowcolor{gray!10}{1,2}
  \columncolor{gray!10}{1,2}
  \cellcolor{red!15}{4-6,5-5,5-6,6-4,6-5,6-6}
\Body
                     &    & \Block{1-4}{VHeader} \\
                     &    & h1 & h2 & h3 & h4 \\
\Block{4-1}{HHeader} & h5 \\
                     & h6 \\
                     & h7 \\
                     & h8 \\
\end{NiceTabular}
\end{document}

使用键corners=NW,西北的空角由 计算nicematrix,并且规则和颜色指令不适用于该角落的单元格。

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

相关内容