节点与边


节点

在tikz中节点意味着一个形状以及置于其中的文字。节点可以用命令“\node” 构造,也可以作为路径元素,语法是

... node[选项1,选项2,...] (名字) at 坐标 {文字} ...;
除了“node”,其它部分都是可选的。节点可以有一个名字,之后你可以像使用一个坐标一样 引用它。名字也可以通过选项
name=名字
alias=名字
指定。“坐标”用于定位节点,“文本”被标注在节点内。比如
\tikz \fill[fill=examplefill]
(0,0) node {first node}
-- (1,1) node[draw] {second node}
-- (0,2) node[fill=red!20,draw,double,rounded corners] {third node};

分割节点

有时需要将节点分割为多个部分,比如面向对象的属性分析。这可以用命令

\nodepart[选项1,选项2,...]{名字}
它只能用在节点的“文本”部分。比如
\begin{tikzpicture}
\node [circle split,draw,double,fill=red!20]
{
$q_1$
\nodepart{lower}
$00$
};
\end{tikzpicture}
你可以多次使用它,以便将节点分割为多个部分。

风格

every 名字 node part
可以控制同一名字的节点部分。比如
\tikz [every lower node part/.style={red}]
\node [circle split,draw] {$q_1$ \nodepart{lower} $00$};

节点中的文本

节点中文本的颜色可以用选项

text=颜色表达式
设置。比如
\begin{tikzpicture}
\draw[red] (0,0) -- +(1,1) node[above] {red};
\draw[text=red] (1,0) -- +(1,1) node[above] {red};
\draw (2,0) -- +(1,1) node[above,red] {red};
\end{tikzpicture}

文本字体可以通过选项

font=字体命令
设置。比如
\begin{tikzpicture}
\draw[font=\itshape] (1,0) -- +(1,1) node[above] {italic};
\end{tikzpicture}

如果是多行文本,可以使用“\\”命令进行断行,多行多列文本可以用表格。比如

\tikz[align=center] \node[draw] {This is a\\demonstration.};

你还可以用选项

text width=宽度
指定文本行宽度,使其自动断行。

多行文本的对齐可以用选项

align=对齐方式
“对齐方式”可能的值有
left
flush left
right
flush right
center
flush center
justify

文本的高度和深度可以通过选项

text height=高度
text depth=深度

节点的形状

节点最有趣的属性是它的形状,这可以通过选项

shape=名字
指定。有三个预先定义的形状“rectangle”、“circle”、“coordinate”,其它形状要用到库。 下面两个风格会影响节点
every node
every 形状 node
比如
\begin{tikzpicture}[every node/.style={draw}]
\draw (0,0) node {A} -- (1,1) node {B};
\end{tikzpicture}

如果节点的形状是“coordinate”,也就是表示一个点,可以用命令“\coordinate” 表示,或者作为路径元素

... coordinate[选项1,选项2,...] (名字) at 坐标 ...;
这时,“名字”是必须的。

节点可以有不同的形状,这些形状有相同或不同的属性。下面这些属性是 共同的

inner sep=长度
设置了在形状边沿和文本之间的空隙。
inner xsep=长度
只设定x轴方向上形状边沿和文本间的空隙,类似地
inner ysep=长度
设定了y轴方向上的间隙。比如
\begin{tikzpicture}
\draw (0,0) node[inner sep=0pt,draw] {tight}
(0cm,2em) node[inner sep=5pt,draw] {loose}
(0cm,4em) node[fill=examplefill] {default};
\end{tikzpicture}
形状边沿外侧的距离,也就是如果有连接线的话,这个距离假如 大于零,那么看上去连接点和边沿就有一定的距离,有三个选项
outer sep=长度
outer xsep=长度
outer ysep=长度
比如
\begin{tikzpicture}
\draw[line width=5pt]
(0,0) node[outer sep=0pt,fill=examplefill] (f) {filled}
(2,0) node[inner sep=.5\pgflinewidth+2pt,draw] (d) {drawn};
\draw[->] (1,-1) -- (f);
\draw[->] (1,-1) -- (d);
\end{tikzpicture}

形状的最小高度可以用选项

minimum height=高度
指定。比如
\begin{tikzpicture}
\draw (0,0) node[minimum height=1cm,draw] {1cm}
(2,0) node[minimum height=0cm,draw] {0cm};
\end{tikzpicture}
最小宽度可以用选项
minimum width=宽度
指定。也可以用选项
minimum size=长度
同时指定长度和宽度。比如
\begin{tikzpicture}
\draw (0,0) node[minimum size=2cm,draw] {square};
\draw (0,-2) node[minimum size=2cm,draw,circle] {circle};
\end{tikzpicture}

