TikZ 中的“on grid”起什么作用?

TikZ 中的“on grid”起什么作用?

在 TikZ 手册第 60/1318 页上有一个绘制 Petri 网的示例,如下所示:

在此处输入图片描述

下面是我的代码:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary {arrows.meta,backgrounds,fit,positioning,petri}
\begin{document}

\begin{tikzpicture}[node distance=1.3cm, on grid,>={Stealth[round]},auto,
every place/.style= {minimum size=6mm,thick,draw=blue!75,fill=blue!20},
every transition/.style={thick,draw=black!75,fill=black!20},
red place/.style= {place,draw=red!75,fill=red!20},
bend angle=45, every label/.style={red},
place/.style={circle,draw=blue!50,fill=blue!20,thick,
inner sep=0pt,minimum size=10 pt},
transition/.style={rectangle,draw=black!50,fill=black!20,thick,
inner sep=0pt,minimum size=10 pt}, bend angle=45,
pre/.style={<-,shorten <=1pt,>={Stealth[round]},semithick},
post/.style={->,shorten >=1pt,>={Stealth[round]},semithick}]

\node [place, tokens=1] (w1) {};
\node [place] (c1) [below=of w1] {};
\node [place] (s) [below=of c1,label=above:$s\le 3$] {};
\node [place] (c2) [below=of s] {};
\node [place,tokens=1] (w2) [below=of c2] {};
\node [transition] (e1) [left=of c1] {}
edge [pre,bend left] (w1)
edge [post,bend right] (s)
edge [post] (c1);
\node [transition] (e2) [left=of c2] {}
edge [pre,bend right] (w2)
edge [post,bend left] (s)
edge [post] (c2);
\node [transition] (l1) [right=of c1] {}
edge [pre] (c1)
edge [pre,bend left] (s)
edge [post,bend right] node[swap] {2} (w1);
\node [transition] (l2) [right=of c2] {}
edge [pre] (c2)
edge [pre,bend right] (s)
edge [post,bend left] node {2} (w2);
\end{tikzpicture}
\end{document}

我不明白“on grid”是什么意思。有什么想法吗?

答案1

根据 tikz 手册(版本 3.1.5b)第 242 页第 17.5.3 节:

/tikz/on grid=⟨boolean⟩(无默认值,初始为 false)当此键设置为 true 时,当前形式的 ⟨of-part⟩ 的行为会有所不同:为当前节点设置的锚点以及用于其他 ⟨node name⟩ 的锚点都设置为中心。这会产生以下效果:当您在将 on grid 设置为 true 的情况下说 above=1cm of somenode 时,新节点将放置在其中心位于 somenode 中心上方 1cm 的位置。以这种方式反复放置节点将导致节点以“网格坐标”为中心,因此该选项的名称由此而来。

因此,on grid使用移位指定的坐标将变为of=...最近的网格坐标。

相关内容