答案1
这是第 3 章中示例的改编版
教程:Hagen 的 Petri 网在tikz
手册中。
让我们首先使用库将节点放在正确的位置positioning
:
\node [place] (x1) {X1};
\node [place] (x2) [right=of x1] {X2};
\node [place] (x3) [right=of x2] {X3};
\node [place] (y1) [above=of x1] {Y1};
\node [place] (y2) [right=of y1] {Y2};
\node [place] (y3) [right=of y2] {Y3};
\node [place] (z) [above= 1cm of y2] {Z};
我已经使用过
\node [<style>] (<label for tikz>) [<placement relative to other nodes>] {<text>}
由于节点具有相同的外观,因此节点的样式定义为
place/.style={%
circle,
minimum size = 1.3cm,
draw,
},
其中place
是样式的名称,circle
定义形状并minimum size = 1.3cm,
确保所有节点的形状都是相同的大小(否则大小取决于节点内部打印的文本)。
节点之间的距离由 设定node distance
,其中水平和垂直距离可以分别定义。Z
节点具有特定的节点距离,可覆盖默认设置。
然后我们用以下方式连接节点:
\draw (y1) -- (z);
\draw (y2) -- (z);
\draw (y3) -- (z);
\draw (y1) -- node [label] {W11} (x1);
\draw (y1) -- node [label] {W12} (x2);
\draw (y1) -- node [label] {W13} (x3);
\draw (y2) -- node [label] {W21} (x1);
\draw (y2) -- node [label] {W22} (x2);
\draw (y2) -- node [label] {W23} (x3);
\draw (y3) -- node [label] {W31} (x1);
\draw (y3) -- node [label] {W32} (x2);
\draw (y3) -- node [label] {W33} (x3);
我已经使用过
\draw (<label of start node>) -- node [<style of the label>] {<text of the label>} (<label of end node>);
边的标签是最棘手的,因为默认情况下它们会重叠。因此,为所有标签定义了一种样式:
label/.style = {%
fill = white,
pos = 0.8,
},
允许pos
将标签放置在与起点有一定距离的线上,由 1 的分数指定(0:起始节点,1:结束节点),并使fill = white
背景为白色,以提高可读性。
得到的 MWE 如下:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[%
thick,
node distance=2.5cm and 3cm,
place/.style = {%
circle,
minimum size = 1.3cm,
draw,
},
label/.style = {%
fill = white,
pos = 0.8,
},
font=\sffamily
]
\node [place] (x1) {X1};
\node [place] (x2) [right=of x1] {X2};
\node [place] (x3) [right=of x2] {X3};
\node [place] (y1) [above=of x1] {Y1};
\node [place] (y2) [right=of y1] {Y2};
\node [place] (y3) [right=of y2] {Y3};
\node [place] (z) [above= 1cm of y2] {Z};
\draw (y1) -- (z);
\draw (y2) -- (z);
\draw (y3) -- (z);
\draw (y1) -- node [label] {W11} (x1);
\draw (y1) -- node [label] {W12} (x2);
\draw (y1) -- node [label] {W13} (x3);
\draw (y2) -- node [label] {W21} (x1);
\draw (y2) -- node [label] {W22} (x2);
\draw (y2) -- node [label] {W23} (x3);
\draw (y3) -- node [label] {W31} (x1);
\draw (y3) -- node [label] {W32} (x2);
\draw (y3) -- node [label] {W33} (x3);
\end{tikzpicture}
\end{document}