绘制周围的矩形

绘制周围的矩形

如何在 tikz 图的其他节点和边的顶部添加一个环绕矩形?当我使用该命令时,\draw (0,0) rectangle (10,40)所有其他组件都从该点下方开始。我想要的是此图中的某些内容

在此处输入图片描述

我的 tikz 代码如下:

\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}

答案1

如果您不指定节点的坐标或相对位置,则将其放置在 (0,0)。这意味着 C_11 节点位于 (0,0)。所有其他节点都位于此节点的右侧和/或下方,因此自然不会\draw (0,0) rectangle (10,40);适合第一列,因为 y 轴的正方向向上。此外,由于默认单位向量长 1 厘米,您将得到一个 10 厘米乘 40 厘米的矩形,这太大了。

当然,使用其他坐标rectangle也可以,例如

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

但你也可以使用fit库。添加\usetikzlibrary{fit}到序言中,然后

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

在图中。这将创建一个适合v1v10节点的节点。这比样式中使用的inner sep一半多一点。line widthedge

无关评论:\tikzstyle{foo}=[..]一般认为 已被弃用,取而代之的是\tikzset{foo/.style={...}}。并且在顶部标签中,不要将其Task作为数学表达式的一部分。改写Task \( T_1 \)为 eg。

在此处输入图片描述

\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}

相关内容