形状的宽高比可以用选项

shape aspect=比率
指定。比如
\begin{tikzpicture}
\draw (0,0) node[shape aspect=1,diamond,draw] {aspect 1};
\draw (0,-2) node[shape aspect=2,diamond,draw] {aspect 2};
\end{tikzpicture}

选项

shape border uses incircle=true/false
决定了形状的边界是否用内切圆构造,“true”是缺省值。选项
shape border rotate=角度
可以将形状旋转一定的角度。

与形状相关的节点的另一个有趣的属性是“锚定点”,也就是当用线段将节点或节点与其他元素 连接起来时,连接线应该位于节点的哪个位置。这可以用选项

anchor=锚定点的名字
设定,作为坐标引用时可以用“节点的名字.锚定点的名字”。比如
\begin{tikzpicture}[scale=3,transform shape]
\draw[anchor=center] (0,1) node{x} -- (0.5,1) node{y} -- (1,1) node{t};
\draw[anchor=base] (0,.5) node{x} -- (0.5,.5) node{y} -- (1,.5) node{t};
\draw[anchor=mid] (0,0) node{x} -- (0.5,0) node{y} -- (1,0) node{t};
\end{tikzpicture}
下列选项还可以设定从哪个方向锚定
above=偏移量
below=偏移量
left=偏移量
right=偏移量
above left
above right
below left
below right
比如
\tikz \fill (0,0) circle (2pt) node[above] {above};

预先定义的形状

圆形的名字是“circle”,它的锚定点有

north
center
mid
base
text
south
130
north west
west
mid west
base west
south west
north east
10
east
mid east
base east
south east

矩形

矩形的名字是“rectangle”,它的锚定点有

north
center
mid
base
text
south
130
north west
west
mid west
base west
south west
north east
10
east
mid east
base east
south east

几何形状

几何形状定义在“shapes.geometric”库中,所以请在LaTeX导言中加入

\usetikzlibrary{shapes.geometric}

菱形

菱形的名字是“diamond”,其宽高比可以用选项

aspect=宽高比
设置。它的锚定点有
north
center
mid
base
text
south
130
north west
west
south west
north east
10
east
south east

椭圆形

椭圆形的名字是“ellipse”,它的锚定点有

north
center
mid
base
text
south
130
north west
west
mid west
base west
south west
north east
10
east
mid east
base east
south east

梯形

梯形的名字是“trapezium”,它的形状可以通过下列选项设置

trapezium left angle=左底角
trapezium right angle=右底角
trapezium angle=底角
比如
\begin{tikzpicture}
\tikzstyle{every node}=[trapezium, draw]
\node at (0,2) {A};
\node[trapezium left angle=75, trapezium right angle=45pt] at (0,1) {B};
\node[trapezium left angle=120, trapezium right angle=60pt] at (0,0) {C};
\end{tikzpicture}

选项

trapezium stretches=true/false
控制着梯形的 宽高是否可以各自独立地扩大,也就是在水平方向或垂直方向拉伸。比如
\begin{tikzpicture}
\tikzset{trapezium stretches=true}
\draw [help lines] grid (3,2);
\node [my node=red] {A};
\node [my node=green, minimum height=1.5cm] at (1, 1.25) {B};
\node [my node=blue, minimum width=1.5cm] at (2, 0) {C};
\end{tikzpicture}
和上述选项类似的另一个选项是
trapezium stretches body=true/false
区别是它会扩大上底。

梯形的锚定点有

top side
north
center
mid
base
text
south
bottom side
north west
top left corner
160
west
left side
mid west
base west
bottom left corner
south west
north east
top right corner
east
right side
mid east
base east
bottom right corner
south east

半圆形

半圆形的名字是“semicircle”,它的锚定点是

apex
north
center
mid
base
text
south
chord center
north west
west
mid west
base west
arc end
south west
north east
30
east
mid east
base east
arc start
south east

正多边形

正多边形的名字是“regular polygon”,其形状由选项

regular polygon sides=边数
决定。它的锚定点是
north
center
mid
base
text
south
corner 1
corner 2
...
corner n
side 1
side 2
...
side n
north west
west
south west
75
north east
east
south east

星形

星形的名字是“star”,形状可以由下列选项设定

