# How can you put multicolor backgrounds in a table entry?

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.

• You can use a tikz matrix and fill them at will. – user11232 Aug 27 '15 at 0:51
• Just posting an image and crossing your fingers sometimes work if a member of the procrastination team is particularly interested and takes the hook. But a more reliable strategy is to post a minimum working example with the code you've got, which does the parts you can and allows you to ask a specific question about a particular obstacle you are facing. This also makes your question more generally useful to other users, less likely to be misunderstood and, therefore, both easier and less hazardous to answer. – cfr Aug 27 '15 at 1:29
• @cfr Is there such a thing as a procrastination team around here? ;) – Gonzalo Medina Aug 27 '15 at 2:57
• @GonzaloMedina There is tikz mafia! :-) I came back to answer only to find that you already did. :) – user11232 Aug 27 '15 at 3:16
• @GonzaloMedina Of course! The team is even featured on meta . No idea who is in it, though ;). – cfr Aug 27 '15 at 12:06

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}

• +1. I wouldn't exactly call this stuff "easy", though. Maybe "straightforward"? – Mico Aug 27 '15 at 4:46
• @Mico Agreed. Change made. – Gonzalo Medina Aug 27 '15 at 14:38

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