赞
踩
Tikz作图教程:图论中环的画法
今天我们来介绍基于Tikz
的图论图形绘制宏包:tkz-graph
。学会三个基本作图命令和若干选项参数,就能非常方便地绘制各种图论中的图形。
一个图里面有三类要素:顶点(Vertex)、边(Edge)、环(Loop)
。tkz-graph
中的三个基本作图命令就是反斜杠\
后面跟上这三个单词构成的。
画顶点需要给出顶点的坐标和顶点的引用名称。
\Vertex
\Vertex[<options>]{<name>}
<options>
里常用选项有坐标, Math
,例如\Vertex[Math,x=1,y=0](x)
的意思是设置顶点x
的坐标为(1,0)
, 顶点标签用数学字体显示。如果省略坐标,则默认该点位于(0,0)
,即坐标原点。
<name>
是这个顶点的名称,定义了名为x
的顶点以后,下面就可以用(x)
来引用它。
\Edge
画边需要给出两个顶点:
\Edge[<options>](<vertex1>)(<vertex2>)
[red,dashed,label={$e_1$}]
意为这条边为“红色,虚线,标签为
e
1
e_1
e1”。\Loop
画环只要一个顶点:
\Loop[<options>]{<vertex>}
[dist=2cm,dir=NO,style={-,dashed,red},label={$e_4$}]
, 其中,dist=2cm
控制环的大小。dir=NO
表示环的方向朝北(与地图方位一样,表示在顶点的上方)。类似的,dir=SO
朝下,dir=WE
和dir=EA
分别表示朝左和朝右。一个简单的例子如下:
代码为:
\begin{tikzpicture}[
edge1/.style={red,dashed,bend left},edge2/.style={blue, bend left},
]
\tikzset{LabelStyle/.style = {shape=rectangle,draw=none,fill=brown!50,font=\normalsize,text =black}}
\Vertex[Math]{y}
\Vertex[Math,x=0,y=3]{x}
\Edge[style=edge2,label={$e_3$}](x)(y)
\Edge[style=edge1,label={$e_2$}](y)(x)
\Loop[dist=2cm,dir=NO,style={-,dashed,red},label={$e_4$}](x)
\Loop[dist=2cm,dir=SO,style={-,dashed,red},label={$e_1$}](y)
\end{tikzpicture}
如下修改设置:
\begin{tikzpicture}[auto,every node/.style={shape=circle,draw},
edge1/.style={red,dashed,bend left},edge2/.style={blue, bend left},
]
\tikzset{LabelStyle/.style = {draw=none,font=\normalsize,text =black}}
tikzpicture
环境后中括号内增加选项auto
;draw=none
,即不画出标签边框,不填充。art
增加语句:
\begin{tikzpicture}[auto,every node/.style={shape=circle,draw},
edge1/.style={red,dashed,bend left},edge2/.style={blue, bend left},
]
\GraphInit[vstyle=art]
\tikzset{LabelStyle/.style = {draw=none,font=\normalsize,text =black}}
可以将顶点风格设置为art
,即顶点为橙色渐变小球,边的颜色为橙色。
修改设置得到的两个图与原图对比:
读者可以试着画出下面的葛尼斯堡七桥问题的图形:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。