star points=角的数量
star point height=中心到外角的距离
star point ratio=内角到中心的距离与外角到中心的距离的比值
它的锚定点是
north
center
mid
base
text
south
outer point 1
outer point 1
...
outer point n
inner point 1
inner point 2
...
inner point n
north west
west
south west
north east
east
south east

等腰三角形

等腰三角形的名字是“isosceles triangle”,选项

isosceles triangle apex angle=顶角
可以设置顶角。选项
isosceles triangle stretches=true/false
决定宽高是否可以独立拉伸。它的锚定点是
apex
north
center
mid
base
text
south
north east
left side
left corner
east
mid east
base east
south east
right side
right corner
north west
150
west
lower side
mid west
base west
south west

风筝形

风筝形的名字是“kite”。其形状可以通过三个角度确定

kite upper vertex angle=顶部内角
kite lower vertex angle=底部内角
kite vertex angles=四个内角
它的锚定点是
upper vertex
north
center
mid
base
text
south
lower vertex
110
upper left side
north west
left vertex
west
mid west
base west
south west
lower left side
upper right side
north east
right vertex
east
mid east
base east
south east
lower right side

飞镖形

飞镖形的名字是“dart”。形状可以通过两个角度设置

dart tip angle=镖头的夹角
dart tail angle=镖尾的夹角
它的锚定点是
tip
north
center
mid
base
text
south
tail center
110
north west
left side
west
mid west
base west
south west
left tail
north east
right side
east
mid east
base east
south east
right tail

扇形

扇形的名字是“circular sector”,它只有圆心角可以设置

circular sector angle=圆心角
它的锚定点是
north
center
mid
base
text
south
arc start
north west
west
arc center
south west
arc end
north east
30
sector center
east
south east

圆柱形

圆柱形的名字是“cylinder”,它的形状可以通过宽高比设置

aspect=宽高比
还可以设置是否用颜色填充
cylinder uses custom fill=true/false
cylinder end fill=顶部颜色
cylinder body fill=柱体颜色
它的锚定点是
north
center
shape center
mid
base
text
south
north west
after bottom
160
bottom
west
mid west
base west
before bottom
south west
north east
before top
top
east
mid east
base east
after top
south east

符号形状

符号形状定义在“shapes.symbols”库中,所以请在LaTeX导言中加入

\usetikzlibrary{shapes.symbols}

禁止标志

禁止标志的名字是“forbidden sign”,它的锚定点是

north
center
mid
base
text
south
130
north west
west
mid west
base west
south west
north east
10
east
mid east
base east
south east

云形的名字是“cloud”,云的膨胀数和膨胀处的弧长可以用选项

cloud puffs=膨胀数
cloud puff arc=膨胀处的弧长
指定,
cloud ignores aspect=true/false
可以指定是否忽视宽高比。它的锚定点是
north
center
mid
base
text
south
puff 1
puff 2
...
puff n
north west
west
south west
north east
east
south east

爆炸

爆炸形的名字是“starburst”,炸出的尖点由选项

starburst points=炸出的尖点数
starburst point height=尖点边的最大长度
random starburst=创建炸点的随机种子数,最大为16383
它的锚定点是
north
center
mid
base
text
south
outer point 1
outer point 2
...
outer point n
inner point 1
inter point 2
...
inner point n
north west
west
south west
80
north east
east
south east

示意牌

示意牌的名字是“signal”,箭头由选项

signal pointer angle=箭头两边的形成的角度
signal from=从哪个方向指向示意牌
signal to=从示意牌指向哪个方向
决定,其中方向的值可以是
north
south
east
west
above
below
left
right
也可以在这些值后加上“and 反方向”。示意牌的锚定点有
north
center
mid
base
text
south
north west
west
mid west
base west
south west
70
north east
east
mid east
base east
south east

飘带

飘带形的名字是“tape”,其弯曲由选项

tape bend top=弯曲风格
tape bend bottom=弯曲风格
tape bend height=弯曲高度
设置,其中弯曲风格可以是“in and out”、“out and in”、“none”。 它的锚定点是
north
center
mid
base
text
south
north west
west
mid west
base west
south west
60
north east
east
mid east
base east
south east

箭头形状

箭头形状定义在“shapes.arrows”库中,所以请在LaTeX导言中加入

\usetikzlibrary{shapes.arrows}

单箭头

单箭头的名字是“single arrow”,箭头形状由选项

single arrow tip angle=头部夹角
single arrow head extend=头部底边(单侧)长度
single arrow head indent=头部底边向头部缩进的长度
它的锚定点是
north
center
mid
base
text
south
north west
after tail
west
mid west
base west
before tail
south west
before head
north east
before tip
20
east
tip
mid east
base east
south east
after tip
after head

