I'm wondering if these is a simple way shade an entry in a table with multiple background colors. Image attached shows between 0-3 colors per table item entry.
Asked
Active
Viewed 786 times
4
2 Answers
13
This is straightforward using TikZ:
The code:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc,backgrounds}
\definecolor{mybrown}{RGB}{33,34,28}
\definecolor{myyellow}{RGB}{242,226,149}
\definecolor{mygreen}{RGB}{176,232,145}
\definecolor{myblue}{RGB}{61,139,189}
\definecolor{myorange}{RGB}{245,156,74}
\definecolor{mypurple}{RGB}{230,111,148}
\definecolor{myred}{RGB}{215,80,50}
\begin{document}
\begin{tikzpicture}
\matrix[
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={
draw,
text width=5em,
align=center,
minimum height=3ex
}
] (mat)
{
Title1 & Title2 \\
Foo1 & Bar1 \\
Foo2 & Bar2 \\
Foo3 & |[fill=myblue]|Bar3 \\
Foo4 & Bar4 \\
};
\begin{pgfonlayer}{background}
%two color fill
\fill[fill=myblue!80]
(mat-2-2.north west) rectangle
( $ (mat-2-2.south west)!0.5!(mat-2-2.south east) $ );
\fill[fill=mypurple!80]
( $ (mat-2-2.north west)!0.5!(mat-2-2.north east) $ ) rectangle
(mat-2-2.south east);
%three color fill
\fill[fill=myred!80]
(mat-3-2.north west) rectangle
( $ (mat-3-2.south west)!0.3333!(mat-3-2.south east) $ );
\fill[fill=mygreen!80]
( $ (mat-3-2.north west)!0.3333!(mat-3-2.north east) $ ) rectangle
( $ (mat-3-2.south west)!0.6666!(mat-3-2.south east) $ );
\fill[fill=myblue!80]
( $ (mat-3-2.north west)!0.6666!(mat-3-2.north east) $ ) rectangle
(mat-3-2.south east);
%four color fill
\fill[fill=myorange!80]
(mat-5-2.north west) rectangle
( $ (mat-5-2.south west)!0.25!(mat-5-2.south east) $ );
\fill[fill=mygreen!80]
( $ (mat-5-2.north west)!0.25!(mat-5-2.north east) $ ) rectangle
( $ (mat-5-2.south west)!0.50!(mat-5-2.south east) $ );
\fill[fill=myyellow]
( $ (mat-5-2.north west)!0.50!(mat-5-2.north east) $ ) rectangle
( $ (mat-5-2.south west)!0.75!(mat-5-2.south east) $ );
\fill[fill=myblue!80]
( $ (mat-5-2.north west)!0.75!(mat-5-2.north east) $ ) rectangle
(mat-5-2.south east);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
Gonzalo Medina
- 505,128
-
2+1. I wouldn't exactly call this stuff "easy", though. Maybe "straightforward"? – Mico Aug 27 '15 at 04:46
-
0
A solution non generalized with array, xcolor. The problem that i have encountered is centering "Bar1" any one has a best way of centering is invited to correct it.
Code
\documentclass[margin=2mm]{standalone}
\usepackage{array}
\usepackage[table]{xcolor}
\newlength{\len}
\settowidth{\len}{Bar1}
\def\mylen{\dimexpr -0.2\len -2\tabcolsep}
\newcolumntype{C}[1]{>{\centering\cellcolor{#1!25}}m{\dimexpr 1cm-2\tabcolsep}}
\newcolumntype{Y}[1]{>{\centering\cellcolor{#1!25}}m{\dimexpr 2cm-2\tabcolsep}}
\newcolumntype{Z}[1]{>{\centering\cellcolor{#1!25}}m{\dimexpr 4cm-2\tabcolsep}}
\newcolumntype{L}[1]{>{\raggedright\cellcolor{#1!25}}m{\dimexpr 2cm-2\tabcolsep}}
\begin{document}
\setlength\arrayrulewidth{0.8pt}
\begin{tabular}{|c|cccc|}
\hline
Title1 & \multicolumn{4}{|c|}{Title2}\\
\hline
Foo1 & \multicolumn{2}{|Y{green}}{}& \multicolumn{2}{L{blue}|}{\hspace*{\mylen}Bar1}\\
\hline
Foo2 & \multicolumn{1}{|C{red}}{}& \multicolumn{2}{Y{green}}{Bar2}&\multicolumn{1}{C{blue}|}{}\\
\hline
Foo3 & \multicolumn{4}{|Z{blue}|}{Bar3}\\
\hline
Foo4 & \multicolumn{4}{|c|}{Bar4}\\
\hline
\end{tabular}
\end{document}
Result
Salim Bou
- 17,021
- 2
- 31
- 76



tikzmatrixand fill them at will. – Aug 27 '15 at 00:51tikzmafia! :-) I came back to answer only to find that you already did. :) – Aug 27 '15 at 03:16