我今天刚开始使用 TikZ,遇到了一个问题:没有用于排版有向加权图的示例代码片段。有人可以在答案中提供一个简单的例子吗?
答案1
我制作tkz-graph
并tkz-berge
帮助初学者绘制一些图表。tkz-berge
用于特殊图表(图论中称为图表)
您只能使用它tikz
来绘制图形。
带 tkz-graph 的版本
\documentclass[]{article}
\usepackage[utf8]{inputenc}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{fullpage}
\usepackage[upright]{fourier}
\usepackage{tkz-graph}
\usetikzlibrary{arrows}
\thispagestyle{empty}
\begin{document}
\SetVertexNormal[Shape = circle,
FillColor = orange,
LineWidth = 2pt]
\SetUpEdge[lw = 1.5pt,
color = black,
labelcolor = white,
labeltext = red,
labelstyle = {sloped,draw,text=blue}]
\begin{center}
\begin{tikzpicture}
\Vertex[x=0 ,y=0]{K}
\Vertex[x=0 ,y=2]{F}
\Vertex[x=-1,y=4]{D}
\Vertex[x=3 ,y=7]{H}
\Vertex[x=8 ,y=5]{B}
\Vertex[x=9 ,y=2]{N}
\Vertex[x=5 ,y=0]{M}
\Vertex[x=3 ,y=1]{S}
\tikzset{EdgeStyle/.append style = {bend left}}
\Edge[label = $120$](K)(F)
\Edge[label = $650$](H)(S)
\Edge[label = $780$](H)(M)
\Edge[label = $490$](D)(B)
\Edge[label = $600$](D)(M)
\Edge[label = $580$](B)(M)
\Edge[label = $600$](H)(N)
\Edge[label = $490$](F)(H)
\tikzset{EdgeStyle/.append style = {bend right}}
\Edge[label = $630$](S)(B)
\Edge[label = $210$](S)(N)
\Edge[label = $230$](S)(M)
\end{tikzpicture}
\end{center}
\end{document}
边缘有箭头
\documentclass[11pt]{scrartcl}
\usepackage{tkz-graph}
\begin{document}
\begin{tikzpicture}
\SetUpEdge[lw = 1.5pt,
color = orange,
labelcolor = white]
\GraphInit[vstyle=Normal]
\SetGraphUnit{3}
\tikzset{VertexStyle/.append style={fill}}
\Vertex{P}
\NOEA(P){B} \SOEA(P){M} \NOEA(B){D}
\SOEA(B){C} \SOEA(C){L}
\tikzset{EdgeStyle/.style={->}}
\Edge[label=$3$](C)(B)
\Edge[label=$10$](D)(B)
\Edge[label=$10$](L)(M)
\Edge[label=$10$](B)(P)
\tikzset{EdgeStyle/.style={<->}}
\Edge[label=$4$](P)(M)
\Edge[label=$9$](C)(M)
\Edge[label=$4$](C)(L)
\Edge[label=$5$](C)(D)
\Edge[label=$10$](B)(M)
\tikzset{EdgeStyle/.style={<->,relative=false,in=0,out=60}}
\Edge[label=$11$](L)(D)
\end{tikzpicture}
\end{document}
更新:版本tikz
与automata
\documentclass[11pt]{scrartcl}
\PassOptionsToPackage{usenames,dvipsnames,svgnames}{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,automata}
\begin{document}
\begin{tikzpicture}[>=stealth',shorten >=1pt,node distance=3cm,on grid,initial/.style ={}]
\node[state] (P) {$P$};
\node[state] (B) [above right =of P] {$B$};
\node[state] (M) [below right =of P] {$M$};
\node[state] (D) [above right =of B] {$D$};
\node[state] (C) [below right =of B] {$C$};
\node[state] (L) [below right =of C] {$L$};
\tikzset{mystyle/.style={->,double=orange}}
\tikzset{every node/.style={fill=white}}
\path (C) edge [mystyle] node {$3$} (B)
(D) edge [mystyle] node {$10$} (B)
(L) edge [mystyle] node {$10$} (M)
(B) edge [mystyle] node {$10$} (P);
\tikzset{mystyle/.style={<->,double=orange}}
\path (P) edge [mystyle] node {$4$} (M)
(C) edge [mystyle] node {$9$} (M)
(C) edge [mystyle] node {$4$} (D)
(B) edge [mystyle] node {$5$} (M);
\tikzset{mystyle/.style={<->,relative=false,in=0,out=60,double=orange}}
\path (L) edge [mystyle] node {$10$} (D);
\end{tikzpicture}
\end{document}
答案2
tkz-graph
因为我只想要一些简单的东西,所以这里有一个使用Alain Matthes 代码的精简版,它提供了合理的数学/CS 论文默认值,并演示了一些常见的顶点、边和标签选项。
\documentclass{article}
\usepackage{tkz-graph}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\Vertex[x=0,y=0]{K} % standard vertex at (0,0) with Name k
\Vertex[x=0,y=2]{F}
\Vertex[x=-1,y=4]{D}
\Vertex[x=8,y=5]{B}
\Vertex[x=9,y=2,L=$n$]{N} % custom label
\Vertex[x=5,y=0,LabelOut,Lpos=-90,Ldist=.2cm]{M} % custom label position
\Vertex[x=3,y=3.5,NoLabel]{S} % unlabeled
\tikzstyle{LabelStyle}=[sloped] % rotate labels to match edges
\Edge[label=$120$,labelstyle={pos=0.3,above}](K)(F) % custom label position
\Edge[label=$490$](D)(B)
\Edge[label=$580$](B)(M)
\Loop[dir=NO,dist=2cm,label=b,labelstyle={auto}](B) % loop/self-edge
\tikzstyle{EdgeStyle}=[pre, bend right] % set curved arrow style
\Edge[label=$210$](S)(N)
\end{tikzpicture}
\end{figure}
\end{document}
有用的资源:
- 循环:如何制作边从同一顶点开始和结束的 tkz 图?
- 不可见的顶点:使用 tkz-graph 隐藏顶点
- 无圆的顶点:如何在 tikz 中绘制仅由标签名称表示的顶点的图形?
- 引用内部包含数学类型的顶点来创建边
labelstyle
使用(above
,sloped
,pos
, ...)对标签进行微调标记加权有向图中的边- 改变弯曲角度并交换箭头上的节点位置
tkz-graph 的手册是法语的,但其中包含足够的英语单词或同源词,可以辨认。https://ctan.org/pkg/tkz-graph
tkz-graph 还附带有用的内置图形样式: