7

How do I add a surrounding rectangle on top of the other nodes and edges in a tikz diagram? When I use the command \draw (0,0) rectangle (10,40) all the other components start below this point. What I would like is something in this diagram

enter image description here

My tikz code is the following:

\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}

\tikzstyle{vertex} = [fill,shape=circle,node distance=80pt]
\tikzstyle{edge} = [fill,opacity=.2,fill opacity=.5,line cap=round, line join=round, line width=50pt]
\tikzstyle{elabel} =  [fill,shape=circle,node distance=30pt, opacity = .2]

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{document}

\begin{tikzpicture}



\begin{pgfonlayer}{background}



\node[vertex,label={[font = \Large]above:\(C_{11}, \$30\)}, label={[font = \Large]below:\(F_{1}, F_{2}\)}] (v1) {};

\node[vertex,right of=v1,label={[font = \Large]above:\(C_{21}\)}, label={[font = \Large]below:\(F_{1}, F_{2}\)}] (v2) {};

\node[vertex,right of=v2,label={[font = \Large]above:\(C_{31}\)}, label={[font = \Large]below:\(F_{6}\)}] (v3) {};



\node[vertex,below of=v1,label={[font = \Large]above:\(C_{12},\$40\)}, label={[font = \Large]below:\(F_{1}, F_{2}\)}] (v4) {};

\node[vertex,right of=v4,label={[font = \Large]above:\(C_{22},\$20\)}, label={[font = \Large]below:\(F_{1}, F_{2}\)}] (v5) {};

\node[vertex,right of=v5,label={[font = \Large]above:\(C_{32},\$20\)}, label={[font = \Large]below:\(F_{6}\)}] (v6) {};



\node[vertex,below of=v4,label={[font = \Large]above:\(C_{31},\$45\)}, label={[font = \Large]below:\(F_{1}\)}] (v7) {};

\node[vertex,below of=v5,label={[font = \Large]above:\(C_{32},\$50\)}, label={[font = \Large]below:\(F_{4}, F_{5}\)}] (v8) {};

\node[vertex,below of=v6,label={[font = \Large]above:\(C_{33},\$40\)}, label={[font = \Large]below:\(F_{4}, F_{5}\)}] (v9) {};



\node[vertex,below of=v7,label={[font = \Large]above:\(C_{41}, \$30\)}, label={[font = \Large]below:\(F_{1}\)}] (v10) {};

\node[vertex,below of=v8,label={[font = \Large]above:\(C_{42}, \$70\)}, label={[font = \Large]below:\(F_{4}, F_{5}\)}] (v11) {};



\draw[edge,color=yellow,line width=65pt] (v2) -- (v3);

\draw[edge,color=red,line width=65pt] (v1) -- (v5);


\draw[edge,color=blue,line width=65pt] (v10) -- (v8) -- (v6);


\draw[edge,color=green, line width=65pt] (v10) -- (v11) -- (v6);


\end{pgfonlayer}

\node[elabel,color=yellow,label=right:\(\$25\)]  (e1) at (7,0) {};
\node[elabel,below of=e1,color=red,label=right:\(\$30\)]  (e2) {};
\node[elabel,below of=e2,color=blue,label=right:\(\$70\)]  (e3) {};
\node[elabel,below of=e3,color=green,label=right:\(\$90\)]  (e4) {};

%Task nodes

\node[above of=v1,label={[font = \Large]above:\(Task \ T_1\)}] (t1) {};

\node[above of=v2,label={[font = \Large]above:\(Task \ T_2\)}] (t2) {};

\node[above of=v3,label={[font = \Large]above:\(Task \  T_3\)}] (t3) {};

\end{tikzpicture}
\end{document}
7

If you don't specify the coordinates, or relative position, of a node, it's placed at (0,0). That means the C_11-node is at (0,0). All the other nodes are to the right and/or below this, so naturally a \draw (0,0) rectangle (10,40); won't fit around the first column, as the positive direction of the y-axis is upward. Also, since the default unit vectors are 1cm long, you'd get a rectangle that is 10cm by 40cm, which is far too large.

Of course using some other coordinates for the rectangle path would work, e.g.

