这是问题的后续TikZ 中节点连接不正确 。
按照该问题中接受的答案所建议的方法,当我使用原始代码中的逻辑时,输出并不相同。
我得到了这样的信息:
代码:
\documentclass{article}
\usepackage{tikz}
\tikzset{square/.style = {
shape = rectangle,
fill = gray!50,
draw = black,
thick
}}
\tikzset{circle/.style = {
shape = circle,
fill = blue!20,
draw = blue,
thick
}}
\tikzset{help lines/.style=very thin}
\tikzset{My Grid/.style={help lines,color=blue!50}}
\begin{document}
\begin{tikzpicture}
%\draw[My Grid] (-5,-5) grid (15,15);
\draw[square] (-4,4)rectangle node (r1) {r1} (-3,5);
\draw[square] (1,4) rectangle node (r2) {r2} (2,5);
\draw[square] (-3,0) rectangle node (r3) {r3} (-4,1);
\draw[square] (1,0) rectangle node (r4) {r4} (2,1);
\draw[circle] (-1,2.5) circle [radius=0.5cm] node[above=2ex,red] (s) {$s \leq 3$};
\draw[circle] (-1,4.5) circle [radius=0.5cm] node (s1) {s1};
\draw[circle] (-1,6.5) circle [radius=0.5cm] node (s2) {};
\draw[fill=black] (-1,6.5) circle [radius=0.1cm] node (s3) {};
\draw[circle] (-1,0.5) circle [radius=0.5cm] node (s4) {s4};
\draw[circle] (-1,-1.5) circle [radius=0.5cm] node (s5) {};
\draw[fill=black] (-1,-1.5) circle [radius=0.1cm];
\draw[->] (r1) -- (s1);
\end{tikzpicture}
\end{document}
答案1
直接将元素定义为\node
。这样,您就可以在元素上定义锚点。如果我使用部分代码展示这种方式:
\documentclass{article}
\usepackage{tikz}
\tikzset{square/.style = {
shape = rectangle,
fill = gray!50,
draw = black,
minimum height=1cm,
minimum width=1cm,
thick
}}
\tikzset{circle/.style = {
shape = circle,
fill = blue!20,
draw = blue,
minimum size=1cm,
inner sep=0pt,
thick
}}
\tikzset{help lines/.style=very thin}
\tikzset{My Grid/.style={help lines,color=blue!50}}
\begin{document}
\begin{tikzpicture}
%\draw[My Grid] (-5,-5) grid (15,15);
\node (r1) at (-3.5,4.5) [square] {r1};
\node (s1)at (-1,4.5) [circle]{s1};
\draw[->] (r1) -- (s1);
\end{tikzpicture}
\hfill
\begin{tikzpicture}[xshift=2cm]
%\draw[My Grid] (-5,-5) grid (15,15);
\node (r1) at (-3.5,4.5) [square] {r1};
\node (s1)at (-1,4.5) [circle]{s1};
\draw[->] (r1.east) -- (s1.west);
\end{tikzpicture}
\hfill
\begin{tikzpicture}[xshift=2cm]
%\draw[My Grid] (-5,-5) grid (15,15);
\node (r1) at (-3.5,4.5) [square] {r1};
\node (s1)at (-1,4.5) [circle]{s1};
\draw[->] (r1.south east) -- (s1.180);
\draw[->] (r1.north east) -- (s1.north);
\end{tikzpicture}
\end{document}
答案2
您要创建的图形与教程中创建的图形完全相同手册中的 Hagen 的 Petri 网第 49 至 60 页的 3.1.1。
我邀请你阅读它,它写得非常好而且非常完整。
以下是第 52 页给出的手册图的完整代码:
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}
\begin{tikzpicture}[place/.style={circle,draw=blue!50,fill=blue!20,thick,
inner sep=0pt,minimum size=6mm},
transition/.style={rectangle,draw=black!50,fill=black!20,thick,
inner sep=0pt,minimum size=4mm}]
\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] {};
\draw [->] (enter critical.east) -- (critical.west);
\draw [->] (waiting.west) .. controls +(left:5mm) and +(up:5mm)
.. (enter critical.north);
\end{tikzpicture}
\end{document}
这是本教程结束时构建的最终图形。