Debian Codegraph @

texmf-dist/doc/generic/pgf/pgfmanual-en-tutorial-nodes.tex

raw
1 % Copyright 2020 by Till Tantau
2 %
3 % This file may be distributed and/or modified
4 %
5 % 1. under the LaTeX Project Public License and/or
6 % 2. under the GNU Free Documentation License.
7 %
8 % See the file doc/generic/pgf/licenses/LICENSE for more details.
9
10
11 \section{Tutorial: A Petri-Net for Hagen}
12
13 In this second tutorial we explore the node mechanism of \tikzname\ and
14 \pgfname.
15
16 Hagen must give a talk tomorrow about his favorite formalism for distributed
17 systems: Petri nets! Hagen used to give his talks using a blackboard and
18 everyone seemed to be perfectly content with this. Unfortunately, his audience
19 has been spoiled recently with fancy projector-based presentations and there
20 seems to be a certain amount of peer pressure that his Petri nets should also
21 be drawn using a graphic program. One of the professors at his institute
22 recommends \tikzname\ for this and Hagen decides to give it a try.
23
24
25 \subsection{Problem Statement}
26
27 For his talk, Hagen wishes to create a graphic that demonstrates how a net with
28 place capacities can be simulated by a net without capacities. The graphic
29 should look like this, ideally:
30 %
31 \begin{quote}
32 \begin{tikzpicture}
33 [node distance=1.3cm,>={Stealth[round]},bend angle=45,auto,
34 place/.style={circle,thick,draw=blue!75,fill=blue!20,minimum size=6mm},
35 red place/.style={place,draw=red!75,fill=red!20},
36 transition/.style={rectangle,thick,draw=black!75,fill=black!20,minimum size=4mm},
37 every label/.style={red},on grid]
38
39 \begin{scope}
40 % First net
41 \node [place,tokens=1] (w1) {};
42 \node [place] (c1) [below=of w1] {};
43 \node [place] (s) [below=of c1,label=above:$s\le 3$] {};
44 \node [place] (c2) [below=of s] {};
45 \node [place,tokens=1] (w2) [below=of c2] {};
46
47 \node [transition] (e1) [left=of c1] {}
48 edge [pre,bend left] (w1)
49 edge [post,bend right] (s)
50 edge [post] (c1);
51
52 \node [transition] (e2) [left=of c2] {}
53 edge [pre,bend right] (w2)
54 edge [post,bend left] (s)
55 edge [post] (c2);
56
57 \node [transition] (l1) [right=of c1] {}
58 edge [pre] (c1)
59 edge [pre,bend left] (s)
60 edge [post,bend right] node[swap] {2} (w1);
61
62 \node [transition] (l2) [right=of c2] {}
63 edge [pre] (c2)
64 edge [pre,bend right] (s)
65 edge [post,bend left] node {2} (w2);
66 \end{scope}
67
68 \begin{scope}[xshift=6cm]
69 % Second net
70 \node [place,tokens=1]
71 (w1') {};
72 \node [place] (c1') [below=of w1'] {};
73 \node [red place] (s1') [below=of c1',xshift=-5mm,label=left:$s$] {};
74 \node [red place,tokens=3]
75 (s2') [below=of c1',xshift=5mm,label=right:$\bar s$] {};
76 \node [place] (c2') [below=of s1',xshift=5mm] {};
77 \node [place,tokens=1]
78 (w2') [below=of c2'] {};
79
80 \node [transition] (e1') [left=of c1'] {}
81 edge [pre,bend left] (w1')
82 edge [post] (s1')
83 edge [pre] (s2')
84 edge [post] (c1');
85
86 \node [transition] (e2') [left=of c2'] {}
87 edge [pre,bend right] (w2')
88 edge [post] (s1')
89 edge [pre] (s2')
90 edge [post] (c2');
91
92 \node [transition] (l1') [right=of c1'] {}
93 edge [pre] (c1')
94 edge [pre] (s1')
95 edge [post] (s2')
96 edge [post,bend right] node[swap] {2} (w1');
97
98 \node [transition] (l2') [right=of c2'] {}
99 edge [pre] (c2')
100 edge [pre] (s1')
101 edge [post] (s2')
102 edge [post,bend left] node {2} (w2');
103 \end{scope}
104
105 \begin{scope}[on background layer]
106 \node (r1) [fill=black!10,rounded corners,fit=(w1)(w2)(e1)(e2)(l1)(l2)] {};
107 \node (r2) [fill=black!10,rounded corners,fit=(w1')(w2')(e1')(e2')(l1')(l2')] {};
108 \end{scope}
109
110 \draw [shorten >=1mm,->,thick,decorate,decoration={snake,amplitude=.4mm,segment
111 length=2mm,pre=moveto,pre length=1mm,post length=2mm}]
112 (r1) -- (r2)
113 node [above=1mm,midway,text width=3cm,align=center]
114 {replacement of the \textcolor{red}{capacity} by \textcolor{red}{two places}};
115
116 \end{tikzpicture}
117 \end{quote}
118
119
120 \subsection{Setting up the Environment}
121
122 For the picture Hagen will need to load the \tikzname\ package as did Karl in
123 the previous tutorial. However, Hagen will also need to load some additional
124 \emph{library packages} that Karl did not need. These library packages contain
125 additional definitions like extra arrow tips that are typically not needed in a
126 picture and that need to be loaded explicitly.
127
128 Hagen will need to load several libraries: The |arrows.meta| library for the
129 special arrow tip used in the graphic, the |decorations.pathmorphing| library
130 for the ``snaking line'' in the middle, the |backgrounds| library for the two
131 rectangular areas that are behind the two main parts of the picture, the |fit|
132 library to easily compute the sizes of these rectangles, and the |positioning|
133 library for placing nodes relative to other nodes.
134
135
136 \subsubsection{Setting up the Environment in \LaTeX}
137
138 When using \LaTeX\ use:
139 %
140 \begin{codeexample}[code only]
141 \documentclass{article} % say
142
143 \usepackage{tikz}
144 \usetikzlibrary{arrows.meta,decorations.pathmorphing,backgrounds,positioning,fit,petri}
145
146 \begin{document}
147 \begin{tikzpicture}
148 \draw (0,0) -- (1,1);
149 \end{tikzpicture}
150 \end{document}
151 \end{codeexample}
152
153
154 \subsubsection{Setting up the Environment in Plain \TeX}
155
156 When using plain \TeX\ use:
157 %
158 \begin{codeexample}[code only]
159 %% Plain TeX file
160 \input tikz.tex
161 \usetikzlibrary{arrows.meta,decorations.pathmorphing,backgrounds,positioning,fit,petri}
162 \baselineskip=12pt
163 \hsize=6.3truein
164 \vsize=8.7truein
165 \tikzpicture
166 \draw (0,0) -- (1,1);
167 \endtikzpicture
168 \bye
169 \end{codeexample}
170
171
172 \subsubsection{Setting up the Environment in Con\TeX t}
173
174 When using Con\TeX t, use:
175 %
176 \begin{codeexample}[code only]
177 %% ConTeXt file
178 \usemodule[tikz]
179 \usetikzlibrary[arrows.meta,decorations.pathmorphing,backgrounds,positioning,fit,petri]
180
181 \starttext
182 \starttikzpicture
183 \draw (0,0) -- (1,1);
184 \stoptikzpicture
185 \stoptext
186 \end{codeexample}
187
188
189 \subsection{Introduction to Nodes}
190
191 In principle, we already know how to create the graphics that Hagen desires
192 (except perhaps for the snaked line, we will come to that): We start with big
193 light gray rectangle and then add lots of circles and small rectangle, plus
194 some arrows.
195
196 However, this approach has numerous disadvantages: First, it is hard to change
197 anything at a later stage. For example, if we decide to add more places to the
198 Petri nets (the circles are called places in Petri net theory), all of the
199 coordinates change and we need to recalculate everything. Second, it is hard to
200 read the code for the Petri net as it is just a long and complicated list of
201 coordinates and drawing commands -- the underlying structure of the Petri net
202 is lost.
203
204 Fortunately, \tikzname\ offers a powerful mechanism for avoiding the above
205 problems: nodes. We already came across nodes in the previous tutorial, where
206 we used them to add labels to Karl's graphic. In the present tutorial we will
207 see that nodes are much more powerful.
208
209 A node is a small part of a picture. When a node is created, you provide a
210 position where the node should be drawn and a \emph{shape}. A node of shape
211 |circle| will be drawn as a circle, a node of shape |rectangle| as a rectangle,
212 and so on. A node may also contain some text, which is why Karl used nodes to
213 show text. Finally, a node can get a \emph{name} for later reference.
214
215 In Hagen's picture we will use nodes for the places and for the transitions of
216 the Petri net (the places are the circles, the transitions are the rectangles).
217 Let us start with the upper half of the left Petri net. In this upper half we
218 have three places and two transitions. Instead of drawing three circles and two
219 rectangles, we use three nodes of shape |circle| and two nodes of shape
220 |rectangle|.
221 %
222 \begin{codeexample}[]
223 \begin{tikzpicture}
224 \path ( 0,2) node [shape=circle,draw] {}
225 ( 0,1) node [shape=circle,draw] {}
226 ( 0,0) node [shape=circle,draw] {}
227 ( 1,1) node [shape=rectangle,draw] {}
228 (-1,1) node [shape=rectangle,draw] {};
229 \end{tikzpicture}
230 \end{codeexample}
231
232 Hagen notes that this does not quite look like the final picture, but it seems
233 like a good first step.
234
235 Let us have a more detailed look at the code. The whole picture consists of a
236 single path. Ignoring the |node| operations, there is not much going on in this
237 path: It is just a sequence of coordinates with nothing ``happening'' between
238 them. Indeed, even if something were to happen like a line-to or a curve-to,
239 the |\path| command would not ``do'' anything with the resulting path. So, all
240 the magic must be in the |node| commands.
241
242 In the previous tutorial we learned that a |node| will add a piece of text at
243 the last coordinate. Thus, each of the five nodes is added at a different
244 position. In the above code, this text is empty (because of the empty |{}|).
245 So, why do we see anything at all? The answer is the |draw| option for the
246 |node| operation: It causes the ``shape around the text'' to be drawn.
247
248 So, the code |(0,2) node [shape=circle,draw] {}| means the following: ``In the
249 main path, add a move-to to the coordinate |(0,2)|. Then, temporarily suspend
250 the construction of the main path while the node is built. This node will be a
251 |circle| around an empty text. This circle is to be |draw|n, but not filled or
252 otherwise used. Once this whole node is constructed, it is saved until after
253 the main path is finished. Then, it is drawn.'' The following
254 |(0,1) node [shape=circle,draw] {}| then has the following effect: ``Continue
255 the main path with a move-to to |(0,1)|. Then construct a node at this position
256 also. This node is also shown after the main path is finished.'' And so on.
257
258
259 \subsection{Placing Nodes Using the At Syntax}
260
261 Hagen now understands how the |node| operation adds nodes to the path, but it
262 seems a bit silly to create a path using the |\path| operation, consisting of
263 numerous superfluous move-to operations, only to place nodes. He is pleased to
264 learn that there are ways to add nodes in a more sensible manner.
265
266 First, the |node| operation allows one to add |at (|\meta{coordinate}|)| in
267 order to directly specify where the node should be placed, sidestepping the
268 rule that nodes are placed on the last coordinate. Hagen can then write the
269 following:
270 %
271 \begin{codeexample}[]
272 \begin{tikzpicture}
273 \path node at ( 0,2) [shape=circle,draw] {}
274 node at ( 0,1) [shape=circle,draw] {}
275 node at ( 0,0) [shape=circle,draw] {}
276 node at ( 1,1) [shape=rectangle,draw] {}
277 node at (-1,1) [shape=rectangle,draw] {};
278 \end{tikzpicture}
279 \end{codeexample}
280
281 Now Hagen is still left with a single empty path, but at least the path no
282 longer contains strange move-to's. It turns out that this can be improved
283 further: The |\node| command is an abbreviation for |\path node|, which allows
284 Hagen to write:
285 %
286 \begin{codeexample}[]
287 \begin{tikzpicture}
288 \node at ( 0,2) [circle,draw] {};
289 \node at ( 0,1) [circle,draw] {};
290 \node at ( 0,0) [circle,draw] {};
291 \node at ( 1,1) [rectangle,draw] {};
292 \node at (-1,1) [rectangle,draw] {};
293 \end{tikzpicture}
294 \end{codeexample}
295
296 Hagen likes this syntax much better than the previous one. Note that Hagen has
297 also omitted the |shape=| since, like |color=|, \tikzname\ allows you to omit
298 the |shape=| if there is no confusion.
299
300
301 \subsection{Using Styles}
302
303 Feeling adventurous, Hagen tries to make the nodes look nicer. In the final
304 picture, the circles and rectangle should be filled with different colors,
305 resulting in the following code:
306 %
307 \begin{codeexample}[]
308 \begin{tikzpicture}[thick]
309 \node at ( 0,2) [circle,draw=blue!50,fill=blue!20] {};
310 \node at ( 0,1) [circle,draw=blue!50,fill=blue!20] {};
311 \node at ( 0,0) [circle,draw=blue!50,fill=blue!20] {};
312 \node at ( 1,1) [rectangle,draw=black!50,fill=black!20] {};
313 \node at (-1,1) [rectangle,draw=black!50,fill=black!20] {};
314 \end{tikzpicture}
315 \end{codeexample}
316
317 While this looks nicer in the picture, the code starts to get a bit ugly.
318 Ideally, we would like our code to transport the message ``there are three
319 places and two transitions'' and not so much which filling colors should be
320 used.
321
322 To solve this problem, Hagen uses styles. He defines a style for places and
323 another style for transitions:
324 %
325 \begin{codeexample}[]
326 \begin{tikzpicture}
327 [place/.style={circle,draw=blue!50,fill=blue!20,thick},
328 transition/.style={rectangle,draw=black!50,fill=black!20,thick}]
329 \node at ( 0,2) [place] {};
330 \node at ( 0,1) [place] {};
331 \node at ( 0,0) [place] {};
332 \node at ( 1,1) [transition] {};
333 \node at (-1,1) [transition] {};
334 \end{tikzpicture}
335 \end{codeexample}
336
337
338 \subsection{Node Size}
339
340 Before Hagen starts naming and connecting the nodes, let us first make sure
341 that the nodes get their final appearance. They are still too small. Indeed,
342 Hagen wonders why they have any size at all, after all, the text is empty. The
343 reason is that \tikzname\ automatically adds some space around the text. The
344 amount is set using the option |inner sep|. So, to increase the size of the
345 nodes, Hagen could write:
346 %
347 \begin{codeexample}[]
348 \begin{tikzpicture}
349 [inner sep=2mm,
350 place/.style={circle,draw=blue!50,fill=blue!20,thick},
351 transition/.style={rectangle,draw=black!50,fill=black!20,thick}]
352 \node at ( 0,2) [place] {};
353 \node at ( 0,1) [place] {};
354 \node at ( 0,0) [place] {};
355 \node at ( 1,1) [transition] {};
356 \node at (-1,1) [transition] {};
357 \end{tikzpicture}
358 \end{codeexample}
359
360 However, this is not really the best way to achieve the desired effect. It is
361 much better to use the |minimum size| option instead. This option allows Hagen
362 to specify a minimum size that the node should have. If the node actually needs
363 to be bigger because of a longer text, it will be larger, but if the text is
364 empty, then the node will have |minimum size|. This option is also useful to
365 ensure that several nodes containing different amounts of text have the same
366 size. The options |minimum height| and |minimum width| allow you to specify the
367 minimum height and width independently.
368
369 So, what Hagen needs to do is to provide |minimum size| for the nodes. To be on
370 the safe side, he also sets |inner sep=0pt|. This ensures that the nodes will
371 really have size |minimum size| and not, for very small minimum sizes, the
372 minimal size necessary to encompass the automatically added space.
373 %
374 \begin{codeexample}[]
375 \begin{tikzpicture}
376 [place/.style={circle,draw=blue!50,fill=blue!20,thick,
377 inner sep=0pt,minimum size=6mm},
378 transition/.style={rectangle,draw=black!50,fill=black!20,thick,
379 inner sep=0pt,minimum size=4mm}]
380 \node at ( 0,2) [place] {};
381 \node at ( 0,1) [place] {};
382 \node at ( 0,0) [place] {};
383 \node at ( 1,1) [transition] {};
384 \node at (-1,1) [transition] {};
385 \end{tikzpicture}
386 \end{codeexample}
387
388
389 \subsection{Naming Nodes}
390
391 Hagen's next aim is to connect the nodes using arrows. This seems like a tricky
392 business since the arrows should not start in the middle of the nodes, but
393 somewhere on the border and Hagen would very much like to avoid computing these
394 positions by hand.
395
396 Fortunately, \pgfname\ will perform all the necessary calculations for him.
397 However, he first has to assign names to the nodes so that he can reference
398 them later on.
399
400 There are two ways to name a node. The first is to use the |name=| option. The
401 second method is to write the desired name in parentheses after the |node|
402 operation. Hagen thinks that this second method seems strange, but he will soon
403 change his opinion.
404 %
405 \begin{codeexample}[setup code,hidden]
406 \tikzset{
407 place/.style={circle,draw=blue!50,fill=blue!20,thick,
408 inner sep=0pt,minimum size=6mm},
409 transition/.style={rectangle,draw=black!50,fill=black!20,thick,
410 inner sep=0pt,minimum size=4mm}
411 }
412 \end{codeexample}
413 %
414 \begin{codeexample}[]
415 % ... set up styles
416 \begin{tikzpicture}
417 \node (waiting 1) at ( 0,2) [place] {};
418 \node (critical 1) at ( 0,1) [place] {};
419 \node (semaphore) at ( 0,0) [place] {};
420 \node (leave critical) at ( 1,1) [transition] {};
421 \node (enter critical) at (-1,1) [transition] {};
422 \end{tikzpicture}
423 \end{codeexample}
424
425 Hagen is pleased to note that the names help in understanding the code. Names
426 for nodes can be pretty arbitrary, but they should not contain commas, periods,
427 parentheses, colons, and some other special characters. However, they can
428 contain underscores and hyphens.
429
430 The syntax for the |node| operation is quite liberal with respect to the order
431 in which node names, the |at| specifier, and the options must come. Indeed, you
432 can even have multiple option blocks between the |node| and the text in curly
433 braces, they accumulate. You can rearrange them arbitrarily and perhaps the
434 following might be preferable:
435 %
436 \begin{codeexample}[]
437 \begin{tikzpicture}
438 \node[place] (waiting 1) at ( 0,2) {};
439 \node[place] (critical 1) at ( 0,1) {};
440 \node[place] (semaphore) at ( 0,0) {};
441 \node[transition] (leave critical) at ( 1,1) {};
442 \node[transition] (enter critical) at (-1,1) {};
443 \end{tikzpicture}
444 \end{codeexample}
445
446
447 \subsection{Placing Nodes Using Relative Placement}
448
449 Although Hagen still wishes to connect the nodes, he first wishes to address
450 another problem again: The placement of the nodes. Although he likes the |at|
451 syntax, in this particular case he would prefer placing the nodes ``relative to
452 each other''. So, Hagen would like to say that the |critical 1| node should be
453 below the |waiting 1| node, wherever the |waiting 1| node might be. There are
454 different ways of achieving this, but the nicest one in Hagen's case is the
455 |below| option:
456 %
457 \begin{codeexample}[preamble={\usetikzlibrary{positioning}}]
458 \begin{tikzpicture}
459 \node[place] (waiting) {};
460 \node[place] (critical) [below=of waiting] {};
461 \node[place] (semaphore) [below=of critical] {};
462 \node[transition] (leave critical) [right=of critical] {};
463 \node[transition] (enter critical) [left=of critical] {};
464 \end{tikzpicture}
465 \end{codeexample}
466
467 With the |positioning| library loaded, when an option like |below| is followed
468 by |of|, then the position of the node is shifted in such a manner that it is
469 placed at the distance |node distance| in the specified direction of the given
470 direction. The |node distance| is either the distance between the centers of
471 the nodes (when the |on grid| option is set to true) or the distance between
472 the borders (when the |on grid| option is set to false, which is the default).
473
474 Even though the above code has the same effect as the earlier code, Hagen can
475 pass it to his colleagues who will be able to just read and understand it,
476 perhaps without even having to see the picture.
477
478
479 \subsection{Adding Labels Next to Nodes}
480
481 Before we have a look at how Hagen can connect the nodes, let us add the
482 capacity ``$s \le 3$'' to the bottom node. For this, two approaches are
483 possible:
484 %
485 \begin{enumerate}
486 \item Hagen can just add a new node above the |north| anchor of the
487 |semaphore| node.
488 %
489 \begin{codeexample}[preamble={\usetikzlibrary{positioning}}]
490 \begin{tikzpicture}
491 \node[place] (waiting) {};
492 \node[place] (critical) [below=of waiting] {};
493 \node[place] (semaphore) [below=of critical] {};
494 \node[transition] (leave critical) [right=of critical] {};
495 \node[transition] (enter critical) [left=of critical] {};
496
497 \node [red,above] at (semaphore.north) {$s\le 3$};
498 \end{tikzpicture}
499 \end{codeexample}
500 %
501 This is a general approach that will ``always work''.
502
503 \item Hagen can use the special |label| option. This option is given to a
504 |node| and it causes \emph{another} node to be added next to the node
505 where the option is given. Here is the idea: When we construct the
506 |semaphore| node, we wish to indicate that we want another node with
507 the capacity above it. For this, we use the option
508 |label=above:$s\le 3$|. This option is interpreted as follows: We want
509 a node above the |semaphore| node and this node should read ``$s \le
510 3$''. Instead of |above| we could also use things like |below left|
511 before the colon or a number like |60|.
512 %
513 \begin{codeexample}[preamble={\usetikzlibrary{positioning}}]
514 \begin{tikzpicture}
515 \node[place] (waiting) {};
516 \node[place] (critical) [below=of waiting] {};
517 \node[place] (semaphore) [below=of critical,
518 label=above:$s\le3$] {};
519 \node[transition] (leave critical) [right=of critical] {};
520 \node[transition] (enter critical) [left=of critical] {};
521 \end{tikzpicture}
522 \end{codeexample}
523 %
524 It is also possible to give multiple |label| options, this causes
525 multiple labels to be drawn.
526 %
527 \begin{codeexample}[]
528 \tikz
529 \node [circle,draw,label=60:$60^\circ$,label=below:$-90^\circ$] {my circle};
530 \end{codeexample}
531 %
532 Hagen is not fully satisfied with the |label| option since the label
533 is not red. To achieve this, he has two options: First, he can
534 redefine the |every label| style. Second, he can add options to the
535 label's node. These options are given following the |label=|, so he
536 would write |label=[red]above:$s\le3$|. However, this does not quite
537 work since \TeX\ thinks that the |]| closes the whole option list of
538 the |semaphore| node. So, Hagen has to add braces and writes
539 |label={[red]above:$s\le3$}|. Since this looks a bit ugly, Hagen
540 decides to redefine the |every label| style.
541 %
542 \begin{codeexample}[preamble={\usetikzlibrary{positioning}}]
543 \begin{tikzpicture}[every label/.style={red}]
544 \node[place] (waiting) {};
545 \node[place] (critical) [below=of waiting] {};
546 \node[place] (semaphore) [below=of critical,
547 label=above:$s\le3$] {};
548 \node[transition] (leave critical) [right=of critical] {};
549 \node[transition] (enter critical) [left=of critical] {};
550 \end{tikzpicture}
551 \end{codeexample}
552 \end{enumerate}
553
554
555 \subsection{Connecting Nodes}
556
557 It is now high time to connect the nodes. Let us start with something simple,
558 namely with the straight line from |enter critical| to |critical|. We want this
559 line to start at the right side of |enter critical| and to end at the left side
560 of |critical|. For this, we can use the \emph{anchors} of the nodes. Every node
561 defines a whole bunch of anchors that lie on its border or inside it. For
562 example, the |center| anchor is at the center of the node, the |west| anchor is
563 on the left of the node, and so on. To access the coordinate of a node, we use
564 a coordinate that contains the node's name followed by a dot, followed by the
565 anchor's name:
566 %
567 \begin{codeexample}[preamble={\usetikzlibrary{positioning}}]
568 \begin{tikzpicture}
569 \node[place] (waiting) {};
570 \node[place] (critical) [below=of waiting] {};
571 \node[place] (semaphore) [below=of critical] {};
572 \node[transition] (leave critical) [right=of critical] {};
573 \node[transition] (enter critical) [left=of critical] {};
574 \draw [->] (enter critical.east) -- (critical.west);
575 \end{tikzpicture}
576 \end{codeexample}
577
578 Next, let us tackle the curve from |waiting| to |enter critical|. This can be
579 specified using curves and controls:
580 %
581 \begin{codeexample}[preamble={\usetikzlibrary{positioning}}]
582 \begin{tikzpicture}
583 \node[place] (waiting) {};
584 \node[place] (critical) [below=of waiting] {};
585 \node[place] (semaphore) [below=of critical] {};
586 \node[transition] (leave critical) [right=of critical] {};
587 \node[transition] (enter critical) [left=of critical] {};
588 \draw [->] (enter critical.east) -- (critical.west);
589 \draw [->] (waiting.west) .. controls +(left:5mm) and +(up:5mm)
590 .. (enter critical.north);
591 \end{tikzpicture}
592 \end{codeexample}
593
594 Hagen sees how he can now add all his edges, but the whole process seems a but
595 awkward and not very flexible. Again, the code seems to obscure the structure
596 of the graphic rather than showing it.
597
598 So, let us start improving the code for the edges. First, Hagen can leave out
599 the anchors:
600 %
601 \begin{codeexample}[preamble={\usetikzlibrary{positioning}}]
602 \begin{tikzpicture}
603 \node[place] (waiting) {};
604 \node[place] (critical) [below=of waiting] {};
605 \node[place] (semaphore) [below=of critical] {};
606 \node[transition] (leave critical) [right=of critical] {};
607 \node[transition] (enter critical) [left=of critical] {};
608 \draw [->] (enter critical) -- (critical);
609 \draw [->] (waiting) .. controls +(left:8mm) and +(up:8mm)
610 .. (enter critical);
611 \end{tikzpicture}
612 \end{codeexample}
613
614 Hagen is a bit surprised that this works. After all, how did \tikzname\ know
615 that the line from |enter critical| to |critical| should actually start on the
616 borders? Whenever \tikzname\ encounters a whole node name as a ``coordinate'',
617 it tries to ``be smart'' about the anchor that it should choose for this node.
618 Depending on what happens next, \tikzname\ will choose an anchor that lies on
619 the border of the node on a line to the next coordinate or control point. The
620 exact rules are a bit complex, but the chosen point will usually be correct --
621 and when it is not, Hagen can still specify the desired anchor by hand.
622
623 Hagen would now like to simplify the curve operation somehow. It turns out that
624 this can be accomplished using a special path operation: the |to| operation.
625 This operation takes many options (you can even define new ones yourself). One
626 pair of options is useful for Hagen: The pair |in| and |out|. These options
627 take angles at which a curve should leave or reach the start or target
628 coordinates. Without these options, a straight line is drawn:
629 %
630 \begin{codeexample}[preamble={\usetikzlibrary{positioning}}]
631 \begin{tikzpicture}
632 \node[place] (waiting) {};
633 \node[place] (critical) [below=of waiting] {};
634 \node[place] (semaphore) [below=of critical] {};
635 \node[transition] (leave critical) [right=of critical] {};
636 \node[transition] (enter critical) [left=of critical] {};
637 \draw [->] (enter critical) to (critical);
638 \draw [->] (waiting) to [out=180,in=90] (enter critical);
639 \end{tikzpicture}
640 \end{codeexample}
641
642 There is another option for the |to| operation, that is even better suited to
643 Hagen's problem: The |bend right| option. This option also takes an angle, but
644 this angle only specifies the angle by which the curve is bent to the right:
645 %
646 \begin{codeexample}[preamble={\usetikzlibrary{positioning}}]
647 \begin{tikzpicture}
648 \node[place] (waiting) {};
649 \node[place] (critical) [below=of waiting] {};
650 \node[place] (semaphore) [below=of critical] {};
651 \node[transition] (leave critical) [right=of critical] {};
652 \node[transition] (enter critical) [left=of critical] {};
653 \draw [->] (enter critical) to (critical);
654 \draw [->] (waiting) to [bend right=45] (enter critical);
655 \draw [->] (enter critical) to [bend right=45] (semaphore);
656 \end{tikzpicture}
657 \end{codeexample}
658
659 It is now time for Hagen to learn about yet another way of specifying edges:
660 Using the |edge| path operation. This operation is very similar to the |to|
661 operation, but there is one important difference: Like a node the edge
662 generated by the |edge| operation is not part of the main path, but is added
663 only later. This may not seem very important, but it has some nice
664 consequences. For example, every edge can have its own arrow tips and its own
665 color and so on and, still, all the edges can be given on the same path. This
666 allows Hagen to write the following:
667 %
668 \begin{codeexample}[preamble={\usetikzlibrary{positioning}}]
669 \begin{tikzpicture}
670 \node[place] (waiting) {};
671 \node[place] (critical) [below=of waiting] {};
672 \node[place] (semaphore) [below=of critical] {};
673 \node[transition] (leave critical) [right=of critical] {};
674 \node[transition] (enter critical) [left=of critical] {}
675 edge [->] (critical)
676 edge [<-,bend left=45] (waiting)
677 edge [->,bend right=45] (semaphore);
678 \end{tikzpicture}
679 \end{codeexample}
680
681 Each |edge| caused a new path to be constructed, consisting of a |to| between
682 the node |enter critical| and the node following the |edge| command.
683
684 The finishing touch is to introduce two styles |pre| and |post| and to use the
685 |bend angle=45| option to set the bend angle once and for all:
686 %
687 \begin{codeexample}[preamble={\usetikzlibrary{arrows.meta,positioning}}]
688 % Styles place and transition as before
689 \begin{tikzpicture}
690 [bend angle=45,
691 pre/.style={<-,shorten <=1pt,>={Stealth[round]},semithick},
692 post/.style={->,shorten >=1pt,>={Stealth[round]},semithick}]
693
694 \node[place] (waiting) {};
695 \node[place] (critical) [below=of waiting] {};
696 \node[place] (semaphore) [below=of critical] {};
697
698 \node[transition] (leave critical) [right=of critical] {}
699 edge [pre] (critical)
700 edge [post,bend right] (waiting)
701 edge [pre, bend left] (semaphore);
702 \node[transition] (enter critical) [left=of critical] {}
703 edge [post] (critical)
704 edge [pre, bend left] (waiting)
705 edge [post,bend right] (semaphore);
706 \end{tikzpicture}
707 \end{codeexample}
708
709
710 \subsection{Adding Labels Next to Lines}
711
712 The next thing that Hagen needs to add is the ``$2$'' at the arcs. For this
713 Hagen can use \tikzname's automatic node placement: By adding the option
714 |auto|, \tikzname\ will position nodes on curves and lines in such a way that
715 they are not on the curve but next to it. Adding |swap| will mirror the label
716 with respect to the line. Here is a general example:
717 %
718 % TODOsp: codeexamples: styles not needed here
719 \begin{codeexample}[]
720 \begin{tikzpicture}[auto,bend right]
721 \node (a) at (0:1) {$0^\circ$};
722 \node (b) at (120:1) {$120^\circ$};
723 \node (c) at (240:1) {$240^\circ$};
724
725 \draw (a) to node {1} node [swap] {1'} (b)
726 (b) to node {2} node [swap] {2'} (c)
727 (c) to node {3} node [swap] {3'} (a);
728 \end{tikzpicture}
729 \end{codeexample}
730
731 What is happening here? The nodes are given somehow inside the |to| operation!
732 When this is done, the node is placed on the middle of the curve or line
733 created by the |to| operation. The |auto| option then causes the node to be
734 moved in such a way that it does not lie on the curve, but next to it. In the
735 example we provide even two nodes on each |to| operation.
736
737 For Hagen that |auto| option is not really necessary since the two ``2'' labels
738 could also easily be placed ``by hand''. However, in a complicated plot with
739 numerous edges automatic placement can be a blessing.
740 %
741 \begin{codeexample}[
742 preamble={\usetikzlibrary{arrows.meta,positioning}},
743 pre={\tikzset{
744 pre/.style={<-,shorten <=1pt,>={Stealth[round]},semithick},
745 post/.style={->,shorten >=1pt,>={Stealth[round]},semithick},
746 }},
747 ]
748 % Styles as before
749 \begin{tikzpicture}[bend angle=45]
750 \node[place] (waiting) {};
751 \node[place] (critical) [below=of waiting] {};
752 \node[place] (semaphore) [below=of critical] {};
753
754 \node[transition] (leave critical) [right=of critical] {}
755 edge [pre] (critical)
756 edge [post,bend right] node[auto,swap] {2} (waiting)
757 edge [pre, bend left] (semaphore);
758 \node[transition] (enter critical) [left=of critical] {}
759 edge [post] (critical)
760 edge [pre, bend left] (waiting)
761 edge [post,bend right] (semaphore);
762 \end{tikzpicture}
763 \end{codeexample}
764 % TODOsp: codeexamples: styles and `positioning` are needed up to here
765
766
767 \subsection{Adding the Snaked Line and Multi-Line Text}
768
769 With the node mechanism Hagen can now easily create the two Petri nets. What he
770 is unsure of is how he can create the snaked line between the nets.
771
772 For this he can use a \emph{decoration}. To draw the snaked line, Hagen only
773 needs to set the two options |decoration=snake| and |decorate| on the path.
774 This causes all lines of the path to be replaced by snakes. It is also possible
775 to use snakes only in certain parts of a path, but Hagen will not need this.
776 %
777 \begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}]
778 \begin{tikzpicture}
779 \draw [->,decorate,decoration=snake] (0,0) -- (2,0);
780 \end{tikzpicture}
781 \end{codeexample}
782
783 Well, that does not look quite right, yet. The problem is that the snake
784 happens to end exactly at the position where the arrow begins. Fortunately,
785 there is an option that helps here. Also, the snake should be a bit smaller,
786 which can be influenced by even more options.
787 %
788 \begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}]
789 \begin{tikzpicture}
790 \draw [->,decorate,
791 decoration={snake,amplitude=.4mm,segment length=2mm,post length=1mm}]
792 (0,0) -- (3,0);
793 \end{tikzpicture}
794 \end{codeexample}
795
796 Now Hagen needs to add the text above the snake. This text is a bit challenging
797 since it is a multi-line text. Hagen has two options for this: First, he can
798 specify an |align=center| and then use the |\\| command to enforce the line
799 breaks at the desired positions.
800 %
801 \begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}]
802 \begin{tikzpicture}
803 \draw [->,decorate,
804 decoration={snake,amplitude=.4mm,segment length=2mm,post length=1mm}]
805 (0,0) -- (3,0)
806 node [above,align=center,midway]
807 {
808 replacement of\\
809 the \textcolor{red}{capacity}\\
810 by \textcolor{red}{two places}
811 };
812 \end{tikzpicture}
813 \end{codeexample}
814
815 Instead of specifying the line breaks ``by hand'', Hagen can also specify a
816 width for the text and let \TeX\ perform the line breaking for him:
817 %
818 \begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}]
819 \begin{tikzpicture}
820 \draw [->,decorate,
821 decoration={snake,amplitude=.4mm,segment length=2mm,post length=1mm}]
822 (0,0) -- (3,0)
823 node [above,text width=3cm,align=center,midway]
824 {
825 replacement of the \textcolor{red}{capacity} by
826 \textcolor{red}{two places}
827 };
828 \end{tikzpicture}
829 \end{codeexample}
830
831
832 \subsection{Using Layers: The Background Rectangles}
833
834 Hagen still needs to add the background rectangles. These are a bit tricky:
835 Hagen would like to draw the rectangles \emph{after} the Petri nets are
836 finished. The reason is that only then can he conveniently refer to the
837 coordinates that make up the corners of the rectangle. If Hagen draws the
838 rectangle first, then he needs to know the exact size of the Petri net -- which
839 he does not.
840
841 The solution is to use \emph{layers}. When the |backgrounds| library is loaded,
842 Hagen can put parts of his picture inside a scope with the
843 |on background layer| option. Then this part of the picture becomes part of the
844 layer that is given as an argument to this environment. When the
845 |{tikzpicture}| environment ends, the layers are put on top of each other,
846 starting with the background layer. This causes everything drawn on the
847 background layer to be behind the main text.
848
849 The next tricky question is, how big should the rectangle be? Naturally, Hagen
850 can compute the size ``by hand'' or using some clever observations concerning
851 the $x$- and $y$-coordinates of the nodes, but it would be nicer to just have
852 \tikzname\ compute a rectangle into which all the nodes ``fit''. For this, the
853 |fit| library can be used. It defines the |fit| options, which, when given to a
854 node, causes the node to be resized and shifted such that it exactly covers all
855 the nodes and coordinates given as parameters to the |fit| option.
856 %
857 % TODOsp: codeexamples: redo/add styles starting from here
858 \begin{codeexample}[
859 preamble={\usetikzlibrary{arrows.meta,backgrounds,fit,positioning}},
860 pre={\tikzset{
861 pre/.style={<-,shorten <=1pt,>={Stealth[round]},semithick},
862 post/.style={->,shorten >=1pt,>={Stealth[round]},semithick},
863 }},
864 ]
865 % Styles as before
866 \begin{tikzpicture}[bend angle=45]
867 \node[place] (waiting) {};
868 \node[place] (critical) [below=of waiting] {};
869 \node[place] (semaphore) [below=of critical] {};
870
871 \node[transition] (leave critical) [right=of critical] {}
872 edge [pre] (critical)
873 edge [post,bend right] node[auto,swap] {2} (waiting)
874 edge [pre, bend left] (semaphore);
875 \node[transition] (enter critical) [left=of critical] {}
876 edge [post] (critical)
877 edge [pre, bend left] (waiting)
878 edge [post,bend right] (semaphore);
879
880 \begin{scope}[on background layer]
881 \node [fill=black!30,fit=(waiting) (critical) (semaphore)
882 (leave critical) (enter critical)] {};
883 \end{scope}
884 \end{tikzpicture}
885 \end{codeexample}
886
887
888 \subsection{The Complete Code}
889
890 Hagen has now finally put everything together. Only then does he learn that
891 there is already a library for drawing Petri nets! It turns out that this
892 library mainly provides the same definitions as Hagen did. For example, it
893 defines a |place| style in a similar way as Hagen did. Adjusting the code so
894 that it uses the library shortens Hagen code a bit, as shown in the following.
895
896 First, Hagen needs less style definitions, but he still needs to specify the
897 colors of places and transitions.
898 %
899 \begin{codeexample}[code only]
900 \begin{tikzpicture}
901 [node distance=1.3cm,on grid,>={Stealth[round]},bend angle=45,auto,
902 every place/.style= {minimum size=6mm,thick,draw=blue!75,fill=blue!20},
903 every transition/.style={thick,draw=black!75,fill=black!20},
904 red place/.style= {place,draw=red!75,fill=red!20},
905 every label/.style= {red}]
906 \end{codeexample}
907
908 Now comes the code for the nets:
909 %
910 \ifpgfmanualexternalize\tikzexternaldisable\fi
911 \begin{codeexample}[
912 preamble={\usetikzlibrary{arrows.meta,petri,positioning}},
913 pre={\tikzset{
914 every place/.style={minimum size=6mm,thick,draw=blue!75,fill=blue!20},
915 every transition/.style={thick,draw=black!75,fill=black!20},
916 every label/.style={red},
917 every picture/.style={on grid,node distance=1.3cm,>={Stealth[round]},bend angle=45,auto},
918 }%
919 \begin{tikzpicture}},
920 post={\end{tikzpicture}},
921 ]
922 \node [place,tokens=1] (w1) {};
923 \node [place] (c1) [below=of w1] {};
924 \node [place] (s) [below=of c1,label=above:$s\le 3$] {};
925 \node [place] (c2) [below=of s] {};
926 \node [place,tokens=1] (w2) [below=of c2] {};
927
928 \node [transition] (e1) [left=of c1] {}
929 edge [pre,bend left] (w1)
930 edge [post,bend right] (s)
931 edge [post] (c1);
932 \node [transition] (e2) [left=of c2] {}
933 edge [pre,bend right] (w2)
934 edge [post,bend left] (s)
935 edge [post] (c2);
936 \node [transition] (l1) [right=of c1] {}
937 edge [pre] (c1)
938 edge [pre,bend left] (s)
939 edge [post,bend right] node[swap] {2} (w1);
940 \node [transition] (l2) [right=of c2] {}
941 edge [pre] (c2)
942 edge [pre,bend right] (s)
943 edge [post,bend left] node {2} (w2);
944 \end{codeexample}
945
946 \ifpgfmanualexternalize\tikzexternaldisable\fi
947 \begin{codeexample}[
948 preamble={\usetikzlibrary{arrows.meta,petri,positioning}},
949 pre={\tikzset{
950 every place/.style={minimum size=6mm,thick,draw=blue!75,fill=blue!20},
951 every transition/.style={thick,draw=black!75,fill=black!20},
952 red place/.style= {place,draw=red!75,fill=red!20},
953 every label/.style={red},
954 every picture/.style={on grid,node distance=1.3cm,>={Stealth[round]},bend angle=45,auto},
955 }%
956 \begin{tikzpicture}},
957 post={\end{tikzpicture}},
958 ]
959 \begin{scope}[xshift=6cm]
960 \node [place,tokens=1] (w1') {};
961 \node [place] (c1') [below=of w1'] {};
962 \node [red place] (s1') [below=of c1',xshift=-5mm]
963 [label=left:$s$] {};
964 \node [red place,tokens=3] (s2') [below=of c1',xshift=5mm]
965 [label=right:$\bar s$] {};
966 \node [place] (c2') [below=of s1',xshift=5mm] {};
967 \node [place,tokens=1] (w2') [below=of c2'] {};
968
969 \node [transition] (e1') [left=of c1'] {}
970 edge [pre,bend left] (w1')
971 edge [post] (s1')
972 edge [pre] (s2')
973 edge [post] (c1');
974 \node [transition] (e2') [left=of c2'] {}
975 edge [pre,bend right] (w2')
976 edge [post] (s1')
977 edge [pre] (s2')
978 edge [post] (c2');
979 \node [transition] (l1') [right=of c1'] {}
980 edge [pre] (c1')
981 edge [pre] (s1')
982 edge [post] (s2')
983 edge [post,bend right] node[swap] {2} (w1');
984 \node [transition] (l2') [right=of c2'] {}
985 edge [pre] (c2')
986 edge [pre] (s1')
987 edge [post] (s2')
988 edge [post,bend left] node {2} (w2');
989 \end{scope}
990 \end{codeexample}
991
992 The code for the background and the snake is the following:
993 %
994 \begin{codeexample}[code only]
995 \begin{scope}[on background layer]
996 \node (r1) [fill=black!10,rounded corners,fit=(w1)(w2)(e1)(e2)(l1)(l2)] {};
997 \node (r2) [fill=black!10,rounded corners,fit=(w1')(w2')(e1')(e2')(l1')(l2')] {};
998 \end{scope}
999
1000 \draw [shorten >=1mm,->,thick,decorate,
1001 decoration={snake,amplitude=.4mm,segment length=2mm,
1002 pre=moveto,pre length=1mm,post length=2mm}]
1003 (r1) -- (r2) node [above=1mm,midway,text width=3cm,align=center]
1004 {replacement of the \textcolor{red}{capacity} by \textcolor{red}{two places}};
1005 \end{tikzpicture}
1006 \end{codeexample}
1007
1008 % -----------------------------------------------------------------------------
1009 % TODOsp: codeexamples: This is needed because -- unlike I thought --
1010 % `setup code is remembered also outside this file. Thus the changed
1011 % style of `place` and `transition` are "remembered" in
1012 % <pgfmanual-en-library-petri.tex>
1013 \begin{codeexample}[setup code,hidden]
1014 % from <tikzlibrarypetri.code.tex>
1015 \tikzset{
1016 place/.style={circle,draw,inner sep=0pt,minimum size=5ex,every place},
1017 transition/.style={rectangle,draw,inner sep=0pt,minimum size=4mm,every transition},
1018 }
1019 \end{codeexample}
1020 % -----------------------------------------------------------------------------