The TikZ and PGF Packages
Manual for version 3.1.9a
64 Petri-Net Drawing Library
TikZ Library petri ¶
\usetikzlibrary[petri] % ConTeXt
This packages provides shapes and styles for drawing Petri nets.
The package defines a style for drawing places of Petri nets.
/tikz/place(style, no value) ¶
/tikz/every place(style, no value) ¶
This style indicates that a node is a place of a Petri net. Usually, the text of the node should be empty since places do not contain any text. You should use the label option to add text outside the node like its name or its capacity. You should use the tokens options, explained in Section 64.3, to add tokens inside the place.
This style is evoked by the style place. To change the appearance of places, you can change this style.
Transitions are also nodes. They should be drawn using the following style:
/tikz/transition(style, no value) ¶
/tikz/every transition(style, no value) ¶
/tikz/pre(style, no value) ¶
/tikz/post(style, no value) ¶
/tikz/pre and post(style, no value) ¶
This style indicates that a node is a transition. As for places, the text of a transition should be empty and the label option should be used for adding labels.
To connect a transition to places, you can use the edge command as in the following example:
This style is evoked by the style transition.
This style can be used with paths leading from a transition to a place to indicate that the place is in the pre-set of the transition. By default, this style is <-,shorten <=1pt, but feel free to redefine it.
This style is also used with paths leading from a transition to a place, but this time the place is in the post-set of the transition. Again, feel free to redefine it.
This style is to be used to indicate that a place is both in the pre- and post-set of a transition.
Interestingly, the most complicated aspect of drawing Petri nets in TikZ turns out to be the placement of tokens.
Let us start with a single token. They are also nodes and there is a simple style for typesetting them.
/tikz/token(style, no value) ¶
/tikz/every token(style, no value) ¶
This style indicates that a node is a token. By default, this causes the node to be a small black circle. Unlike places and transitions, it does make sense to provide text for the token node. Such text will be typeset in a tiny font and in white on black (naturally, you can easily change this by setting the style every token).
Change this style to change the appearance of tokens.
In the above example, it is bothersome that we need an extra command for the token node. Worse, when we have two tokens on a node, it is difficult to place both nodes inside the node without overlap.
The Petri library offers a solution to this problem: The children are tokens style.
/tikz/children are tokens(style, no value) ¶
/tikz/token distance=⟨distance⟩(no default) ¶
The idea behind this style is to use trees mechanism for placing tokens. Every token lying on a place is treated as a child of the node. Normally this would have the effect that the tokens are placed below the place and they would be connected to the place by an edge. The children are tokens style, however, redefines the growth function of trees such that it places the children next to each other inside (or, rather, on top) of the place node. Additionally, the edge from the parent node is not drawn.
In detail, what happens is the following: Tree growth functions tell TikZ where it should place the children of nodes. These functions get passed the number of children that a node has an the number of the child that should be placed. The special tree growth function for tokens has a special mapping for each possible number of children up to nine children. This mapping decides for each child where it should be placed on top of the place. For example, a single child is placed directly on top of the place. Two children are placed next to each other, separated by the token distance. Three children are placed in a triangle whose side lengths are token distance; and so on up to nine tokens. If you wish to place more than nice tokens on a place, you will have to write your own placement code.
This specifies the distance between the centers of the tokens in the arrangements of the option children are tokens.
The children are tokens option gives you a lot of flexibility, but it is a bit cumbersome to use. For this reason there are some options that help in standard situations. They all use children are tokens internally, so any change to, say, the every token style will affect how these options depict tokens.
/tikz/tokens=⟨number⟩(no default) ¶
This option is given to a place node, not to a token node. The effect of this option is to add ⟨number⟩ many child nodes to the place, each having the style token. Thus, the following two pieces of codes have the same effect:
It is legal to say tokens=0, no tokens are drawn in this case. This option does not handle ten or more tokens correctly. If you need this many tokens, you will have to program your own code.
/tikz/colored tokens=⟨color list⟩(no default) ¶
This option, which must also be given when a place node is being created, gets a list of colors as parameter. It will then add as many tokens to the place as there are colors in this list, each filled correspondingly.
/tikz/structured tokens=⟨token texts⟩(no default) ¶
This option, which must again be passed to a place, gets a list of texts for tokens. For each text, a new token will be added to the place.
If you use lots of structured tokens, consider redefining the every token style so that the tokens are larger.
Here is the same net once more, but with these styles changes: