![使用颜色的 Buggy 表格](https://linux22.com/image/463111/%E4%BD%BF%E7%94%A8%E9%A2%9C%E8%89%B2%E7%9A%84%20Buggy%20%E8%A1%A8%E6%A0%BC.png)
这个表格有某种缺陷,有一个白色单元格,我想用渐变背景填充每个主列的整个第一行;有人有办法让它更干净吗?
\documentclass{article}
\usepackage{colortbl}
\usepackage{multirow}
\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{tikz}
\definecolor{customcolor}{RGB}{77, 50, 52}
\definecolor{lightcolor}{RGB}{110, 70, 72}
\definecolor{lightgrey}{RGB}{220, 220, 220}
\definecolor{lightpink}{RGB}{255, 230, 235}
\definecolor{myorange}{RGB}{255, 165, 0}
\definecolor{mydarkorange}{RGB}{255, 140, 0}
\definecolor{darkprune}{RGB}{102, 34, 102}
\definecolor{darkviolet}{RGB}{115, 0, 230}
\setlength{\arrayrulewidth}{2pt}
\arrayrulecolor{white}
\newcommand\mycell[2]{%
\begin{tikzpicture}
\node[inner sep=0pt, minimum width=\linewidth, minimum height=3em] (a) {\phantom{#2}};
\fill[left color=customcolor, right color=lightcolor] (a.south west) rectangle (a.north east);
\node at (a) {#2};
\end{tikzpicture}%
}
\begin{document}
\begin{tabular}{|>{\columncolor{white}}c|>{\columncolor{white}}c|>{\columncolor{white}}c|>{\columncolor{white}}c|}
\hline
\multicolumn{2}{c}{\mycell{customcolor}{First cell content}} & \multicolumn{2}{c}{\mycell{customcolor}{Second cell content}} \\
\hline
\multirow{2}{*}{\cellcolor{lightgrey} Content} & \cellcolor{lightpink}\textcolor{darkprune}{Content} & \cellcolor{lightgrey} Content & \cellcolor{lightpink}\textcolor{darkprune}{Content} \\
\cline{2-4}
& \cellcolor{lightpink}\textcolor{darkprune}{Content} & \cellcolor{lightgrey} Content & \cellcolor{lightpink}\textcolor{darkprune}{Content} \\
\hline
\cellcolor{mydarkorange} Content & \cellcolor{myorange}\textcolor{darkviolet}{Content} & \cellcolor{mydarkorange} Content & \cellcolor{myorange}\textcolor{darkviolet}{Content} \\
\hline
\cellcolor{mydarkorange} Content & \cellcolor{myorange}\textcolor{darkviolet}{Content} & \cellcolor{mydarkorange} Content & \cellcolor{myorange}\textcolor{darkviolet}{Content} \\
\hline
\end{tabular}
\end{document}
答案1
- 彩色多行单元格的命令应位于跨行数为负数的跨行的最后:
...
\hline
\cellcolor{lightgrey}
& \cellcolor{lightpink}\textcolor{darkprune}{Content}
& \cellcolor{lightgrey} Content
& \cellcolor{lightpink}\textcolor{darkprune}{Content} \\
\cline{2-4}
\multirow{-2}{*}{\cellcolor{lightgrey} Content}
& \cellcolor{lightpink}\textcolor{darkprune}{Content}
& \cellcolor{lightgrey} Content
& \cellcolor{lightpink}\textcolor{darkprune}{Content} \\
\hline
...
- 但是,您的表格宽度有问题,可能是由 的定义引起的
\mycell
。您可以考虑直接为第一行着色。
附录:
不幸的是,我不清楚你想用彩色表格实现什么效果。所以我猜,通过使用包并定义新的单元格着色命令,可以更简单地编写该tblr
表格tabularray
:
\documentclass{article}
\usepackage{xcolor}
\definecolor{customcolor}{RGB}{77, 50, 52}
\definecolor{lightcolor}{RGB}{110, 70, 72}
\definecolor{lightgrey}{RGB}{220, 220, 220}
\definecolor{lightpink}{RGB}{255, 230, 235}
\definecolor{myorange}{RGB}{255, 165, 0}
\definecolor{mydarkorange}{RGB}{255, 140, 0}
\definecolor{darkprune}{RGB}{102, 34, 102}
\definecolor{darkviolet}{RGB}{115, 0, 230}
\usepackage{tabularray}
\NewTableCommand\SCC[1]{\SetCell{bg=#1}}
\begin{document}
\begin{tblr}{hlines={2pt,fg=white}, vlines={2pt,fg=white},
colspec = {*{2}{Q[l, fg=darkprune] X[c, fg=darkviolet]}},
row{1} = {ht=3em},
hspan = minimum}
\SetCell[c=2]{c, bg=customcolor,fg=white} First cell content
& & \SetCell[c=2]{c, bg=customcolor,fg=white} Second cell content
& \\
\SetCell[r=2]{bg=lightgrey} Content
& \SCC{lightpink} Content
& \SCC{lightgrey} Content
& \SCC{lightpink} Content \\
& \SCC{lightpink} Content
& \SCC{lightgrey} Content
& \SCC{lightpink} Content \\
\SCC{mydarkorange} Content
& \SCC{myorange} Content
& \SCC{mydarkorange} Content
& \SCC{myorange} Content \\
\SCC{mydarkorange} Content
& \SCC{myorange}\textcolor{darkviolet}{Content}
& \SCC{mydarkorange} Content
& \SCC{myorange} Content \\ %Removed extra brace
\end{tblr}
\end{document}
答案2
无需额外的节点。单个节点即可接受您需要设置的所有属性:文本和填充自定义颜色,包括阴影。您可以像调整三个单独的重叠节点一样调整尺寸。
我稍微简化了你的代码。例如,\coumncolor{white}...
如果你不使用白色背景,则似乎是多余的。最好将其设置为最常见的颜色,例如和,mydarkorange
并myorange
避免在每个单元格中重复局部颜色规范,除了那些需要单独设置的单元格。我删除了可选的,[table]
因为你加载colortbl
以避免潜在的包冲突。但是,如果你想添加其他有用的选项(svgnames
用于更多颜色定义),请将其放在xcolor
任何其他也加载的包之前xcolor
。
showframe
是一个方便的包,它绘制一个参考框架(蓝色),以便您可以看到表格如何适应页面。
下表:
以及代码:
\documentclass{article}
\usepackage{array}
\usepackage[svgnames]{xcolor}
\usepackage{colortbl}
\usepackage{multirow}
\usepackage{tikz}
\usepackage{showframe} % Draws reference frame around a page
\renewcommand*\ShowFrameLinethickness{0.2pt}
\renewcommand*\ShowFrameColor{\color{blue}}
\definecolor{customcolor}{RGB}{77, 50, 52}
\definecolor{lightcolor}{RGB}{110, 70, 72}
\definecolor{lightgrey}{RGB}{220, 220, 220}
\definecolor{lightpink}{RGB}{255, 230, 235}
\definecolor{myorange}{RGB}{255, 165, 0}
\definecolor{mydarkorange}{RGB}{255, 140, 0}
\definecolor{darkprune}{RGB}{102, 34, 102}
\definecolor{darkviolet}{RGB}{115, 0, 230}
\newcommand\mycell[2]{%
\tikz[baseline]
\node[
color=#1,
left color=customcolor,
right color=lightcolor,
minimum width=\linewidth,
minimum height=3em,
inner sep=0pt,
outer sep=0pt,
] {#2};}
\begin{document}
\noindent
\begingroup
\renewcommand{\arraystretch}{1.25}%
\setlength{\arrayrulewidth}{2pt}%
\arrayrulecolor{white}%
\centering
\begin{tabular}{|
>{\columncolor{mydarkorange}\color{Blue}}c|
>{\columncolor{myorange}\color{darkviolet}}c|
>{\columncolor{mydarkorange}\color{Blue}}c|
>{\columncolor{myorange}\color{darkviolet}}c |}
%%% Header
\hline
\multicolumn{2}{|@{}p{\dimexpr0.5\linewidth-1.5\arrayrulewidth}@{}|}{\mycell{white}{First cell content}}
& \multicolumn{2}{@{}p{\dimexpr0.5\linewidth-1.5\arrayrulewidth}@{}|}{\mycell{white}{Second cell content}} \\
\hline
%%% Subheader
\cellcolor{lightgrey}
& \cellcolor{lightpink}\textcolor{darkprune}{Content}
& \cellcolor{lightgrey}\textcolor{black}{Content}
& \cellcolor{lightpink}\textcolor{darkprune}{Content} \\
\multirow{-2}*{\cellcolor{lightgrey}\textcolor{black}{Content}}
& \cellcolor{lightpink}\textcolor{darkprune}{Content}
& \cellcolor{lightgrey}\textcolor{black}{Content}
& \cellcolor{lightpink}\textcolor{darkprune}{Content} \\
%%% Main content
\hline
Content & Content & Content & Content \\
\hline
Content & Content & Content & Content \\
\hline
Content & Content & Content & Content \\
\hline
\end{tabular}
\endgroup
\end{document}