# Table with TikZ

I should create a table like the one here as example. I think that the best way to do a table like this one is generate some labels and use them to colorize the nodes in order to make it more automatic. First column are categorized colors and right column are a color spectrum. My question is if you have to face a problem like this one which tools from TikZ would you use to generate such table. I started to code the table in TikZ but I was defining everything: The position of the nodes, all the line intersecting points to create the lines afterwards. The code gets huge, nasty, and horrible to check if i I've placed the colors properly.

Therefore, do you have any suggestions of how would you do this job?

-
See tex.stackexchange.com/questions/30168/…, I think the UI concept offered there would easily be twisted for this diagram and would provide cleaner code. – Yiannis Lazarides Sep 3 '13 at 0:22

This can give you a starting point:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\definecolor{redi}{RGB}{255,38,0}
\definecolor{redii}{RGB}{200,50,30}
\definecolor{yellowi}{RGB}{255,251,0}
\definecolor{bluei}{RGB}{0,150,255}
\definecolor{blueii}{RGB}{135,247,210}
\definecolor{blueiii}{RGB}{91,205,250}
\definecolor{blueiv}{RGB}{115,244,253}
\definecolor{bluev}{RGB}{1,58,215}
\definecolor{orangei}{RGB}{240,143,50}
\definecolor{yellowii}{RGB}{222,247,100}
\definecolor{greeni}{RGB}{166,247,166}

\tikzset{
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={rectangle,draw=black,text width=1.25ex,align=center},
text depth=0.25ex,
text height=1ex,
nodes in empty cells
},
texto/.style={font=\footnotesize\sffamily},
title/.style={font=\small\sffamily}
}

