# Create an array in TikZ with \underbrace in different colors

I would like to create TikZ something like:

-------------------------------  ...   ----------------
| 0000 | 0001 | 0010 | 0100 |           | 0111 | 1111 |
------------------------------  ...   -----------------
\     / \                  /                    \     /
\M_0/   \------ M_1------/                      \M_d/


where I could give the first box one color, the 3 box after words different color and the last one a 3rd color.

I tried to create \underbrace for the M_0, M_1 and M_d

Does someone know where I could find short example for something like this?

EDIT

Thank you for the nice solutions. In my example above it was hard to see and also to create an example for it, so below is an example how i would like to have the connecting box. I hope it is also possible a relative easy way.

-
You could have a look at this question: tex.stackexchange.com/questions/31336/…. It should be straightforward to add some color in TikZ. Just use the color=colorname option. –  Roelof Spijker Dec 12 '11 at 16:30
If the above link solves your question, we should close this one. If it doesn't, please make clear how it doesn't. –  Seamus Dec 12 '11 at 16:51
Answers in the link are (too) complex for this kind of problem but it's a personal point of view. –  Alain Matthes Dec 12 '11 at 17:10
@Seamus, this solution is in the direction of what i am searching for, but not exactly. Please see the edit part. –  Eagle Dec 12 '11 at 17:56
@Eagle: I've updated my answer to include dashed lines - slightly crude, but since I had something sans-tikz originally, I stuck to it. :) –  Werner Dec 12 '11 at 18:40

This solution builds on Altermundus answer; I only used the shapes library to add two "tapes" to get the desired aspect for the interrupted band:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,shapes}

\begin{document}
\begin{tikzpicture}
\foreach \c/\i [count=\n] in
{blue!20/0001,red!20/0001,red!20/0010,red!20/0100}
\node[draw,fill=\c,minimum height=1cm,minimum width = 2cm,xshift=\n*2cm,font=\ttfamily](N\n){\i} ;

\draw [decoration={brace,mirror,raise=5pt},decorate] (N1.south west) --  node[below=10pt]{$M_0$}(N1.south east);
\draw [decoration={brace,mirror,raise=5pt},decorate] (N1.south east) --  node[below=10pt]{$M_1$}(N4.south east);

\node [tape, draw,minimum size=1cm,tape bend top=none,
tape bend height=0.4cm,rotate=90] at (9.3,0) (t) {};
\node [tape, draw,minimum size=1cm,tape bend top=none,
tape bend height=0.4cm,rotate=270] at (11.3,0) (t) {};

\node at (10.3,0.5) {\dots};
\node at (10.3,-0.5) {\dots};

\foreach \c/\i [count=\m] in
{olive!20/0111,green!20/1111}
\node[draw,fill=\c,minimum height=1cm,minimum width = 2cm,xshift=10.6cm+\m*2cm](N\m){\i} ;
\draw [decoration={brace,mirror,raise=5pt},decorate] (N2.south west) --  node[below=10pt]{$M_d$}(N2.south east);

\end{tikzpicture}

\end{document}


-

