我改编了帖子中的一个解决方案多色单元格背景使用 Overleaf。
我使用的代码如下:
%%% This code comes from https://tex.stackexchange.com/questions/348933/multi-colored-cell-background with minor adjustments regarding the format of the table %%%
\documentclass{article}
\usepackage{colortbl}
\usepackage{tikz}
\usepackage{nicematrix}
\usepackage{siunitx}
\usepackage{booktabs}
\begin{document}
\makeatletter
\tikzset{
double color fill/.code 2 args={
\pgfdeclareverticalshading[%
tikz@axis@top,tikz@axis@middle,tikz@axis@bottom%
]{diagonalfill}{100bp}{%
color(0bp)=(tikz@axis@bottom);
color(50bp)=(tikz@axis@bottom);
color(50bp)=(tikz@axis@middle);
color(50bp)=(tikz@axis@top);
color(100bp)=(tikz@axis@top)
}
\tikzset{shade, left color=#1, right color=#2, shading=diagonalfill}
}
}
\makeatother
\tikzset{%
diagonal fill/.style 2 args={%
double color fill={#1}{#2},
shading angle=45,
opacity=0.8},
other filling/.style={%
shade,
shading=myshade, % myshade is defined below
shading angle=0,
opacity=0}
}
\noindent\begin{NiceTabular}{| p{0.25\textwidth}
|p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
|p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
|p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
|p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
|}
\hline
\textbf{Lorem}
& \multicolumn{4}{c|}{dolor}
& \multicolumn{4}{c|}{sit}
& \multicolumn{4}{c|}{amet}
& \multicolumn{4}{c|}{beneficae} \\ \cline{2-17}
\hline
\hline
\textbf{ipsum} & \multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} \\
\hline
Lorem ipsum dolor sit amet & \Block[tikz={diagonal fill={yellow}{green}}]{}{} & \Block[tikz={diagonal fill={yellow}{green}}]{}{} & & \\
\hline
\end{NiceTabular}
\end{document}
与原始帖子相反,这两种颜色并没有严格分开,但它们有渐变,尽管我很确定我使用的是相同的代码。我想摆脱这种渐变,有人能帮我吗?
多谢!
答案1
你也可以尝试这种方法(取自这里) 不使用该shadings
库,因此应该在 Overleaf 上正确显示:
\documentclass{article}
\usepackage{colortbl}
\usepackage{tikz}
\usepackage{nicematrix}
\usepackage{siunitx}
\usepackage{booktabs}
\begin{document}
\tikzset{%
diagonal fill/.style 2 args={%
fill=#2, path picture={
\fill[#1, sharp corners]
(path picture bounding box.south west) -|
(path picture bounding box.north east) -- cycle;
}
},
}
\noindent\begin{NiceTabular}{| p{0.25\textwidth}
|p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
|p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
|p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
|p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
|}
\hline
\textbf{Lorem}
& \multicolumn{4}{c|}{dolor}
& \multicolumn{4}{c|}{sit}
& \multicolumn{4}{c|}{amet}
& \multicolumn{4}{c|}{beneficae} \\ \cline{2-17}
\hline
\hline
\textbf{ipsum} & \multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} \\
\hline
Lorem ipsum dolor sit amet & \Block[tikz={diagonal fill={yellow}{green}}]{}{} & \Block[tikz={diagonal fill={yellow}{green}}]{}{} & & \\
\hline
\end{NiceTabular}
\end{document}
附录
如果您需要调整角度,您可以使用上述方法的增强版本,将角度作为第三个参数(我使用了一个更简单的示例来展示应用程序,但您也可以将其应用于上面的代码):
\documentclass[border=1mm, tikz]{standalone}
\usetikzlibrary{calc}
\tikzset{%
diagonal fill/.style n args={3}{%
fill=#2, path picture={
\fill[#1, rotate=#3]
let \p1 = ($(path picture bounding box.south west)-(path picture bounding box.north east)$)
in (path picture bounding box.center) -- ++(0,{veclen(\x1,\y1)}) arc (90:-90:{veclen(\x1,\y1)}) -- cycle;
}
},
}
\begin{document}
\begin{tikzpicture}
\fill[diagonal fill={red}{blue}{30}] (0,0) rectangle ++(5,5);
\fill[diagonal fill={gray}{cyan}{-10}] (7.5,2.5) ellipse (2 and 3);
\fill[diagonal fill={yellow}{green}{135}] (10,1) rectangle ++(6,3);
\end{tikzpicture}
\end{document}