描述
我正在制作一个图表,并使用装饰标记在圆形节点之间制作方形节点。这一直运行良好,直到我想在 T_t 和 I_t 之间(靠近顶部)添加装饰。正方形偏离中心,我不知道为什么,它使用与其他所有正方形相同的装饰代码。
那么问题是为什么?我该如何解决它?我确信第一个问题的答案将是解决第二个问题的一大步。
示例图像
生成图像的代码
\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}[rotate=#2, transform shape]
\osnode[]{$Y_{r_{#1}}$}{-Yr}
\pnode[above right= of -Yr]{-f1}
\snode[below right = of -f1]{$X_{r_{#1}}$}{-Xr}
\snode[above = of -f1]{$X_{c_{#1}}$}{-Xc}
\snode[above = 20mm of -Xc]{$C_{{#1}}$}{-C}
\draw[-] (-Yr)--(-f1);
\draw[-] (-Xr)--(-f1);
\draw[-] (-Xc)--(-f1);
\draw[midblock={name=-XctoC}] (-Xc)--(-C);
\end{scope}
}
}
}
\tikzset{
pics/object2/.style 2 args={
code={
\begin{scope}[rotate=#2, transform shape]
\pnode[]{-f2}
\snode[above left=of -f2]{$T_{#1}$}{-Tt}
\snode[above right = of -f2]{$I_{#1}$}{-It}
\draw[-] (-Tt)--(-f2);
\draw[-] (-It)--(-f2);
\draw[midblock={name=testing}] (-Tt)--(-It);
\end{scope}
}
}
}
\begin{document}
\begin{tikzpicture}[remember picture]
\path pic (t1) {object1={t}{0}};
\pic [above= of t1-C] (c1) {object2={t}{0}};
\draw[-] (t1-C)--(c1-f2);
\end{tikzpicture}
\hspace{2 cm}
\begin{tikzpicture}[remember picture]
\path pic (t2) {object1={t+1}{0}};
\pic [above= of t2-C] (c2) {object2={t+1}{0}};
\draw[-] (t2-C)--(c2-f2);
\end{tikzpicture}
\begin{tikzpicture}[remember picture,overlay,every text node part/.style={align=center}] % text node stuff for multiline text
%%% Draw some edges
\draw[midblock={name=XtoX}] (t1-Xr) to [out=310,in=230] (t2-Xr);
\draw[-] (c1-It) to [out=345,in=135] (XtoX);
\draw[midblock={name=TtoT}] (c1-Tt) to [out=50,in=130] (c2-Tt);
\draw[midblock={name=ItoI}] (c1-It) to [out=50,in=130] (c2-It);
\end{tikzpicture}
\end{document}
答案1
编辑答案:
经过仔细检查,我发现这只影响了object2
,其定义与 几乎相同object1
。唯一的区别是,当object1
在实际 中使用时tikzpicture
,不会将任何选项传递给\pic
命令。另一方面,\pic
的命令有选项。这似乎被传递给,使得所有s 都出现在绘制它们的路径上方(路径显示为红色)。object2
[above= of t1-C]
above
object2
midblock
在下图中,我先将左侧命名为node
,然后将pic
包含内容放置object2
在 处,从而对左侧进行了更正node
。右侧使用原始名称\pic [above= of ...]
来显示差异。我还添加了更多midblock
来说明它们的位置。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,fit,decorations.markings,patterns}
\def\pnode [#1]#2{
% node for the potential function
\node[regular polygon,regular polygon sides=4, minimum size=5.0pt,fill=black,#1] (#2) {};
%\node[circle, minimum size=5.0pt,fill=red,#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}[rotate=#2, transform shape]
\osnode[]{$Y_{r_{#1}}$}{-Yr}
\pnode[above right= of -Yr]{-f1}
\snode[below right = of -f1]{$X_{r_{#1}}$}{-Xr}
\snode[above = of -f1]{$X_{c_{#1}}$}{-Xc}
\snode[above = 20mm of -Xc]{$C_{{#1}}$}{-C}
\draw[-] (-Yr)--(-f1);
\draw[-] (-Xr)--(-f1);
\draw[-] (-Xc)--(-f1);
\draw[midblock={name=-XctoC}] (-Xc)--(-C);
\end{scope}
}
}
}
\tikzset{
pics/object2/.style 2 args={
code={
\begin{scope}[rotate=#2, transform shape]
\pnode[]{-f2}
\snode[above left=of -f2]{$T_{#1}$}{-Tt}
\snode[above right=of -f2]{$I_{#1}$}{-It}
\draw[-,red,midblock={name=foo}] (-Tt)--(-f2);
\draw[-,red,midblock={name=bar}] (-It)--(-f2);
\draw[-,red,midblock={name=testing}] (-Tt)--(-It);
\end{scope}
}
}
}
\begin{document}
\begin{tikzpicture}[remember picture]
\path pic (t1) {object1={t}{0}};
%\pic [above= of t1-C] (c1) {object2={t}{0}};
\node[above=of t1-C] (n1){};
\pic(c1) at (n1) {object2={t}{0}};
\draw[-] (t1-C)--(c1-f2);
\end{tikzpicture}
\hspace{2 cm}
\begin{tikzpicture}[remember picture]
\path pic (t2) {object1={t+1}{0}};
\pic [above= of t2-C] (c2) {object2={t+1}{0}};
\draw[-] (t2-C)--(c2-f2);
\end{tikzpicture}
\begin{tikzpicture}[remember picture,overlay,every text node part/.style={align=center}] % text node stuff for multiline text
%%% Draw some edges
\draw[midblock={name=XtoX}] (t1-Xr) to [out=310,in=230] (t2-Xr);
\draw[-] (c1-It) to [out=345,in=135] (XtoX);
\draw[midblock={name=TtoT}] (c1-Tt) to [out=50,in=130] (c2-Tt);
\draw[midblock={name=ItoI}] (c1-It) to [out=50,in=130] (c2-It);
\end{tikzpicture}
\end{document}