\draw (-1,1.2) rectangle (1,-9.8);

but you could also use the fit library. Add \usetikzlibrary{fit} to the preamble, and

\node [draw, fit=(v1)(v10), inner sep=30pt] {};

in the diagram. That will make a node that fits around the v1 and v10 node. The inner sep is a little more than half the line width used in the edge style.

Unrelated comments: \tikzstyle{foo}=[..] is generally considered deprecated in favour of \tikzset{foo/.style={...}}. And in the top labels, don't make Task a part of the mathematical expression. Write e.g. Task \( T_1 \) instead.

enter image description here

\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{fit}
\tikzset{
  vertex/.style={
     fill,
     shape=circle,
     node distance=80pt},
  edge/.style={
     fill,
     opacity=.2,
     fill opacity=.5,
     line cap=round,
     line join=round,
     line width=50pt},
  elabel/.style={
     fill,
     shape=circle,
     node distance=30pt,
     opacity = .2}
}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{document}

\begin{tikzpicture}
\begin{pgfonlayer}{background}
\node[vertex,label={[font = \Large]above:\(C_{11}, \$30\)}, label={[font = \Large]below:\(F_{1}, F_{2}\)}] (v1) {};

\node[vertex,right of=v1,label={[font = \Large]above:\(C_{21}\)}, label={[font = \Large]below:\(F_{1}, F_{2}\)}] (v2) {};

\node[vertex,right of=v2,label={[font = \Large]above:\(C_{31}\)}, label={[font = \Large]below:\(F_{6}\)}] (v3) {};


\node[vertex,below of=v1,label={[font = \Large]above:\(C_{12},\$40\)}, label={[font = \Large]below:\(F_{1}, F_{2}\)}] (v4) {};

\node[vertex,right of=v4,label={[font = \Large]above:\(C_{22},\$20\)}, label={[font = \Large]below:\(F_{1}, F_{2}\)}] (v5) {};

\node[vertex,right of=v5,label={[font = \Large]above:\(C_{32},\$20\)}, label={[font = \Large]below:\(F_{6}\)}] (v6) {};


\node[vertex,below of=v4,label={[font = \Large]above:\(C_{31},\$45\)}, label={[font = \Large]below:\(F_{1}\)}] (v7) {};

\node[vertex,below of=v5,label={[font = \Large]above:\(C_{32},\$50\)}, label={[font = \Large]below:\(F_{4}, F_{5}\)}] (v8) {};

\node[vertex,below of=v6,label={[font = \Large]above:\(C_{33},\$40\)}, label={[font = \Large]below:\(F_{4}, F_{5}\)}] (v9) {};



\node[vertex,below of=v7,label={[font = \Large]above:\(C_{41}, \$30\)}, label={[font = \Large]below:\(F_{1}\)}] (v10) {};

\node[vertex,below of=v8,label={[font = \Large]above:\(C_{42}, \$70\)}, label={[font = \Large]below:\(F_{4}, F_{5}\)}] (v11) {};


\draw[edge,color=yellow,line width=65pt] (v2) -- (v3);

\draw[edge,color=red,line width=65pt] (v1) -- (v5);


\draw[edge,color=blue,line width=65pt] (v10) -- (v8) -- (v6);


\draw[edge,color=green, line width=65pt] (v10) -- (v11) -- (v6);


\end{pgfonlayer}

\node[elabel,color=yellow,label=right:\(\$25\)]  (e1) at (7,0) {};
\node[elabel,below of=e1,color=red,label=right:\(\$30\)]  (e2) {};
\node[elabel,below of=e2,color=blue,label=right:\(\$70\)]  (e3) {};
\node[elabel,below of=e3,color=green,label=right:\(\$90\)]  (e4) {};

%Task nodes

\node[above of=v1,label={[font = \Large]above:Task \( T_1\)}] (t1) {};

\node[above of=v2,label={[font = \Large]above:Task \(T_2\)}] (t2) {};

\node[above of=v3,label={[font = \Large]above:Task \(T_3\)}] (t3) {};


%\draw (-1,1.2) rectangle (1,-9.8);

\node [draw, fit=(v1)(v10), inner sep=30pt] {};
\end{tikzpicture}
\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.