描述
这个问题源于为什么装饰标记偏离中心?我们发现问题所在,但没有为什么这是首先发生的。
似乎在放置物体时o2
多于图片o1
选项above
被传递给物体中的装饰o2
。这是假设,因为当我们放置物体时o2_2
在位于上方的节点o1_2
,则装饰将按预期绘制。
示例图像
半最小代码
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,fit,decorations.markings,patterns}
\pagenumbering{gobble} % remove page numbering to get good cropping when using pdfcrop
\def\pnode [#1]#2{
% node for the potential function
\node[regular polygon,regular polygon sides=4, minimum size=5.0pt,fill=black,#1] (#2) {};
}
\def\snode [#1]#2#3{
% node for the state variables
\node[circle, minimum size=35.0pt , fill=lightgray,line width=0.625pt, draw=black,#1](#3){#2};
}
\def\osnode [#1]#2#3{
% for for observed state variables
\node[circle, minimum size=35.0pt , fill={rgb:red,1;green,2;blue,3},line width=0.625pt, draw=black,#1](#3){#2};
}
\tikzset{
set midblock/.code={\pgfqkeys{/tikz/midblock}{#1}},
set midblock={name/.initial=b1},
midblock/.style={
set midblock={#1},
postaction={
decorate,
decoration={
markings,
mark=at position .5 with {\pnode[]{\pgfkeysvalueof{/tikz/midblock/name}}}}
}
}
}
\tikzset{
pics/object1/.style 2 args={
code={
\begin{scope}
\snode[]{obj-1}{-i};
\end{scope}
}
}
}
\tikzset{
pics/object2/.style 2 args={
code={
\begin{scope}[rotate=#2, transform shape]
\snode[]{obj-2}{-i};
\snode[right = of -i]{j-txt}{-j};
\snode[above = of -i]{k-txt}{-k};
\snode[left = of -i]{l-txt}{-l};
\draw[midblock={name=foo}] (-i)--(-j);
\draw[midblock={name=baz}] (-j)--(-k);
\draw[midblock={name=bar}] (-k)--(-l);
\draw[midblock={name=bar}] (-l)--(-i);
\end{scope}
}
}
}
\begin{document}
\begin{tikzpicture}
% Broken Code ... but why?
\path pic (o1) {object1};
\pic [above= of o1-i] (o2) {object2={}{0}};
\end{tikzpicture}
\hspace {2cm}
\begin{tikzpicture}
% This code works
\path pic (o1_2) {object1};
\snode[above=of o1_2-i]{}{n1};
\pic (o2_2) at (n1) {object2={}{0}};
\end{tikzpicture}
\end{document}
******编辑******
根据要求提供一个更简约的例子。
图像
代码
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,fit,decorations.markings,patterns}
\pagenumbering{gobble} % remove page numbering to get good cropping when using pdfcrop
\def\pnode [#1]#2{
% node for the potential function
\node[regular polygon,regular polygon sides=4, minimum size=5.0pt,fill=black,#1] (#2) {};
}
\def\snode [#1]#2#3{
% node for the state variables
\node[circle, minimum size=35.0pt , fill=lightgray,line width=0.625pt, draw=black,#1](#3){#2};
}
\tikzset{
set midblock/.code={\pgfqkeys{/tikz/midblock}{#1}},
set midblock={name/.initial=b1},
midblock/.style={
set midblock={#1},
postaction={
decorate,
decoration={
markings,
mark=at position .5 with {\pnode[]{\pgfkeysvalueof{/tikz/midblock/name}}}}
}
}
}
\tikzset{
pics/object1/.style 2 args={
code={
\begin{scope}
\snode[]{obj-1}{-i};
\end{scope}
}
}
}
\tikzset{
pics/object2/.style 2 args={
code={
\begin{scope}[rotate=#2, transform shape]
\snode[]{obj-2}{-i};
\snode[right = of -i]{j-txt}{-j};
\draw[midblock={name=foo}] (-i)--(-j);
\end{scope}
}
}
}
\begin{document}
\begin{tikzpicture}
% Broken Code ... but why?
\path pic (o1) {object1};
\pic [above= of o1-i] (o2) {object2={}{0}};
\end{tikzpicture}
\hspace {2cm}
\begin{tikzpicture}
% This code works
\path pic (o1_2) {object1};
\snode[above=of o1_2-i]{}{n1};
\pic (o2_2) at (n1) {object2={}{0}};
\end{tikzpicture}
\end{document}
答案1
\pic[above= of o1-i]...
→ \path pic[above= of o1-i]...
→ node[above= of o1-i]...
→ \tikzset{above= of o1-i}
→\def\tikz@anchor{south}
记住这一点,你的问题可以通过为 指定显式锚点来解决\pnode
。也就是说,你只需要替换
\pnode[]
经过
\pnode[anchor=center]
一切按预期进行。