\newcommand\CellText[2]{%
\node[texto,left=of mat#1,anchor=east]
at (mat#1.west)
{#2};
}

\newcommand\SlText[2]{%
\node[texto,left=of mat#1,anchor=west,rotate=75]
at ([xshift=3ex]mat#1.north)
{#2};
}

\newcommand\RowTitle[2]{%
\node[title,left=6.3cm of mat#1,anchor=west]
at (mat#1.north west)
{#2};
}

\begin{document}

\begin{tikzpicture}[node distance =0pt and 0.5cm]

\matrix[table] (mat11)
{
|[fill=redi]| & & & \\
|[fill=redi]| & & & \\
|[fill=redi]| & & & \\
};
\matrix[table,right=of mat11] (mat12)
{
|[fill=orangei]| & |[fill=yellowii]| & |[fill=blueii]| & |[fill=blueiii]| \\
& |[fill=blueii]| & |[fill=greeni]| & |[fill=blueiv]| \\
& & |[fill=bluev]| & |[fill=blueiii]| \\
};
\matrix[table,below=of mat11] (mat21)
{
|[fill=redi]| & & & \\
|[fill=redi]| & & & \\
|[fill=yellowi]| & & & \\
|[fill=yellowi]| & & & \\
};
\matrix[table,below=of mat12] (mat22)
{
|[fill=bluev!95]| & & |[fill=yellowii]| & |[fill=orangei!80]| \\
& |[fill=yellowii]| & |[fill=bluev!80]| & |[fill=blueiii]| \\
|[fill=redii]| & |[fill=blueiii!80]| & & |[fill=bluev]| \\
|[fill=bluev]| & & |[fill=blueiii!80]| & |[fill=bluev]| \\
};
\matrix[table,below=of mat21] (mat31)
{
|[fill=redi]| & & & \\
|[fill=bluei]| & & & \\
};
\matrix[table,below=of mat22] (mat32)
{
|[fill=bluev]| & & |[fill=redii]| & |[fill=redii!90!black]| \\
|[fill=redii!90]| & |[fill=redii!75!black]| & |[fill=bluev]| & |[fill=bluev!80!black]| \\
};

\SlText{11-1-2}{Simple Cyst}
\SlText{11-1-3}{Complex Cyst}
\SlText{11-1-4}{Papilloma}

\SlText{12-1-1}{CDI}
\SlText{12-1-2}{CLI}
\SlText{12-1-3}{FA}
\SlText{12-1-4}{Cyst}

\RowTitle{11}{Background echotexture};
\CellText{11-2-1}{Homogeneous fibroglandular-echotexture};
\CellText{11-3-1}{Hoterogeneous};

\RowTitle{21}{Mass shape};
\CellText{21-1-1}{Oval};
\CellText{21-2-1}{Round};
\CellText{21-3-1}{Irregular};

\RowTitle{31}{Mass orientation};
\CellText{21-4-1}{Lobular};
\CellText{31-1-1}{Parallel to skin};
\CellText{31-2-1}{Non-parallel to skin};

\end{tikzpicture}

\end{document}


-

You don't have to use Tikz;-)

\documentclass{article}

\usepackage{xcolor,array,graphicx}

\makeatletter
\newcolumntype{Z}{>{\zz}p{1em}|}

\def\zz\ignorespaces#1{%
\def\thiscol{white}%
\if S#1\expandafter\szz
\else\if R#1\def\thiscol{red}%
\else\if Y#1\def\thiscol{yellow}%
\else\if B#1\def\thiscol{blue}%
\else\if X#1\def\thiscol{black}%
\else#1\fi\fi\fi\fi
\expandafter\zzz
\fi}
\def\szz{\afterassignment\@szz\count@0}
\def\@szz{\edef\thiscol{mycolor-\the\count@}\zzz}

\definecolor{mycolor-1}{rgb}{.8,.4,.6}
\definecolor{mycolor-20}{rgb}{.7,.9,.6}
\definecolor{mycolor-5}{rgb}{.2,.4,.6}
\definecolor{mycolor-4}{rgb}{.2,.7,.3}
\begin{document}
\renewcommand\arraystretch{0}
\setlength\tabcolsep{0pt}
\begin{tabular}{l@{\hspace{2em}}r@{\hspace{2pt}}|ZZZZ@{\hspace{1cm}}ZZZZ}
\multicolumn{1}{l}{\rlap{\rotatebox{60}{something}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{something else}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{ho hum}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{more stuff}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{and more}}}&
\multicolumn{1}{l@{\hspace{1cm}}}{\rlap{\rotatebox{60}{interesting column}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{ABC}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{XYZ}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{EED}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{JHY}}}\\
\hline
stuff& foo &R&&&&  S1 &&&\\\hline
stuff& foo &Y&&&&  S20 &&S5&\\\hline
stuff& foo &B&&&&  S5 &&S1&\\\hline
stuff& foo &X&&&&  S4 &&&S20\\\hline
\end{tabular}

\end{document}

-

You don’t have to use TikZ (but you can).

I would use either pgfplotstable (but that would make a hard start) or at least the collcell package to make the creation easier.

If all you want is colored cells define the needed colors with a distinctive name with \colorlet or \definecolor and use that as the cell content that is than grabbed by \collectcell and given to \myBox as an argument.

In this example, this is simply used to use a \cellcolor but can be provided to TikZ that draws a rectangle, or could be the name of a key (think pgfkeys) that in turns can do anything (\cellcolor, simply text, TikZ).

## Code

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{array,collcell}
\newcommand*{\myBox}[1]{\cellcolor{col@#1}}
\colorlet{col@a}{red}  \colorlet{col@b}{blue}
\colorlet{col@c}{cyan} \colorlet{col@d}{black}
\colorlet{col@e}{green}\colorlet{col@f}{red!50!yellow}
\newcolumntype{C}[1]{>{\collectcell\myBox}#1<{\endcollectcell}}
\begin{document}
\begin{tabular}{l C{c} C{c} p{.5cm} C{c} C{c}}
Foo                    & a                         & b                         & & c                         & d                         \\
Bar                    & e                         & a                         & & d                         & f
\end{tabular}
\end{document}


# A little bit of TikZ

Here is a more sophisticated approach.

TikZ is used to draw the boxes, nothing more.

pgfkeys is used to define a few simply styles like common and probable. The . style is a minimalistic style that only draws the border bot doesn’t use any filling. The .unknown key is set up so that you can use any color that is then used as an argument to the box style; but I recommend defining styles at least for common cells (the colors in the boxes on the right side surely mean something).

## Code

\documentclass{article}
\usepackage{tikz,collcell}
\newcommand*{\cellset}{\pgfqkeys{/cell}}
\newcommand*{\myCell}{\cellset}
\newcolumntype{C}[1]{>{\collectcell\myCell}#1<{\endcollectcell}}
\makeatletter
\cellset{.unknown/.code={\edef\pgfkeys@temp{\noexpand\cellset{box=\pgfkeyscurrentname}}\pgfkeys@temp}}
\makeatother
\cellset{
myCell/.style={
draw=black,
overlay linewidth,
inner sep=+0pt,
outer sep=+0pt,
anchor=center,
fill={#1},
minimum size=+1.7em},
box/.code={%
\tikz[baseline=-1ex]
\node[/cell/myCell={#1}]{};},
box/.default=none,
./.style={box},
@define/.style args={#1:#2}{#1/.style={box=#2}},
@define/.list={%
not present:black,  possible:cyan,
probable:   yellow, common:  red,
o:          orange, y:       yellow}
}
\begin{document}
\begin{tabular}{>{\footnotesize}r *4{C{c} @{}} @{\hspace*{.5cm}} *4{C{c} @{}}}
\multicolumn{1}{l}{Background Echotexture} &                        &                  &                   &               &          &              &         &  \\
Homogeneous adipose-echotexture & common                 & .                & .                 & .             & o        & y            & .       & .              \\
Homogeneous fibroglandular-echotexture & common                 & .                & .                 & .             & blue     & .            & .       & .              \\
Homogeneous adipose-echotexture & common                 & .                & .                 & .             & .        & .            & .       & .              \\[1em]
\multicolumn{1}{l}{Mass Shape} &                        &                  &                   &               &          &              &         &  \\
Oval & common                 & .                & .                 & .             & .        & .            & .       & o              \\
Round & common                 & .                & .                 & .             & .        & .            & .       & .              \\
Irregular & probable               & .                & .                 & .             & .        & .            & .       & .              \\
Iobular & probable               & .                & .                 & .             & .        & .            & .       & .              \\[1em]
\multicolumn{1}{l}{Mass Orientation} &                        &                  &                   &               &          &              &         &  \\
Parallel to skin & common                 & .                & .                 & .             & blue     & .            & red     & red!50!black   \\
Non-parallel to skin & possible               & .                & .                 & .             & red      & red!30!black & blue    & blue!50!black
\end{tabular}
\end{document}


## Output

-
I knew you wouldn't be able to resist tikz in the end. – David Carlisle Sep 2 '13 at 15:44