我正在创建一个彩色表格,并尝试使用多行单元格。然而,这似乎会弄乱背景颜色,如下图所示。如何使用多行单元格解决这个问题?
代码:
\begin{table}[H]
\centering
\renewcommand{\arraystretch}{1.5}
\rowcolors{2}{MidnightBlue!10}{white}
\begingroup\setlength{\fboxsep}{0pt}
\colorbox{MidnightBlue!30}{%
\begin{tabular}{|c|c|c|c|c|}
\hline
\rowcolor{MidnightBlue!30} & \multicolumn{2}{c|}{\textbf{test1}} & \multicolumn{2}{l|}{\textbf{test2}} \\
\hhline{|~|-|-|-|-|}
\rowcolor{MidnightBlue!30} & & & & \\
\rowcolor{MidnightBlue!30} \multirow{-3}{*}{test3} & \multirow{-2}{*}{\shortstack{test1\\test}} & \multirow{-2}{*}{\shortstack{test2\\test}} & \multirow{-2}{*}{\shortstack{test1\\test}} & \multirow{-2}{*}{\shortstack{test2\\test}} \\
\hline
\rowcolor{white}& & & & \\
\multirowcell{-2}{test \\ test} & \multirowcell{-2}{test} & \multirowcell{-2}{test} & \multirowcell{-2}{test} & \multirowcell{-2}{test} \\
test & test & test & test & test \\
\hhline{|-|-|-|-|-|}
\end{tabular}%
}\endgroup
\caption{\textbf{test}}
\end{table}
答案1
有了{NiceTabular}
,nicematrix
你就不会遇到那种问题。
\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{float}
\usepackage{nicematrix}
\begin{document}
\begin{table}[H]
\centering
\renewcommand{\arraystretch}{1.5}
\begin{NiceTabular}{ccccc}[vlines]
\CodeBefore
\rowcolor{MidnightBlue!30}{1,2,3}
\rowcolors{4}{MidnightBlue!10}{}[respect-blocks]
\Body
\hline
\Block{3-1}{text3} & \Block{1-2}{\textbf{test1}} && \Block{1-2}{\textbf{test2}} \\
\Hline
& \Block{2-1}{test1\\test} & \Block{2-1}{test2\\test} & \Block{2-1}{test1\\test} & \Block{2-1}{test2\\test} \\ \\
\hline
\Block{2-1}{test\\ test} & test & test & test & test \\ \\
test & test & test & test & test \\
\hline
\end{NiceTabular}
\end{table}
\end{document}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。
答案2
我建议不要使用和\colorbox
,而使用在第一列具有背景颜色的:\rowcolor{white}
\hhline
\documentclass{article}
\usepackage{caption, float}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{multirow, hhline}
\usepackage{makecell}
\begin{document}
\begin{table}[H]
\centering
\renewcommand{\arraystretch}{1.5}
\rowcolors{2}{MidnightBlue!10}{white}
\begin{tabular}{|c|c|c|c|c|}
\hline
\rowcolor{MidnightBlue!30} & \multicolumn{2}{c|}{\textbf{test1}} & \multicolumn{2}{l|}{\textbf{test2}} \\[-0.4pt]
\hhline{|>{\arrayrulecolor{MidnightBlue!30}}->{\arrayrulecolor{black}}|-|-|-|-|}
\rowcolor{MidnightBlue!30} & & & & \\[-0.4pt]
\rowcolor{MidnightBlue!30} \multirow{-3}{*}{test3} & \multirow{-2}{*}{\shortstack{test1\\test}} & \multirow{-2}{*}{\shortstack{test2\\test}} & \multirow{-2}{*}{\shortstack{test1\\test}} & \multirow{-2}{*}{\shortstack{test2\\test}} \\
\hline
\rowcolor{white}& & & & \\
\multirowcell{-2}{test \\ test} & \multirowcell{-2}{test} & \multirowcell{-2}{test} & \multirowcell{-2}{test} & \multirowcell{-2}{test} \\
test & test & test & test & test \\
\hhline{|-|-|-|-|-|}
\end{tabular}%
\caption{\textbf{test}}
\end{table}
\end{document}