有没有办法只给表格单元格的一部分着色?例如,单元格的顶部三分之一或底部一半?
例如:
\renewcommand{\arraystretch}{3.0}
\begin{tabularx}{10cm}{|X|X|}
\hline
Row 1 & \cellcolor{blue!25} 1/3 colored \\
\hline
Row 2 & uncolored \\
\hline
\end{tabularx}
\renewcommand{\arraystretch}{1.0}
\begin{tabularx}{10cm}{|X|X|}
\hline
&\cellcolor{blue!25} \\
& \\
& \\
\hline
& \\
& \\
& \\
\hline
\end{tabularx}
\renewcommand{\arraystretch}{1.0}
\begin{tabularx}{10cm}{|X|X|}
\hline
Row 1 &\cellcolor{blue!25} 1/3 colored\\
& \\
& \\
\hline
Row 2 & uncolored \\
& \\
& \\
\hline
\end{tabularx}
第一种情况显示了文本应如何显示,但显然单元格已完全着色。在第二种情况下,我将两行拆分为六行,以便原始单元格的三分之一可以单独着色。但是添加文本时,它不再位于真实单元格的中间,因为它被放在子单元格中。
做这个的最好方式是什么?
答案1
如果你想对单元格的阴影进行大量的控制,那么我建议matrix of nodes
使用蒂克兹包 --- 请参阅非常全面的手册的第 57.1 章。例如,您可以生成:
使用代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\begin{document}
\begin{tikzpicture}
\matrix (M)[matrix of nodes]{
Row 1 & 1/3 colored \\
Row 2 & uncolored \\
Row 3 & uncolored \\
Row 4 & uncolored \\
Row 5 & uncolored \\
Row 6 & uncolored \\
};
% cell (1,2) with bluish background
\draw[opacity=0.2,fill=blue!50](M-1-2.north west) rectangle (M-1-2.south east);
% bottom third of cell (2,2) with bluish background
\draw[opacity=0.2,fill=blue!50] ($ (M-2-2.north west)!0.67!(M-2-2.south west) $)
rectangle(M-2-2.south east);
% triangular shading of cell (4,2)
\draw[opacity=0.5, fill=green!50,draw=yellow, thick]
(M-4-2.north west)--(M-4-2.south west)--(M-4-2.north east)--cycle;
% crossing out cells in rows 5 and 6
\draw[thick, red](M-5-1.north west)--(M-6-2.south east);
\draw[thick, red](M-6-1.south west)--(M-5-2.north east);
\end{tikzpicture}
\end{document}
答案2
环境与经典环境类似,但会在单元格、行和列下创建 PGF/TikZ 节点。然后,可以在构建主数组之前或之后使用这些节点与 TikZ 一起绘制您想要的任何内容{NiceTabular}
。nicematrix
{tabular}
\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\renewcommand{\arraystretch}{3.0}
\begin{NiceTabularX}{10cm}{XX}[hvlines]
\CodeBefore
\tikz \fill [blue!25] (1-|2) rectangle ($(1-|3)!0.3!(2-|3)$) ;
\Body
Row 1 & 1/3 colored \\
Row 2 & uncolored \\
\end{NiceTabularX}
\end{document}
还可以编写一个命令\Third
,当在单元格内使用时,该命令将为该单元格的一部分着色。
\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{calc}
\ExplSyntaxOn
\NewDocumentCommand{\Third}{}
{
\tl_gput_right:Nx \g_nicematrix_code_before_tl
{ \__ganter:nn { \arabic{iRow} } { \arabic{jCol } } }
}
\cs_new_protected:Nn \__ganter:nn
{
\tikz \fill [blue!25]
(#1-|#2)
rectangle
($(#1-|\int_eval:n{#2+1})!0.3!(\int_eval:n{#1+1}-|\int_eval:n{#2+1})$) ;
}
\ExplSyntaxOff
\begin{document}
\renewcommand{\arraystretch}{3.0}
\begin{NiceTabularX}{10cm}{XX}[hvlines]
Row 1 & \Third 1/3 colored \\
\Third Row 2 & uncolored \\
\end{NiceTabularX}
\end{document}