双箭头

双箭头形的名字是“double arrow”,箭头形状由选项

double arrow tip angle=头部夹角
double arrow head extend=头部底边(单侧)长度
double arrow head indent=头部底边向头部缩进的长度
它的锚定点是
north
center
mid
base
text
south
after head 2
north west
after tip 2
tip 2
west
mid west
base west
south west
before tip 2
before head 2
before head 1
north east
before tip 1
20
east
tip 1
mid east
base east
south east
after tip 1
after head 2

箭头盒子

箭头盒子形的名字是“arrow box”,箭头形状由选项

arrow box tip angle=头部夹角
arrow box head extend=头部底边(单侧)长度
arrow box head indent=头部底边向头部缩进的长度
此外还有
arrow box shaft width=箭杆宽度
arrow box north arrow=北边箭头长度
arrow box south arrow=南边箭头长度
arrow box east arrow=东边箭头长度
arrow box west arrow=西边箭头长度
arrow box arrows={箭头长度列表}
它的锚定点是
north arrow tip
north
center
mid
base
text
south
south arrow tip
before north arrow tip
before north arrow head
before north arrow
north west
after west arrow
after west arrow head
after west arrow tip
west arrow tip
west
mid west
base west
before west arrow head
before west arrow tip
before west arrow
south west
after south arrow
after south arrow head
after south arrow tip
75
after north arrow tip
after north arrow head
after north arrow
north east
before east arrow
before east arrow head
east arrow tip
east
mid east
base east
after east arrow head
after east arrow tip
after east arrow
south east
before south arrow
before south arrow head
before south arrow tip

多部分文本

多部分文本定义在“shapes.multipart”库中,所以请在LaTeX导言中加入

\usetikzlibrary{shapes.multipart}

圆形分割

圆形分割的名字是“circle split”,锚定点是

north
center
mid
base
text
lower
north
130
north west
mid west
base west
west
south west
north east
mid east
base east
east
south east

圆形斜线分割

圆形斜线分割的名字是“circle solidus”,锚定点与圆形分割一样。

椭圆形分割

椭圆形分割的名字是“ellipse split”,锚定点与圆形分割一样。

矩形分割

矩形分割的名字是“rectangle split”,分割的数量与分割办法由选项

rectangle split parts=分成几部分
rectangle split horizontal=是否水平分割
rectangle split ignore empty parts=是否忽略空的部分
rectangle split empty part width=空部分的宽度
rectangle split empty part height=空部分的高度
rectangle split empty part depth=空部分的深度
rectangle split part align={对齐方式列表,逗号分隔,left,right,center之一}
rectangle split draw splits=是否绘制分割线
rectangle split use custom fill=是否填充颜色
rectangle split part fill={用逗号分隔的颜色表达式列表}
锚定点是
north
center
mid
base
text
north
one
two
...
north west
text west
text split west
two west
two split west
...
text east
text split east
two east
two split east
...

标注

标注定义在“shapes.callout”库中,所以请在LaTeX导言中加入

\usetikzlibrary{shapes.callout}

标注有一些共同属性

callout relative pointer=被标注点相对于标注的坐标
callout absolute pointer=被标注点在图形图像中的绝对坐标
callout pointer shorten=标注缩短的长度

矩形标注

矩形标注的名字是“rectangle callout”,可以设置选项

callout pointer width=指针在矩形边界上的宽度
它的锚定点是
north
center
mid
base
text
south
north west
west
mid west
base west
south west
north east
60
east
mid east
base east
south east

椭圆形标注

椭圆形标注的名字是“ellipse callout”,可以设置选项

callout pointer arc=指针在椭圆边界上的弧度
它的锚定点是
north
center
mid
base
text
south
north west
west
mid west
base west
south west
north east
60
east
mid east
base east
south east

云形标注

云形标注的名字是“cloud callout”,其形状可以通过选项

callout pointer start size=指针起始段的大小
callout pointer end size=指针末端的大小
callout pointer segments=指针的数量
它的锚定点是
north
center
mid
base
text
south
north west
west
puff 1
puff 2
...
puff n
south west
north east
70
east
south east

杂项

杂项形状定义在“shapes.misc”库中,所以请在LaTeX导言中加入

\usetikzlibrary{shapes.misc}

交叉线

交叉线的名字是“cross out”,它的锚定点是

north
center
mid
base
text
south
130
north west
west
mid west
base west
south west
north east
10
east
mid east
base east
south east

