Multi-colored cell background
Based on @leoLiu solution to create cell with diagonal line.
In the example I use suinitx
rather then dcolumn
to align numbers on decimal point.
\documentclass[11pt]{article}
\usepackage{array}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{tikz}
\newcommand\diagfil[4]{%
\multicolumn{1}{p{#1}}{\hskip-\tabcolsep
$\vcenter{\begin{tikzpicture}[baseline=0,anchor=south west,inner sep=0pt,outer sep=0pt]
\path[use as bounding box] (0,0) rectangle (#1+2\tabcolsep,\baselineskip);
\node[minimum width={#1+2\tabcolsep},minimum height=\baselineskip+\extrarowheight+\belowrulesep+\aboverulesep,fill=#2] (box)at(0,-\aboverulesep) {};
\fill [#3] (box.south west)--(box.north east)|- cycle;
\node[anchor=center] at (box.center) {#4};
\end{tikzpicture}}$\hskip-\tabcolsep}}
\begin{document}
\centering
\begin{tabular}{SSSSS}
\toprule
\diagfil{2cm}{blue}{purple}{Text} &&&&2.0\\
\midrule
& & & &3.03\\
15&12&18&50&4.5\\
15&12&18&50&4.54\\
\bottomrule
\end{tabular}
\end{document}
A more flexible background definition can be done by collecting from my answer to TikZ: Rectangle with diagonal fill (two colors) and Gonzalo's answer to Set table background hatched and shaded using tikz. There are some spacing issues, mostly (I think) because I'm not quite aware of the padding macros inside the tabular
cells, specially with booktabs
rules (toprule
, \midrule
,...), so it works better with \hline
right now, but I'm sure it can be easily improved.
The column specifier is not overruled, but it needs to be specified explicitly. In the below MWE I use dcolumn
but I recommend the use of siunitx
instead, as done by Salim Bou.
MWE
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{tikzmark, calc}
% Makes a style for double color filling
\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}
}
}
% Code adapted from Gonzalo: https://tex.stackexchange.com/a/67096/81905
\newcommand\BGcell[4][0pt]{%
\begin{tikzpicture}[overlay,remember picture]%
\path[#4] ( $ (pic cs:#2) + (-.5\tabcolsep,1.9ex) $ ) rectangle ( $ (pic cs:#3) + (\tabcolsep,-#1*\baselineskip-.8ex) $ );
\end{tikzpicture}%
}%
\newcounter{BGnum}
\setcounter{BGnum}{1}
\newcommand\cellBG[3]{
\multicolumn{1}{
!{\BGcell{startBG\arabic{BGnum}}{endBG\arabic{BGnum}}{%
#1}
\tikzmark{startBG\arabic{BGnum}}}
#2
!{\tikzmark{endBG\arabic{BGnum}}}}
{#3}
\addtocounter{BGnum}{1}
}
% end of code from Gonzalo
\usepackage{dcolumn, array, booktabs}
\newcolumntype{.}{D{.}{.}{-1}}
\newcommand*{\tabhead}[1]{\multicolumn{1}{c}{#1}}
\begin{document}
\tikzset{%
diagonal fill/.style 2 args={%
double color fill={#1}{#2},
shading angle=45,
opacity=0.8},
other filling/.style={%
shade,
shading=myshade,
shading angle=0,
opacity=0.5}
}
% Other crazy filling options using shading
\pgfdeclarehorizontalshading{myshade}{100bp}{%
color(0bp)=(blue);
color(25bp)=(blue);
color(37.5bp)=(blue);
color(37.5bp)=(brown);
color(50bp)=(brown);
color(50bp)=(green);
color(62.5bp)=(green);
color(62.5bp)=(purple);
color(75bp)=(red);
color(100bp)=(red)}
\begin{tabular}{c.}
\toprule
\cellBG{double color fill={red}{blue}, shading angle=-45, opacity=0.5}{c}{Header 1} & \tabhead{Header 2}\\
\midrule
\cellBG{diagonal fill={yellow}{green}}{c}{Text} & \cellBG{other filling}{.}{1.2333}\\
\cellBG{other filling}{c}{Text} & 154.622\\
Text & 1.244\\
Text & 11.3\\
Text & 121.2\\
\bottomrule
\end{tabular}
\end{document}
This method relies on TikZ shadings, they're not so complicated to define and are very powerful. One thing to take into account when declaring a shading is that if you define it to be of 100bp
it will automatically scale, and the visible part of the shading will be between 25bp
and 75bp
, that's why my Multi Color shading has blue from 0bp
to 25bp
, because that part is actually hidden. The key shading angle
can be used to set the diagonal slant, other types of shading can be defined as well (check the manual for more info).