按照以下示例如何让这个 TikZ 图片更加符合地道?(环境图)
我尝试使用预定义的“pic”在画布上放置更多“函数定义图标”,使用相对定位(right=of
和base right=of
)。但是,两个圆形节点内的 Petri 网标记有轻微位移。
尽管可能可以用微小的填充节点本身替换标记,但是,是否有办法调试 TikZ-petri 网中的标记定位?
以下是“几乎最小”的例子。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,petri,arrows}
\begin{document}
\begin{tikzpicture}[inner sep=0mm,>=stealth',very thick,color=black!50,
font=\sffamily,pics/two dots/.style={code={
\node [draw,minimum size=5mm,circle,colored tokens={black!50}]
(#1-left) {};
\node [draw,minimum size=5mm,circle,colored tokens={black!50},
right=0pt of #1-left]
(#1-right) {};
\node [rectangle, fit=(#1-left) (#1-right)] (#1){};
\node [circle,fill=red,minimum size=1mm] at (#1-left) {};
\node [circle,fill=red,minimum size=1mm] at (#1-right) {};
}},
every pin edge/.style={<-,very thick},
box/.style={draw,rectangle,inner sep=#1},box/.default=2mm]
\node (W1) {factorial:};
\node (fake1) [right=190mm of W1] {};
\node (fact-iter) [above=1mm of W1] {fact-iter:};
%
\node (g env) [box, fit=(W1) (fake1) (fact-iter),
pin={[text width=1cm,pin distance=10mm]left:global env}]
{ };
% factorial
\path ([yshift=-20mm]W1.east)pic{two dots=w1fun}
(w1fun) edge [<-, to path={|- (\tikztotarget)}] (W1.east);
\node [below=of w1fun-left, align=left] {\ttfamily parameters: n \\ body: \dots}
edge [<-] (w1fun-left.center);
\draw[->] (w1fun-right.center) -| ([xshift=3mm] w1fun-right.east |- g env.south);
% fact-iter
\path pic[base right=20mm of w1fun.east]{two dots=fact-iter-fun}
(fact-iter-fun) edge [<-, to path={|- (\tikztotarget)}] (fact-iter.east);
\node [below=of fact-iter-fun-left, align=left] {\ttfamily parameters: todo \\ body: \dots}
edge [<-] (fact-iter-fun-left.center);
\draw[->] (fact-iter-fun-right.center) -| ([xshift=3mm] fact-iter-fun-right.east |- g env.south);
\end{tikzpicture}
\end{document}
更新:为了理解定位,我添加了微小的调试节点,但结果并不好。圆圈、标记和调试节点的中心似乎都不同。该怎么办?
答案1
positioning
不幸的是,同名库的技术不适用于图片。您想要应用于图片的 pgf 键实际上会以这样的方式应用于节点,即它们想要使用左西锚点进行定位,因此需要额外的偏移。但对于您来说,有一个非常简单的解决方法:引入一个用于定位的辅助坐标。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,petri,arrows}
\begin{document}
\begin{tikzpicture}[inner sep=0mm,>=stealth',very thick,color=black!50,
font=\sffamily,pics/two dots/.style={code={
\node [draw,minimum size=5mm,circle,colored tokens={black!50}]
(#1-left) {};
\node [draw,minimum size=5mm,circle,colored tokens={black!50},
right=0pt of #1-left]
(#1-right) {};
\node [rectangle, fit=(#1-left) (#1-right)] (#1){};
}},
every pin edge/.style={<-,very thick},
box/.style={draw,rectangle,inner sep=#1},box/.default=2mm]
\node (W1) {factorial:};
\node (fake1) [right=190mm of W1] {};
\node (fact-iter) [above=1mm of W1] {fact-iter:};
%
\node (g env) [box, fit=(W1) (fake1) (fact-iter),
pin={[text width=1cm,pin distance=10mm]left:global env}]
{ };
% factorial
\path ([yshift=-20mm]W1.east)pic{two dots=w1fun}
(w1fun) edge [<-, to path={|- (\tikztotarget)}] (W1.east);
\node [below=of w1fun-left, align=left,draw=red, rectangle, very thin] {parameters: d \\ body: \dots}
edge [<-] (w1fun-left.center);
\draw[->] (w1fun-right.center) -| ([xshift=3mm] w1fun-right.east |- g env.south);
% fact-iter
\path coordinate[base right=15mm of w1fun.east] (aux)
(aux) pic{two dots=fact-iter-fun}
(fact-iter-fun) edge [<-, to path={|- (\tikztotarget)}] (fact-iter.east);
\node [below=of fact-iter-fun-left, align=left,draw=red, very thin,rectangle] {parameters: todo \\ body: \dots}
edge [<-] (fact-iter-fun-left.center);
\draw[->] (fact-iter-fun-right.center) -| ([xshift=3mm] fact-iter-fun-right.east |- g env.south);
\end{tikzpicture}
\end{document}
您可能需要增加距离。