我正在尝试在 TikZ 中绘制下图:
我的尝试看起来像这样(请注意节点标签略有不同,但这不是我的问题!):
我的代码如下:
\documentclass[11pt,a4paper]{report}
\usepackage[USenglish]{babel}
\usepackage[T1]{fontenc}
\usepackage[ansinew]{inputenc}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,backgrounds,arrows,shapes,automata,petri}
\tikzset{
place/.style={
circle,
thick,
minimum size=6mm,
draw
},
transitionV/.style={
rectangle,
thick,
fill=black,
minimum height=6mm,
inner xsep=1pt
}
}
\begin{document}
\title{Test}
\author{Me}
\date{\today}
\begin{figure}[h!]
\centering
\begin{tikzpicture}[node distance=1cm and 2cm,>=stealth',bend angle=45,thick]
\node [transitionV,label=above:$q_1$] (q1) {};
\node [place,tokens=1,label=above:$\alpha_{11}(k)$] (p11) [below= of q1] {}
edge[pre,bend right,overlay] (q1)
edge[post,bend left,overlay] (q1);
\node [place,tokens=2,label=above:$\alpha_{41}(k)$] (p41) [below right=0.25cm and 2cm of q1] {}
edge[pre] (q1);
\node [transitionV,label=above:$q_4$] (q4) [below right=0.25cm and 2cm of p41] {}
edge[pre] (p41);
\node [place,label=above:$\alpha_{24}(k)$] (p24) [above right=0.25cm and 2cm of q4] {}
edge[pre] (q4);
\node [transitionV,label=above:$q_2$] (q2) [above right=0.25cm and 2cm of p24] {}
edge[pre] (p24);
\node [place,tokens=1,label=above:$\alpha_{22}(k)$] (p22) [below= of q2] {}
edge[pre,bend right,overlay] (q2)
edge[post,bend left,overlay] (q2);
\node [place,tokens=1,label=above:$\alpha_{31}(k)$] (p31) [above right=0.25cm and 2cm of q1] {}
edge[pre] (q1);
\node [transitionV,label=below:$q_3$] (q3) [above right=0.25cm and 2cm of p31] {}
edge[pre] (p31);
\node [place,label=above:$\alpha_{23}(k)$] (p23) [below right=0.25cm and 2cm of q3] {}
edge[pre] (q3)
edge[post] (q2);
\node [place,tokens=1,label=below:$\alpha_{12}(k)$] (p12) [above= of q3] {}
edge[pre,bend left,out=45,in=45,looseness=1.2] (q2)
edge[post,bend left,out=315,in=315,looseness=1.2] (q1);
\end{tikzpicture}
\caption{Event graph}
\end{figure}
\end{document}
我对 TikZ 的了解已经耗尽。请问有人能告诉我如何才能让我的图表看起来像上面的示例一样漂亮吗?谢谢!
答案1
请具体询问您想要什么、您尝试过什么以及您未能实现哪些目标。就目前的情况而言,这对本网站来说没有任何研究价值,只是一篇“为我做”的帖子。
我修正了我发现的两个主要问题。其余的问题应该可以从您已有的代码中一目了然。
% arara: pdflatex
\documentclass[11pt,a4paper]{report}
\usepackage[USenglish]{babel}
\usepackage[T1]{fontenc}
\usepackage[ansinew]{inputenc}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows,petri}
\tikzset{
place/.style={
circle,
thick,
minimum size=6mm,
draw
},
transitionV/.style={
rectangle,
thick,
fill=black,
minimum height=6mm,
inner xsep=1pt
}
}
\begin{document}
\begin{figure}[h!]
\centering
\begin{tikzpicture}[node distance=1cm and 2cm,>=stealth',bend angle=45,thick]
\node [transitionV,label=above:$q_1$] (q1) {};
\node [place,tokens=1,label=above:$\alpha_{11}(k)$] (p11) [below= of q1] {}
edge[pre,bend right=70] (q1)
edge[post,bend left=70] (q1);
\node [place,tokens=2,label=above:$\alpha_{41}(k)$] (p41) [below right=0.25cm and 2cm of q1] {}
edge[pre] (q1);
\node [transitionV,label=above:$q_4$] (q4) [below right=0.25cm and 2cm of p41] {}
edge[pre] (p41);
\node [place,label=above:$\alpha_{24}(k)$] (p24) [above right=0.25cm and 2cm of q4] {}
edge[pre] (q4);
\node [transitionV,label=above:$q_2$] (q2) [above right=0.25cm and 2cm of p24] {}
edge[pre] (p24);
\node [place,tokens=1,label=above:$\alpha_{22}(k)$] (p22) [below= of q2] {}
edge[pre,bend right=70] (q2)
edge[post,bend left=70] (q2);
\node [place,tokens=1,label=above:$\alpha_{31}(k)$] (p31) [above right=0.25cm and 2cm of q1] {}
edge[pre] (q1);
\node [transitionV,label=below:$q_3$] (q3) [above right=0.25cm and 2cm of p31] {}
edge[pre] (p31);
\node [place,label=above:$\alpha_{23}(k)$] (p23) [below right=0.25cm and 2cm of q3] {}
edge[pre] (q3)
edge[post] (q2);
\node [place,tokens=1,label=below:$\alpha_{12}(k)$] (p12) [above= of q3] {}
edge[pre,out=0,in=20,,looseness=1.2] (q2)
edge[post,out=180,in=160,looseness=1.2] (q1);
\end{tikzpicture}
\caption{Event graph}
\end{figure}
\end{document}
答案2
我尝试修改您的代码,将节点与路径分开,并使用arc
路径在侧面绘制较小的节点。这是因为我认为组织得更好的代码更容易理解,每次我编写/完成一些代码时,我都会花几分钟来修复其结构并删除多余的选项。
线条使用锚点连接不同的路径,在这种情况下,我必须添加calc
才能计算其他位置才能实现这一点。例如,如果您有(q2.west)
和(q2.south west)
,为了计算中间点,您可以写($(q2.west)!.5!(q2.south west)$)
,这样您的箭头就可以连接到某个节点的西边和西南边之间的中间点。
所有路径也已根据您的期望缩短。bending
还添加了库,以便正确对齐弧形箭头上的尖端。
输出
代码
\documentclass[11pt,a4paper]{report}
\usepackage[USenglish]{babel}
\usepackage[T1]{fontenc}
\usepackage[ansinew]{inputenc}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,backgrounds,arrows,shapes,automata,petri,calc,bending}
\tikzset{
place/.style={
circle,
thick,
minimum size=6mm,
draw
},
transitionV/.style={
rectangle,
thick,
fill=black,
minimum height=6mm,
inner xsep=1pt
}
}
\begin{document}
\title{Test}
\author{Me}
\date{\today}
\begin{figure}[h!]
\centering
\begin{tikzpicture}[
node distance=1cm and 2cm,
>=stealth',
bend angle=#1,
thick,
shorten >=2pt,
shorten <=2pt
]
% Nodes
\node [transitionV,label=above:$q_1$] (q1) {};
\node [place,tokens=1,label=above:$\alpha_{11}(k)$] (p11) [below= of q1] {};
\node [place,tokens=2,label=above:$\alpha_{41}(k)$] (p41) [below right=0.25cm and 2cm of q1] {};
\node [transitionV,label=above:$q_4$] (q4) [below right=0.25cm and 2cm of p41] {};
\node [place,label=above:$\alpha_{24}(k)$] (p24) [above right=0.25cm and 2cm of q4] {};
\node [transitionV,label=above:$q_2$] (q2) [above right=0.25cm and 2cm of p24] {};
\node [place,tokens=1,label=above:$\alpha_{22}(k)$] (p22) [below= of q2] {};
\node [place,tokens=1,label=above:$\alpha_{31}(k)$] (p31) [above right=0.25cm and 2cm of q1] {};
\node [transitionV,label=below:$q_3$] (q3) [above right=0.25cm and 2cm of p31] {};
\node [place,label=above:$\alpha_{23}(k)$] (p23) [below right=0.25cm and 2cm of q3] {};
\node [place,tokens=1,label=below:$\alpha_{12}(k)$] (p12) [above= of q3] {};
% Paths
% left
\draw ($(q1.north east)!.5!(q1.east)$) edge[bend left=15,->] (p31);
\draw (q1.east) edge[bend right=15,->] (p41);
% central
\draw (p31) edge[bend left=10,->] (q3);
\draw (q3) edge[bend left=10,->] (p23);
\draw (p41) edge[bend right=10,->] (q4);
\draw (q4) edge[bend right=10,->] (p24);
% right
\draw (p23) edge[bend left=15,->] ($(q2.north west)!.5!(q2.west)$);
\draw (p24) edge[bend right=15,->] (q2.west);
% round arrows on the...
% ...left
\draw[->] ($(p11.west)+(2pt,4pt)$) arc (240:105:1.8em);
\draw[<-] ($(p11.east)+(-2pt,4pt)$) arc (-60:75:1.8em);
% ...right
\draw[->] ($(p22.west)+(2pt,4pt)$) arc (240:105:1.8em);
\draw[<-] ($(p22.east)+(-2pt,4pt)$) arc (-60:75:1.8em);
% huge round arrows from the top node
\draw[->] (p12) edge[out=180,in=145] ($(q1.north west)!.5!(q1.west)$);
\draw[->] (p12) edge[out=0,in=35] ($(q2.north east)!.5!(q2.east)$);
\end{tikzpicture}
\caption{Event graph}
\end{figure}
\end{document}