![如何创建具有自定义颜色边框的表格?](https://linux22.com/image/336123/%E5%A6%82%E4%BD%95%E5%88%9B%E5%BB%BA%E5%85%B7%E6%9C%89%E8%87%AA%E5%AE%9A%E4%B9%89%E9%A2%9C%E8%89%B2%E8%BE%B9%E6%A1%86%E7%9A%84%E8%A1%A8%E6%A0%BC%EF%BC%9F.png)
请注意,单元格 9、17、25 和 13、21、29 的内容是左对齐的,而不是居中的。另外,边框线的宽度无关紧要。
到目前为止,我只做到了这一点:
\documentclass[11pt,a4paper]{book}
\usepackage[table]{xcolor}
\begin{document}
\begin{center}
\begin{tabular}{c|ccc|}
\arrayrulecolor{orange}\hline
\cline{2-4}
\multicolumn{1}{c|}{1} & \cellcolor{green} 2 & \cellcolor{green} 3 & \cellcolor{green} 4 \\ \hline
\multicolumn{1}{|c|}{\cellcolor{red} 9} & 10 & 11 & 12 \\ \arrayrulecolor{blue}\hline
\multicolumn{1}{|c|}{\cellcolor{red} 17} & 18 & 19 & 20 \\ \arrayrulecolor{blue}\hline
\multicolumn{1}{|c|}{\cellcolor{red} 25} & 26 & 27 & 28 \\ \arrayrulecolor{orange}\hline
\end{tabular}
\end{center}
\end{document}
其结果如下:
如果您能稍微解释一下您的代码,我将不胜感激,因为我需要创建更多这样的表(幸运的是,更简单的表)。
先感谢您!
答案1
这是一个解决方案,使用以下参数booktabs
:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} \usepackage{gillius2} \usepackage{geometry}
\usepackage[table, x11names]{xcolor}
\usepackage{booktabs, hhline} \usepackage{etoolbox}
\newcommand\thickvrule[1][DarkOrange2]{\ifblank{#1}{}{\color{#1}}\vrule width 2pt}
\begin{document}
\begin{table}
\sffamily\bfseries%
\centering\arrayrulecolor{DarkOrange2}
\setlength\aboverulesep{0pt}\setlength\belowrulesep{0pt}
\setlength\cmidrulekern{2pt}\setlength\cmidrulewidth{2pt}
\renewcommand\arraystretch{1.6}\setlength\tabcolsep{10pt}
\begin{tabular}{!{\thickvrule}>{\columncolor{Firebrick3!75}\hskip-3pt}p{5mm}!{\thickvrule}cc!{\thickvrule[Plum4]}c!{\thickvrule[Plum4]}% left table
p{2em}% emptycolumn
!{\thickvrule}>{\columncolor{Firebrick3!75}\hskip-3pt}p{5mm}!{\thickvrule} cc!{\thickvrule[Plum4]}c!{\thickvrule[Plum4]}}% right table
\arrayrulecolor{Plum4}
\cmidrule(l{-2pt}){4-9}
\multicolumn{3}{c!{\thickvrule[Plum4]}}{} & \multicolumn{6}{c!{\thickvrule[Plum4]}}{} \\[-3ex]
\cmidrule(l{-2pt}){5-8}
\arrayrulecolor{DarkOrange2}
\multicolumn{3}{c!{\thickvrule[Plum4]}}{} & \multicolumn{1}{c!{\thickvrule[Plum4]}}{} & \multicolumn{4}{c!{\thickvrule[Plum4]}}{} & \multicolumn{1}{c!{\thickvrule[Plum4]}}{} \\[-2ex]
\cmidrule(l{-2pt}r){2-3}\cmidrule(r){4-4}
\cmidrule(l{-2pt}r){7-8}
\cmidrule(r){9-9}
\rowcolor{OliveDrab4!60}\multicolumn{1}{c!{\thickvrule}}{\cellcolor{white}1} & 2 & 3 & 4 &\multicolumn{1}{c}{\cellcolor{white}} & \multicolumn{1}{c!{\thickvrule} }{\cellcolor{white}5} & 6 & 7 & 8 \\[-2.1pt]
\cmidrule(r){1-3} \cmidrule(r){4-4}\cmidrule(l{-2pt}r){6-8}\cmidrule(r){9-9}%
\arrayrulecolor{LightSkyBlue1}9 & 10 & 11 & 12 & & 13 & 14& 15 & 16 \\
\cline{2-4}\cline{7-9}
17 & 18 & 19 & 20 & & 21 & 22 & 23 & 24 \\
\cline{2-4}\cline{7-9}
\arrayrulecolor{DarkOrange2}
25 & 26 & 27 & 28 & & 29 & 30 & 31 & 32\\
\addlinespace[-2pt]
\cmidrule(r){1-3}\cmidrule(r){6-8}\addlinespace[-2pt]
\arrayrulecolor{Plum4}\cmidrule(r){4-4}\cmidrule(r){9-9}
\end{tabular}
\end{table}
\end{document}
答案2
您可以创建类似以下内容的内容
使用以下代码。代码带有注释以进行一些解释,但您需要查看tikzmark
和/或 TikZ 的手册以了解更多详细信息。后者很长,但您显然不需要其中的大部分内容,并且这里使用的部分很简单。
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}
\begin{document}
\begin{center}
\begin{tabular}[b]{|l|ccc|} % |l| to left align the column with lines on both sides; we then override this for the exceptional top left cell rather than all the others
\arrayrulecolor{orange}% remove \hline to avoid the line above the top left cell
\cline{2-4}
% \tikzmark{} is marking coordinates for later reference: a2, a1 etc.
\multicolumn{1}{c|}{1} & \cellcolor{green} 2 & \cellcolor{green} 3\tikzmark{a2} & \tikzmark{a1}\cellcolor{green} 4 \\ \hline
\cellcolor{red} 9 & 10 & 11 & 12 \\ \arrayrulecolor{blue}\hline
\cellcolor{red} 17 & 18 & 19 & 20 \\ \arrayrulecolor{blue}\hline
\cellcolor{red} 25 & 26 & 27 & 28 \\ \arrayrulecolor{orange}\hline
\end{tabular}%
\tikzmark{c}
\hskip 1.5em
\begin{tabular}[b]{|l|ccc|}
\arrayrulecolor{orange}
\cline{2-4}
\multicolumn{1}{c|}{1} & \cellcolor{green} 2 & \cellcolor{green} 3\tikzmark{b2} & \tikzmark{b1}\cellcolor{green} 4 \\ \hline
\cellcolor{red} 9 & 10 & 11 & 12 \\ \arrayrulecolor{blue}\hline
\cellcolor{red} 17 & 18 & 19 & 20 \\ \arrayrulecolor{blue}\hline
\cellcolor{red} 25 & 26 & 27 & 28 \\ \arrayrulecolor{orange}\hline
\end{tabular}%
\tikzmark{d}
\end{center}
% Now we use the marked coordinates to draw the blue shape over the top of the tables using a TikZ picture which is overlaid
\begin{tikzpicture}[remember picture,overlay]
% the \tikzmark uses the `pic` coordinate system so we start by making some regular coordinates e.g. turning ({pic cs:c}) into (c), but this gets tedious so we use loops for extensibility
% the a/b cases are special because we need to use a1, a2, b1 and b2 to get these positions using the `calc` library; we set a and b halfway between a1/a2 and b1/b2 respectively
\foreach \i in {a,b} \coordinate (\i) at ($({pic cs:\i1})!1/2!({pic cs:\i2})$);
% the c/d cases are straightforward
\foreach \i in {c,...,d} \coordinate (\i) at ({pic cs:\i});
% now we draw the loop using the coordinates we just defined: -- for straight; |- for up/down then across; -| for across then up/down
\draw [draw=blue, thick] ([yshift=20pt]a -| c) coordinate (e) -- (c |- d) -| ([yshift=30pt]a) -| (d) -| (b |- e) -- cycle;
\end{tikzpicture}
\end{document}
请注意,我不会这样做,因为我认为这个图表相当丑陋和混乱。如果你刚刚学会如何使用颜色,并且对它非常热衷,它看起来就像你在 Word 中制作的东西。
例如,不同的颜色代表什么?该图使用了红色、橙色、蓝色、绿色和无。这些颜色分别代表什么?我可以看到绿色和红色可能代表列和行标签,尽管这几乎没有必要,因为位置已经更清楚地代表了这些信息。
答案3
这是我的方法,甚至不如其他答案那么优雅,但是使用TiKZ Matrix
。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{gillius2} % Stolen from Bernard answer
\renewcommand\familydefault{\sfdefault}
\usepackage[dvipsnames]{xcolor} %always declare the xcolor package before tikz
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\begin{document}
\bfseries
\begin{tikzpicture}
\tikzset{square matrix/.style={
matrix of nodes,
column sep=-\pgflinewidth, row sep=-\pgflinewidth,
nodes={draw=none,
minimum height=12pt,
anchor=center,
text width=#1,
},
},
square matrix/.default=1.2cm
}
\matrix (first) [square matrix,
column 1/.style={nodes={fill=BrickRed,draw=none}},
column 2/.style={nodes={align=center}},
column 3/.style={nodes={align=center}},
column 4/.style={nodes={align=center}},
row 1/.style={nodes={draw=none,fill=white}},
row 2/.style={nodes={draw=none,fill=white}},
row 3/.style={nodes={fill=LimeGreen,draw=none}},
]
{
{} & {} & {} & {} \\
{} & {} & {} & {} \\
|[draw=none,fill=white,align=center]| 1 & 2 & 3 & 4 \\
9 & 10 & 11 & 12 \\
17 & 18 & 19 & 20 \\
25 & 26 & 27 & 28 \\
};
\matrix (second) [right=of first,square matrix,
column 1/.style={nodes={fill=BrickRed,draw=none}},
column 2/.style={nodes={align=center}},
column 3/.style={nodes={align=center}},
column 4/.style={nodes={align=center}},
row 1/.style={nodes={draw=none,fill=white}},
row 2/.style={nodes={draw=none,fill=white}},
row 3/.style={nodes={fill=LimeGreen,draw=none}},
]
{
{} & {} & {} & {} \\
{} & {} & {} & {} \\
|[draw=none,fill=white,align=center]| 5 & 6 & 7 & 8 \\
13 & 14 & 15 & 16 \\
21 & 22 & 23 & 24 \\
29 & 30 & 31 & 32 \\
};
\draw[blue] (first-4-2.south west) -- (first-4-4.south east);
\draw[blue] (first-5-2.south west) -- (first-5-4.south east);
\draw[blue] (second-4-2.south west) -- (second-4-4.south east);
\draw[blue] (second-5-2.south west) -- (second-5-4.south east);
%
\draw[YellowOrange, ultra thick] (second-4-2.north west) -- (second-6-2.south west) -- (second-6-1.south west) -- (second-6-1.south west) -- (second-3-1.south west) -- (second-3-2.south west) ;
\draw[YellowOrange, ultra thick] (second-4-2.north west) -- (second-3-2.north west) -- (second-3-4.north east) -- (second-4-4.north east) -- (second-3-2.south west) ;
\draw[YellowOrange, ultra thick] (second-6-2.south west) -- (second-6-4.south west);
%
\draw[YellowOrange, ultra thick] (first-4-2.north west) -- (first-6-2.south west) -- (first-6-1.south west) -- (first-6-1.south west) -- (first-3-1.south west) -- (first-3-2.south west) ;
\draw[YellowOrange, ultra thick] (first-4-2.north west) -- (first-3-2.north west) -- (first-3-4.north east) -- (first-4-4.north east) -- (first-3-2.south west) ;
\draw[YellowOrange, ultra thick] (first-6-2.south west) -- (first-6-4.south west);
%
\draw[Violet,ultra thick] (first-6-4.south east) -- (first-6-4.south west) -- (first-1-4.north west) -- (second-1-4.north east) -- (second-6-4.south east) -- (second-6-4.south west) -- (second-1-4.south west) -- (first-1-4.south east) -- (first-6-4.south east) -- (first-6-4.south west);
\end{tikzpicture}
\end{document}
我的代码部分改编自以下问题/答案:数组中方形单元格的颜色,TikZ 矩阵作为表格的替代品,带有 tikz 的大块矩阵。
答案4
{NiceTabular}
这是使用和 TikZ 的解决方案nicematrix
。
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\renewcommand{\arraystretch}{1.3}
\newcommand{\BlueLine}{\Hline[tikz={blue,line width=0.4pt}]}
\newcommand{\MyBlock}{\Block[draw=orange,line-width=1pt]}
\begin{NiceTabular}{w{l}{6mm}ccc}[name=A]
\Block[c]{}{1} & \MyBlock[fill=green!20]{1-3}{} 2 & 3 & 4\\
\MyBlock[fill=red!30]{3-1}{}
9 & \MyBlock[transparent]{3-3}{} 10 & 11 & 12 \\
\BlueLine
17 & 18 & 19 & 20 \\
\BlueLine
25 & 26 & 27 & 18
\end{NiceTabular}
\qquad
\begin{NiceTabular}{w{l}{6mm}ccc}[name=B]
\Block[c]{}{5} & \MyBlock[fill=green!20]{1-3}{} 6 & 7 & 8\\
\MyBlock[fill=red!30]{3-1}{}
13 & \MyBlock[transparent]{3-3}{} 14 & 15 & 16 \\
\BlueLine
21 & 22 & 23 & 24 \\
\BlueLine
29 & 30 & 31 & 32
\end{NiceTabular}
%
\begin{tikzpicture}[remember picture, overlay]
\draw [line width=1.2 pt, purple]
(A-5-|A-4) -| ([yshift=2mm]A-1-|A-5) -| (B-5-|B-4) -| ([yshift=3mm]B-1-|B-5) -| cycle ;
\end{tikzpicture}
\end{document}