# Create a container in a block diagram with tikz package

I have problems with creating a container inside a flow chart via tikz package.

The first pictures shows the expected result and the second picture shows the current version I made.

How to adjust the container box height and how to place the text above the 'center' point? Moreover I got problems with the fill=gray command: How to fix the the layer problem, so the container is in the background?

Here is my current code:

\documentclass[12pt,a4paper]{article}

\usepackage{tikz}

\usetikzlibrary{shapes,arrows,fit}
\tikzstyle{block} = [draw, rectangle, text width=2cm, text centered, minimum height=1.2cm, node distance=3cm]
%\tikzstyle{container} = [draw, rectangle, inner sep=0.3cm, fill=gray]
\tikzstyle{container} = [draw, rectangle, inner sep=0.3cm]

\begin{document}

\begin{figure}[]
\centering

\begin{tikzpicture}

\node [block, name=text1] {Text1};
\node [block, right of=text1] (text2) {Text2};
\node [block, right of=text2] (text3) {Text3};
\node [block, right of=text3] (text4) {Text4};
\node [block, right of=text4] (text5) {Text5};
\node [container,fit=(text2) (text3) (text4)] (container) {};

\draw [->] (text1) -- (text2);
\draw [->] (text2) -- node {} (text3);
\draw [->] (text3) -- node {} (text4);
\draw [->] (text4) -- node {} (text5);

\end{tikzpicture}

\caption{Test.}
\end{figure}

\end{document}


Thank you!

• Welcome! Please edit your question to include the code you mention trying rather than relying on an external link plus our best guess as to how you tried to modify it to your needs. – cfr Nov 20 '16 at 23:13

I found a solution with backgrounds tikzlibrary.

\documentclass[12pt,a4paper]{article}

\usepackage{tikz}

\usetikzlibrary{shapes,arrows,fit,backgrounds}
\tikzstyle{block} = [draw, rectangle, text width=2cm, text centered, minimum height=1.2cm, node distance=3cm,fill=white]
\tikzstyle{container} = [draw, rectangle, inner sep=0.3cm, fill=gray,minimum height=3cm]
%\tikzstyle{container} = [draw, rectangle, inner sep=0.3cm]
\def\bottom#1#2{\hbox{\vbox to #1{\vfill\hbox{#2}}}}
\tikzset{
mybackground/.style={execute at end picture={
\begin{scope}[on background layer]
\node[] at (current bounding box.north){\bottom{1cm} #1};
\end{scope}
}},
}
\begin{document}

\begin{figure}[]
\centering

\begin{tikzpicture}[mybackground={Box1}]

\node [block, name=text1] {Text1};
\node [block, right of=text1] (text2) {\color{red} Text2};
\node [block, right of=text2] (text3) {\color{blue} Text3};
\node [block, right of=text3] (text4) {\color{orange} Text4};
\node [block, right of=text4] (text5) {Text5};
\begin{scope}[on background layer]
\node [container,fit=(text2) (text3) (text4)] (container) {};
\end{scope}
\draw [->] (text1) -- (text2);
\draw [->] (text2) -- node {} (text3);
\draw [->] (text3) -- node {} (text4);
\draw [->] (text4) -- node {} (text5);

\end{tikzpicture}

\caption{Test.}
\end{figure}

\end{document}


• Thank you! I was able to correct the vertical offset of the rectangle with the shift parameter and changing the height: ..., shift={(0cm,0.35cm)}]... – Benjamin1956 Nov 21 '16 at 9:16
• @Benjamin1956, yourwelcom. – ferahfeza Nov 21 '16 at 12:12
• @Benjamin1956 Note that \tikzstyle is deprecated. – cfr Nov 22 '16 at 2:41