我想知道是否可以为表格中的特定行着色,尤其是在使用多列时。我正在使用www.tablesgenerator.com创建我的表,因为它们变得更大了。以下是代码片段:
\documentclass[a4paper,12pt]{article}
\usepackage[table,xcdraw]{xcolor}
\begin{document}
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{l|l|l|l|l|l|l|}
\cline{2-7}
& \multicolumn{6}{l|}{\cellcolor[HTML]{6195C9}Something} \\ \hline
\rowcolor[HTML]{275D90}
\multicolumn{1}{|l|}{\cellcolor[HTML]{275D90}Something} & \multicolumn{6}{l|}{\cellcolor[HTML]{275D90}} \\ \hline
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}1} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & & & & \\ \hline
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}2} & & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & & \\ \hline
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}3} & & & & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} \\ \hline
\end{tabular}
\end{table}
\end{document}
我想修改代码,使白色、黑色和灰色单元格的线条为灰色。但蓝色单元格的边框或右侧的线条应该是黑色。我在\arrayrulecolor{gray}
多列之前尝试过,但这也会影响蓝色单元格边框的颜色。有没有简单的解决方案可以解决这个问题?
答案1
这是一个使用hhline
和定义新列类型来替换|
列之间的分隔符的解决方案,深受boldline
包(来自shipunov
包)的启发。我不太确定一些垂直线是否也必须是灰色的,但这可以改变。此外,我加载了包caption
,以便在标题和表格之间有更好的垂直间距。由于请求非常模糊,我给出了两种可能性:
\documentclass{article}
\usepackage[table, x11names]{xcolor}
\usepackage{array}
\usepackage{caption}
\usepackage{hhline}
\makeatletter
\newcolumntype{W}{!{\color{Snow3}\vrule\@width\arrayrulewidth}} %[1]#1
\makeatother
\newcommand\normalcell[1]{\multicolumn{1}{c|}{#1}}
\begin{document}
\begin{table}[]
\centering \caption{My caption} \label{my-label}
\begin{tabular}{l|*{5}{lW}l! {\vline}}%
\hhline{~|------}
& \multicolumn{6}{l|}{\cellcolor[HTML]{6195C9}Something} \\
\hline
\rowcolor[HTML]{275D90} \multicolumn{1}{|l|}{\cellcolor[HTML]{275D90}Something} & \multicolumn{6}{l|}{\cellcolor[HTML]{275D90}} \\
\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{lightgray}}-->{\arrayrulecolor{black}}----}
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}1} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & \normalcell{} & \normalcell{} & \normalcell{} & \\
\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{lightgray}}---->{\arrayrulecolor{black}}--}
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}2} & & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & \normalcell{} & \\
\hhline{>{\arrayrulecolor{black}}-->{\arrayrulecolor{lightgray}}----->{\arrayrulecolor{black}}|}
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}3} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} \\
\hline
\end{tabular}
\end{table}
\begin{table}[]
\centering \caption{My caption} \label{my-label}
\setlength\arrayrulewidth{0.6pt}
\begin{tabular}{l|*{5}{lW}l|}%
\hhline{~|------}
& \multicolumn{6}{l|}{\cellcolor[HTML]{6195C9}Something} \\
\hline
\rowcolor[HTML]{275D90} \multicolumn{1}{|l|}{\cellcolor[HTML]{275D90}Something} & \multicolumn{6}{l|}{\cellcolor[HTML]{275D90}} \\
\hline
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}1} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & & & &\normalcell{} \\
\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{Snow3}}------>{\arrayrulecolor{black}}|}
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}2} & & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & & \\
\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{Snow3}}------>{\arrayrulecolor{black}}|}
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}3} & & & & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} \\
\hline
\end{tabular}
\end{table}
\end{document}
答案2
像这样?
\documentclass{article}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{l|l|l|l|l|l|l|}
\cline{2-7}
& \multicolumn{6}{l|}{\cellcolor[HTML]{6195C9}Something} \\\arrayrulecolor{red}\hline
\rowcolor[HTML]{275D90}
\multicolumn{1}{|l|}{\cellcolor[HTML]{275D90}Something} & \multicolumn{6}{l|}{\cellcolor[HTML]{275D90}} \\ \hline
\arrayrulecolor{blue}
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}1} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & & & & \\ \hline
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}2} & & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} & & \\ \hline
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}3} & & & & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{000000} & \cellcolor[HTML]{000000} \\ \hline
\end{tabular}
\end{table}
\end{document}
我在适当的地方使用\arrayrulecolor{<colour>}
。
答案3
这是一个{NiceTabular}
使用 的解决方案nicematrix
。
在 中{NiceTabular}
,您可以使用命令 水平和垂直合并单元格\Block
。然后,键hvlines
将绘制除键指定的块和角落之外的所有规则corners
(角落是从空单元格自动计算的)。
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\begin{NiceTabular}{cccccc}[hvlines,corners=NW] % NW = north west
\CodeBefore
\cellcolor{black}{3-2,3-3,4-4,4-5,5-5,5-6}
\cellcolor[HTML]{C0C0C0}{4-3,5-4}
\rowcolor[HTML]{6195C9}{1}
\cellcolor[HTML]{6195C9}{3-1,4-1,5-1}
\rowcolor[HTML]{275D90}{2}
\Body
& \Block{1-*}{Something} \\
Something & \Block{1-*}{} \\
1 & \Block{*-*}{} & & & & \\
2 & \\
3 & \\
\end{NiceTabular}
\end{document}
该包nicematrix
在单元格、行和列下创建 PGF/Tikz 节点。现在可以将这些节点与 Tikz 一起使用来绘制灰度规则。
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\begin{NiceTabular}{cccccc}[hvlines,corners=NW]
\CodeBefore
\cellcolor{black}{3-2,3-3,4-4,4-5,5-5,5-6}
\cellcolor[HTML]{C0C0C0}{4-3,5-4}
\rowcolor[HTML]{6195C9}{1}
\cellcolor[HTML]{6195C9}{3-1,4-1,5-1}
\rowcolor[HTML]{275D90}{2}
\Body
& \Block{1-*}{Something} \\
Something & \Block{1-*}{} \\
1 & \Block{*-*}{} & & & & \\
2 & \\
3 & \\
\CodeAfter
\begin{tikzpicture} [gray!20,line width = 0.5pt]
\foreach \i in {4,5} { \draw (\i-|2) -- (\i-|7) ; }
\foreach \j in {3,4,5,6} { \draw (3-|\j) -- (6-|\j) ; }
\end{tikzpicture}
\end{NiceTabular}
\end{document}
输出结果并不完美,因为灰色规则是在黑色规则之后绘制的。可以再绘制一个黑色矩形,以获得完美的输出结果。
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\begin{NiceTabular}{cccccc}[hvlines,corners=NW]
\CodeBefore
\cellcolor{black}{3-2,3-3,4-4,4-5,5-5,5-6}
\cellcolor[HTML]{C0C0C0}{4-3,5-4}
\rowcolor[HTML]{6195C9}{1}
\cellcolor[HTML]{6195C9}{3-1,4-1,5-1}
\rowcolor[HTML]{275D90}{2}
\Body
& \Block{1-*}{Something} \\
Something & \Block{1-*}{} \\
1 & \Block{*-*}{} & & & & \\
2 & \\
3 & \\
\CodeAfter
\begin{tikzpicture} [gray!20,line width = 0.5pt]
\foreach \i in {4,5} { \draw (\i-|2) -- (\i-|7) ; }
\foreach \j in {3,4,5,6} { \draw (3-|\j) -- (6-|\j) ; }
\end{tikzpicture}
\tikz \draw (3-|2) rectangle (last-|last) ;
\end{NiceTabular}
\end{document}
由于 PGF/Tikz 节点,您需要进行多次编译。
答案4
\documentclass[11pt]{article}
\usepackage[table]{xcolor}
\usepackage{hhline}
\begin{document}
%\setlength\arrayrulewidth{1pt} % No need for this in your final version
\newcommand{\VLINE}{\multicolumn{1}{l !{\color{gray}\vline}}}
\begin{table}
\centering
\caption{My caption}
\label{my-label}
\medskip
\begin{tabular}{l|l|l|l|l|l|l|}
\hhline{~|------}
& \multicolumn{6}{l|}{\cellcolor[HTML]{6195C9}Something} \\ \hline
\rowcolor[HTML]{275D90}
\multicolumn{1}{|l|}{\cellcolor[HTML]{275D90}Something} & \multicolumn{6}{l|}{\cellcolor[HTML]{275D90}} \\ \hline
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}1} & \multicolumn{1}{l !{\color{gray}\vline}}{\cellcolor[HTML]{000000}} &\VLINE{\cellcolor[HTML]{000000}} & \VLINE{} &\VLINE{}&\VLINE{}&\\
\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{gray}}------}%\hline
\arrayrulecolor{black}
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}2} &\VLINE{}&\VLINE{\cellcolor[HTML]{C0C0C0}}&\VLINE{\cellcolor[HTML]{000000}}&\VLINE{\cellcolor[HTML]{000000}}&\VLINE{}& \\
\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{gray}}------}%\hline
\arrayrulecolor{black}
\multicolumn{1}{|l|}{\cellcolor[HTML]{6195C9}3}&\VLINE{}&\VLINE{}&\VLINE{}&\VLINE{\cellcolor[HTML]{C0C0C0}}&\VLINE{\cellcolor[HTML]{000000}}& \cellcolor[HTML]{000000} \\ \hline
\end{tabular}
\end{table}
\end{document}
嗯,这太复杂了,但使用hhline
包,可以为单个单元格定义自定义规则。对于灰色垂直规则,我们可以定义
\newcommand{\VLINE}{\multicolumn{1}{l !{\color{gray}\vline}}}
对于水平段,我们可以通过以下方式设置不同的段颜色:
\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{gray}}------}
\arrayrulecolor{black}
其中虚线的数量代表单元格的数量,为了恢复我们\arrayrulecolor{black}
在末尾添加的黑色规则。