这是TikZ官方手册的摘录,我把重要的部分和代码的解释摘录下来了,还稍微加入了一点个人的理解以及代码的修饰。
请配合官方手册()食用🍔,此文可做备忘录📕
本笔记包括手册中的:
本文主要学习用TikZ画下面这幅图:
In this second tutorial we explore the node mechanism of TikZ and pgf.
For his talk, Hagen wishes to create a graphic that demonstrates how a net with place capacities can be simulated by a net without capacities. The graphic should look like this, ideally:
In LaTeX, we use:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (1,1);
\end{tikzpicture}
\end{document}
In principle, we already know how to create the graphics that Hagen desires:
We start with big light gray rectangle and then add lots of circles and small rectangle, plus some arrows.
However, this approach has numerous disadvantages:
First, it is hard to change anything at a later stage.
Second, it is hard to read the code for the Petri net as it is just a long and complicated list of coordinates and drawing commands – the underlying structure of the Petri net is lost.
Fortunately, TikZ offers a powerful mechanism for avoiding the above problems: nodes
.
A node is a small part of a picture. When a node is created, you provide a position where the node should be drawn and a shape.
A node can:
Draw a shape
Contain some text
Get a name for later reference
In Hagen’s picture we will use nodes for the places (circles) and for the transitions (rectangles) of the Petri net. Let us start with the upper half of the left Petri net.
Instead of drawing three circles and two rectangles, we use three nodes of shape circle and two nodes of shape rectangle:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}
\begin{tikzpicture}
\path (0,2) node [shape=circle, draw]{}
(0,1) node [shape=circle, draw]{}
(0,0) node [shape=circle, draw]{}
(1,1) node [shape=rectangle, draw]{}
(-1,1) node [shape=rectangle, draw]{};
\end{tikzpicture}
\end{document}
This does not quite look like the final picture, but it seems like a good first step. Ignoring the node operations, there is not much going on in this path: It is just a sequence of coordinates with nothing “happening” between them.
There are ways to add nodes in a more sensible manner.
First, the node operation allows one to add at <coordinate> in order to directly specify where the node should be placed, sidestepping the rule that nodes are placed on the last coordinate. Hagen can then write the following:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}
\begin{tikzpicture}
\path node at (0,2) [shape=circle, draw]{}
node at (0,1) [shape=circle, draw]{}
node at (0,0) [shape=circle, draw]{}
node at (1,1) [shape=rectangle, draw]{}
node at (-1,1) [shape=rectangle, draw]{};
\end{tikzpicture}
\end{document}
The result will be the same as 3.1.
It turns out that this can be improved further: The \node
command is an abbreviation for \path node
, which allows Hagen to write:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}
\begin{tikzpicture}
\node at (0,2) [shape=circle, draw]{}
\node at (0,1) [shape=circle, draw]{}
\node at (0,0) [shape=circle, draw]{}
\node at (1,1) [shape=rectangle, draw]{}
\node at (-1,1) [shape=rectangle, draw]{};
\end{tikzpicture}
\end{document}
Note that it is sometimes good to omitted the shape=
since, like color=
, TikZ allows you to omit the shape=
if there is no confusion.
Again, it's the same with3.1.
Feeling adventurous, Hagen tries to make the nodes look nicer:
\begin{document}
\begin{tikzpicture}
\node at (0,2) [circle,draw=blue!50,fill=blue!20] {};
\node at (0,1) [circle,draw=blue!50,fill=blue!20] {};
\node at (0,0) [circle,draw=blue!50,fill=blue!20] {};
\node at (1,1) [rectangle,draw=black!50,fill=black!20] {};
\node at (-1,1) [rectangle,draw=black!50,fill=black!20] {};
\end{tikzpicture}
\end{document}
While this looks nicer in the picture, the code starts to get a bit ugly.
Ideally, we would like our code to transport the message “there are three places and two transitions” and not so much which filling colors should be used.
To solve this problem, Hagen uses styles. He defines a style for places and another style for transitions:
\begin{document}
\begin{tikzpicture}
[place/.style={circle,draw=blue!50,fill=blue!20,thick}, transition/.style={rectangle,draw=black!50,fill=black!20,thick}]
\node at (0,2) [place] {};
\node at (0,1) [place] {};
\node at (0,0) [place] {};
\node at (1,1) [transition] {};
\node at (-1,1) [transition] {};
\end{tikzpicture}
\end{document}
Before Hagen starts naming and connecting the nodes, let us first make sure that the nodes get their final appearance. They are still too small.
Indeed, Hagen wonders why they have any size at all, after all, the text is empty. The reason is that TikZ automatically adds some space around the text. The amount is set using the option inner sep
.
So, to increase the size of the nodes, Hagen could write:
\begin{document}
\begin{tikzpicture}
[inner sep=2mm,
place/.style={circle,draw=blue!50,fill=blue!20,thick},
transition/.style={rectangle,draw=black!50,fill=black!20,thick}]
\node at (0,2) [place] {};
\node at (0,1) [place] {};
\node at (0,0) [place] {};
\node at (1,1) [transition] {};
\node at (-1,1) [transition] {};
\end{tikzpicture}
\end{document}
Hagen’s next aim is to connect the nodes using arrows.
This seems like a tricky business since the arrows should not start in the middle of the nodes, but somewhere on the border and Hagen would very much like to avoid computing these positions by hand.
Fortunately, pgf will perform all the necessary calculations for him. However, he first has to assign names to the nodes so that he can reference them later on.
There are two ways to name a node:
The first is to use the name= option
The second method is to write the desired name in parentheses after the node operation
See:
\begin{document}
\begin{tikzpicture}
[inner sep=2mm,
place/.style={circle,draw=blue!50,fill=blue!20,thick},
transition/.style={rectangle,draw=black!50,fill=black!20,thick}]
\node (waiting 1) at (0,2) [place] {};
\node (critical 1) at (0,1) [place] {};
\node (semaphore) at (0,0) [place] {};
\node (leave critical) at (1,1) [transition] {};
\node (enter critical) at (-1,1) [transition] {};
\end{tikzpicture}
\end{document}
Names for nodes can be pretty arbitrary, but they should not contain commas, periods, parentheses, colons, and some other special characters.
You can rearrange them arbitrarily and perhaps the following might be preferable:
Although Hagen still wishes to connect the nodes, he first wishes to address another problem again: The placement of the nodes.
Although he likes the at syntax, in this particular case he would prefer placing the nodes “relative to each other.” So, Hagen would like to say that the critical 1
node should be below the waiting 1
node, wherever the waiting 1 node might be.
There are different ways of achieving this, but the nicest one in Hagen’s case is the below option:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}
\begin{tikzpicture}
[inner sep=2mm,
place/.style={circle,draw=blue!50,fill=blue!20,thick},
transition/.style={rectangle,draw=black!50,fill=black!20,thick}]
\node[place] (waiting) {};
\node[place] (critical) [below=of waiting] {};
\node[place] (semaphore) [below=of critical] {};
\node[transition] (leave critical) [right=of critical] {};
\node[transition] (enter critical) [left=of critical] {};
\end{tikzpicture}
\end{document}
Before we have a look at how Hagen can connect the nodes, let us add the capacity “” to the bottom node. For this, two approaches are possible:
- Hagen can just add a new node above the north anchor of the semaphore node.
```LaTeX
\begin{document}
\begin{tikzpicture}
[inner sep=2mm,
place/.style={circle,draw=blue!50,fill=blue!20,thick},
transition/.style={rectangle,draw=black!50,fill=black!20,thick}]
\node[place] (waiting) {};
\node[place] (critical) [below=of waiting] {};
\node[place] (semaphore) [below=of critical] {};
\node[transition] (leave critical) [right=of critical] {};
\node[transition] (enter critical) [left=of critical] {};
\node[red, above] at (semaphore.north) {};
\end{tikzpicture}
\end{document}
This is a general approach that will “**always work**”
![](https://upload-images.jianshu.io/upload_images/21892437-598dfe0b0026fb0b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* Hagen can use the special label option:
```LaTeX
\begin{tikzpicture}
[inner sep=2mm,
place/.style={circle,draw=blue!50,fill=blue!20,thick},
transition/.style={rectangle,draw=black!50,fill=black!20,thick}]
\node[place] (waiting) {};
\node[place] (critical) [below=of waiting] {};
\node[place] (semaphore) [below=of critical, label=above: $s \le 3$] {};
\node[transition] (leave critical) [right=of critical] {};
\node[transition] (enter critical) [left=of critical] {};
\end{tikzpicture}
- It is also possible to give multiple label options, this causes multiple labels to be drawn.
It is now high time to connect the nodes.
Let us start with something simple, namely with the straight line from enter critical to critical. We want this line to start at the right side of enter critical and to end at the left side of critical. For this, we can use the anchors
of the nodes.
Every node defines a whole bunch of anchors that lie on its border or inside it. For example, the center anchor is at the center of the node, the west anchor is on the left of the node, and so on.
To access the coordinate of a node, we use a coordinate that contains the node’s name followed by a dot, followed by the anchor’s name:
\draw [->] (critical.west) -- (enter critical.east);
Next, let us tackle the curve from waiting to enter critical. This can be specified using curves and controls:
\begin{tikzpicture}
[inner sep=2mm,
place/.style={circle,draw=blue!50,fill=blue!20,thick},
transition/.style={rectangle,draw=black!50,fill=black!20,thick}]
\node[place] (waiting) {};
\node[place] (critical) [below=of waiting] {};
\node[place] (semaphore) [below=of critical] {};
\node[transition] (leave critical) [right=of critical] {};
\node[transition] (enter critical) [left=of critical] {};
\node [red, above] at (semaphore.north) {$s \le 3$};
\draw [->] (enter critical.east) -- (critical);
\draw[->] (waiting.west) .. controls +(left:5mm) and +(up:5mm) .. (enter critical.north);
\end{tikzpicture}
We'll have:
In some way, you can leave out the anchor
settings and leave them to TikZ:
Whenever TikZ encounters a whole node name as a “coordinate,” it tries to “be smart” about the anchor that it should choose for this node. Depending on what happens next, TikZ will choose an anchor that lies on the border of the node on a line to the next coordinate or control point.
The exact rules are a bit complex, but the chosen point will usually be correct – and when it is not, Hagen can still specify the desired anchor by hand.
Hagen would now like to simplify the curve operation somehow. It turns out that this can be accomplished using a special path operation: the to
operation.
\draw[->] (waiting) to[out=180, in=90] (enter critical.north);
There is another option for the to operation, that is even better suited to Hagen’s problem:
\draw[->] (enter critical) to [bend right=45] (semaphore);
This option also takes an angle, but this angle only specifies the angle by which the curve is bent to the right, see:
\begin{tikzpicture}
[inner sep=2mm,
place/.style={circle,draw=blue!50,fill=blue!20,thick},
transition/.style={rectangle,draw=black!50,fill=black!20,thick}]
\node[place] (waiting) {};
\node[place] (critical) [below=of waiting] {};
\node[place] (semaphore) [below=of critical] {};
\node[transition] (leave critical) [right=of critical] {};
\node[transition] (enter critical) [left=of critical] {};
\node [red, above] at (semaphore.north) {$s \le 3$};
\begin{scope}[>=stealth' ,semithick]
\draw [->] (enter critical.east) -- (critical);
\draw[->] (waiting) to[out=180, in=90] (enter critical.north);
\draw[->] (enter critical) to [bend right=45] (semaphore);
\draw[->] (semaphore) to [bend right=45] (leave critical);
\draw[->] (leave critical) to [out=90, in=0] (waiting);
\draw[->] (critical) to (leave critical);
\end{scope}
\end{tikzpicture}
It is now time for Hagen to learn about yet another way of specifying edges: Using the edge
path operation.
This operation is very similar to the to operation, but there is one important difference: Like a node the edge generated by the edge operation is not part of the main path, but is added only later.
This may not seem very important, but it has some nice consequences. For example, every edge can have its own arrow tips and its own color and so on and, still, all the edges can be given on the same path. This allows Hagen to write the following:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}
\begin{tikzpicture}
\node[place] (waiting) {};
\node[place] (critical) [below=of waiting] {};
\node[place] (semaphore) [below=of critical] {};
\node[transition] (leave critical) [right=of critical] {};
\node[transition] (enter critical) [left=of critical] {}
edge [->] (critical)
edge [<-,bend left=45] (waiting)
edge [->,bend right=45] (semaphore);
\end{tikzpicture}
\end{document}
The next thing that Hagen needs to add is the “2” at the arcs.
For this Hagen can use TikZ’s automatic node placement: By adding the option auto, TikZ will position nodes on curves and lines in such a way that they are not on the curve but next to it.
Adding swap
will mirror the label with respect to the line. Here is a general example:
\begin{tikzpicture}[auto, bend right]
\node (a) at (0:1) {$0^\circ$};
\node (b) at (120:1) {$120^\circ$};
\node (c) at (240:1) {$240^\circ$};
\draw (a) to node {1} node [swap] {1’} (b)
(b) to node {2} node [swap] {2’} (c)
(c) to node {3} node [swap] {3’} (a);
\end{tikzpicture}
The nodes are given somehow inside the to operation! When this is done, the node is placed on the middle of the curve or line created by the to operation.
The auto option then causes the node to be moved in such a way that it does not lie on the curve, but next to it.
You can also adjust the node
by hand:
\draw[->] (c) to node {3} node [right=5pt, below=3pt] {3’} (a);
With the node mechanism Hagen can now easily create the two Petri nets. What he is unsure of is how he can create the snaked line between the nets.
For this he can use a decoration
.
To draw the snaked line, Hagen only needs to set the two options decoration=snake
and decorate on the path. This causes all lines of the path to be replaced by snakes. It is also possible to use snakes only in certain parts of a path, but Hagen will not need this.
Study the code:
\begin{tikzpicture}[auto, bend right]
\node (a) at (0:1) {$0^\circ$};
\node (b) at (120:1) {$120^\circ$};
\node (c) at (240:1) {$240^\circ$};
\draw[->, decorate, decoration=snake] (c) to node {3} node [right=5pt, below=3pt] {3'} (a);
\draw[->, decorate, decoration={snake, amplitude=0.4mm, segment length=1mm, post length=1mm}] (a) to node {2} node [swap] {2'} (b);
\draw[->, decorate, decoration={snake, amplitude=1mm, segment length=1mm, post length=1mm}] (b) to node {1} node [swap] {1'} (c);
\end{tikzpicture}
Hagen still needs to add the background rectangles. These are a bit tricky: Hagen would like to draw the rectangles after the Petri nets are finished.
The reason is that only then can he conveniently refer to the coordinates that make up the corners of the rectangle. If Hagen draws the rectangle first, then he needs to know the exact size of the Petri net – which he does not.
The solution is to use layers.
When the background library is loaded, Hagen can put parts of his picture inside a scope
with the on background layer
option.
When the {tikzpicture}
environment ends, the layers are put on top of each other, starting with the background layer. This causes everything drawn on the background layer to be behind the main text.
Naturally, Hagen can compute the size “by hand” or using some clever observations concerning the x- and y-coordinates of the nodes, but it would be nicer to just have TikZ compute a rectangle into which all the nodes “fit.”
See:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}
\begin{tikzpicture}
[inner sep=2mm,
place/.style={circle,draw=blue!50,fill=blue!20,thick},
transition/.style={rectangle,draw=black!50,fill=black!20,thick}]
\node[place] (waiting) {};
\node[place] (critical) [below=of waiting] {};
\node[place] (semaphore) [below=of critical] {};
\node[transition] (leave critical) [right=of critical] {};
\node[transition] (enter critical) [left=of critical] {};
\node[place] (low2) [below=of semaphore] {$\sum$};
\node[place] (low1) [below=of low2] {}
edge [->, loop below, semithick] (low1);
\node[transition] (left) [left=of low2] {};
\node[transition] (right) [right=of low2] {};
\node [red, above] at (semaphore.north) {$s \le 3$};
\begin{scope}[>=stealth' ,semithick]
\draw [->] (enter critical.east) -- (critical);
\draw[->] (waiting) to[out=180, in=90] (enter critical.north);
\draw[->] (enter critical) to [bend right=45] (semaphore);
\draw[->] (semaphore) to [bend right=45] (leave critical);
\draw[->] (leave critical) to [out=90, in=0] (waiting);
\draw[->] (critical) to (leave critical);
\draw[->, decorate, decoration={snake, amplitude=0.4mm, segment length=2mm, post length=1mm}] (semaphore) to [out=0, in=90] (right);
\draw[->] (left) to (low2);
\draw[->] (low1) to [out=180, in=-90] node[sloped, below]{Interesting} (left);
\draw[->] (left) to [out=90, in=190] (semaphore);
\draw[->] (right) to [out=-90, in=0] (low1);
\draw[->] (low2) to (right);
\draw[->, decorate, decoration={snake, amplitude=0.4mm, segment length=1mm, post length=1mm}] (low1) to [out=135, in=-135] (low2);
\draw[->] (low2) to [out=-45, in=45] (low1);
\end{scope}
\begin{scope}[on background layer]
\node[fill=yellow!30, fit=(waiting) (critical) (semaphore) (leave critical) (enter critical)]{};
\end{scope}
\end{tikzpicture}
\end{document}
注意:圆形内的“点”:
并非通过画图命令控制。这些带“点”的原型是“圆形”的一个子类。详情请看Manual。