Tikz学习笔记(二)

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
\documentclass[tikz,border=4pt]{standalone}  % 图片的边界是4pt
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,snakes,automata,backgrounds,petri,fit}
\begin{document}
\begin{tikzpicture}[node distance=1.3cm,>=stealth',bend angle=45, auto]
% node distance表示节点之间的距离
%当环境带有 on grid 选项时,该值指的是 node 的中心之间的距离;当环境无 on grid 选项时,该值指的是 node 边界之间的距离,初始值为 1cm
% >=stealth’指示箭头样式
% bend angle=45 指示路径的转弯角度
% auto 指示路径标签与路径的相对位置,auto=*,其中*可以是 left,right,false

\tikzstyle{place}=[circle,thick,draw=blue!75,fill=blue!20,minimum size=6mm]
% minimum size是最小尺寸 ,draw是边框的颜色,fill是背景填充的颜色

\tikzstyle{red place}=[place,draw=red!75,fill=red!20]
%在place的基础上重新定义,draw和fill取代了之前的定义

\tikzstyle{transition}=[rectangle,thick,draw=black!75,fill=black!20, minimum size=4mm]
%同上

\tikzstyle{every label}=[red]
%定义了所有标签的样式

%定义了箭头的形式

\begin{scope}
% First net
\node [place, tokens = 1] (w1) {};
%样式为place,tokens=1指示标记数目为 1,节点的名称为w1,花括号无内容,表示标签无文字(但不能省略花括号)

\node [place] (c1) [below of = w1] {};
%c1是节点的名字, below of = w1表示c1位置在w1的下面

\node [place] (s) [below of = c1,label = above:$s\le 3$] {};
% s在在c1的下面,标签位置above表示上面,内容为s<3,注意中间的冒号“:”

\node [place] (c2) [below of = s] {};
% c2在s的下面

\node [place, tokens=1] (w2) [below of=c2] {};

\node [transition] (e1) [left of=c1] {}
edge [post,bend left] (w1)
edge [post,bend right] (s)
edge [post] (c1);
% 从w2向3个节点连线,pre和post是线的样式
% 起点指向终点的线作为参考线,逆时针方向为正
% bend right和bend left代表线的弯曲模式
% bend right=45表示,起点处向左弯曲45度并且终点进入的方向是-45度


\node [transition] (e2) [left of=c2] {}
edge [pre,bend right] (w2)
edge [post,bend left] (s)
edge [post] (c2);



\node [transition] (l1) [right of=c1] {}
edge [pre] (c1)
edge [pre,bend left] (s)
edge [post,bend right] node[swap] {2} (w1);


\node [transition] (l2) [right of=c2] {}
edge [pre] (c2)
edge [pre,bend right] (s)
edge [post,bend left] node {2} (w2);
\end{scope}


%开启 scope 环境,选项 xshift=6cm 使得环境生成的图右移 6cm
\begin{scope}[xshift=6cm]

% Second net
\node [place,tokens=1](w1') {};

\node [place] (c1') [below of=w1'] {};

\node [red place] (s1') [below of=c1',xshift=-5mm,label=left:$s$] {};

\node [red place,tokens=3](s2') [below of=c1',xshift=5mm,label=right:$\bar s$] {};%tokens=3表示中间有3个点

\node [place] (c2') [below of=s1',xshift=5mm] {};

\node [place,tokens=1] (w2') [below of=c2'] {};

\node [transition] (e1') [left of=c1'] {}
edge [pre,bend left] (w1')
edge [post] (s1')
edge [pre] (s2')
edge [post] (c1');

\node [transition] (e2') [left of=c2'] {}
edge [pre,bend right] (w2')
edge [post] (s1')
edge [pre] (s2')
edge [post] (c2');

\node [transition] (l1') [right of=c1'] {}
edge [pre] (c1')
edge [pre] (s1')
edge [post] (s2')
edge [post,bend right] node[swap] {2} (w1');

\node [transition] (l2') [right of=c2'] {}
edge [pre] (c2')
edge [pre] (s1')
edge [post] (s2')
edge [post,bend left] node {2} (w2');
\end{scope}


%需要用到库backgrounds ,指示开启的 scope 环境所在的层属于主层(即 tikzpicture 环境所在层)之下的层
\begin{scope}[on background layer]
\node (r1) [fill=black!10, rounded corners, fit=(w1)(w2)(e1)(e2)(l1)(l2)] {};
\node (r2) [fill=green!10, rounded corners, fit=(w1')(w2')(e1')(e2')(l1')(l2')] {};
%定义了两个节点
%选项 fit 生成一个盒子(fit=(w1)(w2)(e1)(e2)(l1)(l2),等号右侧各项是坐标或 node, 各项之间不能加逗号),生成的盒子将等号右侧各项包围在内。各项与盒子边界的距离用选项 inner sep= 设置
\end{scope}

% draw命令可以跟上可选项 -> ,这样直线的右端就有一个箭头了。此外还有: ->> , ->| , -to , -latex , -stealth
% shorten >和shorten <分别表示的意思就是结尾和开头分别缩短的长度,例如shorten >= 1mm
% thick 线的宽度
% decoration线的形式,
% decorate打开线的装饰,decoration定义线的形式snakes,需要加snakes选项)
% node [above=1mm, midway, text width=3cm, align=center] 连线上方,中间位置,文本框宽,中心方式
\draw [shorten >= 1mm, -to, thick,decorate, decoration={snake, amplitude=.4mm, segment length=2mm, pre = moveto, pre length=1mm,post length=2mm}]
(r1) -- (r2) node [above=1mm, midway, text width=3cm, align=center] {replacement of the \textcolor{red}{capacity}
by \textcolor{red}{two places}};

\end{tikzpicture}

\end{document}

A Petri-Net

您的支持将鼓励我继续创作,非常感谢!