斜线

斜线的名字是“strike out”,锚定点与交叉线一样。

圆角矩形

圆角矩形的名字是“rounded rectangle”,圆角的形状可以通过选项

rounded rectangle arc length=圆角弧度
rounded rectangle west arc=西侧圆角弧度
rounded rectangle left arc=左侧圆角弧度
rounded rectangle east arc=东侧圆角弧度
rounded rectangle right arc=右侧圆角弧度
设置。它的锚定点是
north
center
mid
base
text
south
north west
west
mid west
base west
south west
north east
10
east
mid east
base east
south east

倒角矩形

倒角矩形的名字是“chamfered rectangle”,倒角形状可以通过

chamfered rectangle angle=倒角弧度
chamfered rectangle xsep=倒角在x轴方向上到中心的距离
chamfered rectangle ysep=倒角在y轴方向上到中心得距离
chamfered rectangle sep=倒角在两个方向上到中心的距离
chamfered rectangle corners={要倒些角,逗号分隔,有“north east”、“north west”、“south west”、“south east”}
设置。它的锚定点是
north
center
mid
base
text
south
before north west
north west
after north west
west
mid west
base west
before south west
south west
after south west
70
after north east
north east
before north east
east
mid east
base east
after south east
south east
before south east

变换

一般来说,坐标变换不会作用于节点,不过你可以用选项

transform shape
来要求坐标变换运用于节点。

在直线段和曲线段上放置节点

如果你打算把节点放在直线段或曲线段上,而且不打算计算放置点的坐标,可以用选项

pos=比率
这个比率是放置点到前一个点的距离比前一个点到当前点的距离,比如
\tikz \draw (0,0) -- (3,1) node[pos=0]{0} node[pos=0.5]{1/2} node[pos=0.9]{9/10};

选项

auto=left或者right
以如下原则计算放置点:考虑两点间的连线,如果选项的值是left,放置点就在连线的左侧。 如果选项的值是right,放置点就在连线的右侧。而选项
swap
则将上述规则调个。

选项

sloped
使节点被旋转,以便它沿着曲线的切线方向。

选项

allow upside down=true/false
的值如果是true,则允许节点文本由下向上。

如果你连比率都不打算指定,可以用下列文本化的位置

midway
near start
near end
very near start
very near end
at start
/tikz/at end

label和pin

或许你想到可以用节点标注,没准你还想标注节点,或者其它复杂的标注。 你可以用选项

label=[选项]角度:标注文本
其中“[选项]”和“角度”是可选的。这个选项会在当前结点完成后,额外添加一个节点。 这个节点的文本是“标注文本”。原则上这个额外节点被放置在相对于主节点的“角度” 上。如果它被省略,就使用选项
label position=角度
的值。“角度”既可以是数字的,也可以是“above”、“below”、“left”、“right”, 或者四个单词的组合。选项
absolute=true/false
的值如果是true,“角度”的度量是绝对的:穿过主节点的中心,指向纸张的右侧的直线。 额外节点和主节点的距离由选项
label distance=长度
设置。影响label的风格是
every label

选项

pin=[选项]角度:标注文本
与label相似,它会再额外在主节点和标注节点间加一条边。缺省的角度是
pin position=角度
的值。与主节点的距离由
pin distance=长度
指定。边的属性由选项
pin edge={选项}
设置。影响pin的风格有
every pin
every pin edge

连接两个节点,一种方法是把节点当坐标,比如

\begin{tikzpicture}
\path (0,0) node (x) {Hello World!}
(3,1) node[circle,draw](y) {$\int_1^2 x \mathrm d x$};
\draw[->,blue] (x) -- (y);
\draw[->,red] (x) -| node[near start,below] {label} (y);
\draw[->,orange] (x) .. controls +(up:1cm) and +(left:1cm) .. node[above,sloped] {label} (y);
\end{tikzpicture}
另一种办法就是使用“边”。

边是一种路径元素,它的语法是

... edge[选项] 节点 坐标 ...;
其中“节点”是可选的,如果有,可以作为边的标注。比如
\begin{tikzpicture}
\foreach \name/\angle in {a/0,b/90,c/180,d/270}
\node (\name) at (\angle:1.5) {$\name$};
\path[->] (b) edge node[above right] {$5$} (a) edge (c) edge [-,dotted] node[below,sloped] {missing} (d)
(c) edge (a) edge (d) (d) edge [red] node[above,sloped] {very} node[below,sloped] {bad} (a);

\end{tikzpicture}

边有风格

every edge


目录