Another option without using tikz/pgf:

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\newsavebox{\tempbox}
\newcommand{\cbox}[2]{%
\fcolorbox{black}{#1}{\texttt{#2\strut}}\kern-\fboxrule}% Coloured box
\begin{document}
$\savebox{\tempbox}{\cbox{white}{~~~~~~}} \underbrace{\cbox{red!30}{0000}}_{M_0}\! \underbrace{\cbox{green!20}{0001} \cbox{green!20}{0010} \cbox{green!20}{0100}}_{M_1}\! \rlap{\usebox{\tempbox}}% \makebox[\wd\tempbox][c]{% \fcolorbox{white}{white}{\!\strut\cdot\!}~% \fcolorbox{white}{white}{\!\strut\cdot\!}~% \fcolorbox{white}{white}{\!\strut\cdot\!}}\! \cbox{white}{0111}\! \underbrace{\cbox{blue!40}{1111}}_{M_d}$
\end{document}​


The macro \cbox{<color>}{<stuff>} typesets <stuff> in the appropriate (framed) \fcolorbox with colour <color>. Modifying the frame colour to white and overlaying it on the enlarged rectangle multiple times (slightly spaced) provides the faux dashed line style to "break" the sequence.

-
hehe... I was just about to post a similar TikZ-free solution. –  Gonzalo Medina Dec 12 '11 at 17:29
a bit shorter: \newcommand\cbox[2]{\fcolorbox{black}{#1}{\texttt{#2\strut}}\kern-\fboxrule} –  Herbert Dec 12 '11 at 17:33
@Herbert: Thanks - that's great! –  Werner Dec 12 '11 at 18:39
\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
\foreach \c/\i [count=\n] in
{blue!20/0001,red!20/0001,red!20/0010,red!20/0100,white/\dots,yellow!20/0100}
\node[draw,fill=\c,minimum height=1cm,minimum width = 2cm,xshift=\n*2cm](N\n){\i} ;

\draw [decoration={brace,mirror,raise=5pt},decorate] (N1.south west) --  node[below=10pt]{$M_0$}(N1.south east);
\draw [decoration={brace,mirror,raise=5pt},decorate] (N1.south east) --  node[below=10pt]{$M_1$}(N4.south east);

\end{tikzpicture}
\end{document}


Update : Gonzalo was faster than me ! My code below is not complete I stop it when I saw the good answer of Gonzalo. I put my idea because I find a great difficulty. In my idea the connecting boxes contained numbers. here the result :

\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,shapes}
\begin{document}
\begin{tikzpicture}
\node[tape, draw,fill=red!20, tape bend top=none,rotate=90,minimum width=1cm,minimum height=2cm] at (7.92, 0) {};
\node[tape, draw,fill=yellow!20, tape bend bottom=none,rotate=90,minimum width=1cm,minimum height=2cm] at (12, 0) {};
\foreach \c/\i/\b [count=\n] in
{blue!20/0001/1,red!20/0001/1,red!20/0010/1,/0100/0,//0,/0100/0}
{\ifnum \b=1
\node[draw,fill=\c,minimum height=1cm,minimum width = 2cm,xshift=\n*2cm](N\n){\i} ;
\else
\node[minimum height=1cm,minimum width = 2cm,xshift=\n*2cm](N\n){\i} ;
\fi }

\draw [decoration={brace,mirror,raise=5pt},decorate] (N1.south west) --  node[below=10pt]{$M_0$}(N1.south east);
\draw [decoration={brace,mirror,raise=5pt},decorate] (N1.south east) --  node[below=10pt]{$M_1$}(N4.south east);

\end{tikzpicture}
\end{document}


The difficulty is real ! because I can't rotate nodes with numbers inside.

-

You can create the wiggly lines manually using Bezier curves,

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\newcommand\WigglyStrength{2/3}
% Left box
\draw
(0,0)
-- ++(2,0)
.. controls +(\WigglyStrength,1/3) and +(-\WigglyStrength,-1/3) .. ++(0,1)
-- ++(-2,0)
;
% Right box
\draw
(6,0)
-- ++(-2,0)
.. controls +(\WigglyStrength,1/3) and +(-\WigglyStrength,-1/3) .. ++(0,1)
-- ++(2,0)
;
% Dashed lines
\draw [dashed]
(0,0) ++(2,0) -- ++(2,0)
(0,1) ++(2,0) -- ++(2,0)
;
\end{tikzpicture}
\end{document}

-

I often look for ways to create "byte array" or "square array" (or rather, "rectangle array") as vector graphics - and one way to do it in Tikz, is to use rectangle split shape. (It is also possible to do with the D symbol in the tikz-timing package, but I'll skip that here).

Below is the code using rectangle split that renders this:

\documentclass{article}
\usepackage{calc} %\widthof
\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}
\begin{tikzpicture}

\newlength\tmpl
\setlength{\tmpl}{\widthof{0000}} % \settowidth{\tmpl}{0000} gives 0.0
\typeout{\tmpl \the\tmpl} % \tmpl 20.00006pt
\tikzstyle{nn} = [inner sep=0pt,outer sep=0pt, text width=\tmpl, align=center]
\tikzstyle{db} = [decoration={brace,mirror,raise=5pt},decorate]

\coordinate[] (origin) at (0,0);

\node[
rectangle split,
rectangle split horizontal=true,
rectangle split empty part width=\tmpl,
rectangle split parts=7,
%rectangle split use custom fill,%=true, % documented, but nowork tikz 1.5.1
rectangle split part fill={blue!20,red!20,red!20,red!20,none,olive!20,green!20},
line width=1pt,
draw,
] (barr1) [above=5pt of origin]
{%
\nodepart[nn,fill=red!50]{one}0001% fill is ignored here!
\nodepart[nn]{two}0001%
\nodepart[nn]{three}0010%
\nodepart[nn]{four}0100%
\nodepart[nn]{five}...%
\nodepart[nn]{six}0100%
\nodepart[nn]{seven}1111%
};

\draw [db] (barr1.south west) -- node[below=10pt]{$M_0$} (barr1.one split south);
\draw [db] (barr1.one split south) -- node[below=10pt]{$M_1$} (barr1.four split south);
\draw [db] (barr1.six split south) -- node[below=10pt]{$M_d$} (barr1.south east);

\end{tikzpicture}
\end{document}

-