下面的代码可以在两个圆圈之间画一条线吗?
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\begin{document}
\tikzset{
crcl/.style={draw, thick, shape=circle }
}
\newcommand*{\MyPic}[2]{
\begin{tikzpicture}
\draw #1 rectangle #2;
\node[crcl] at #1 (c) {};
\end{tikzpicture}
}
\begin{tikzpicture}
\node[fill=blue] at (0,0) (a) {\MyPic{(0,0)}{(1,1)}};
\node[fill=green] [xshift=2cm] at (0,0) (b) {\MyPic{(0,0)}{(1,1)}};
\draw[thick] (a) -- (b);
\end{tikzpicture}
\end{document}
答案1
这与关键且独特的节点名称有关remember picture
。
这至少需要两次编译。
另外,我\MyPic
以更 TikZ 的方式重新定义:
\MyPic (<p1>) (<p2>) {<name>}
代码
\documentclass[tikz]{standalone}
\tikzset{
crcl/.style={draw, thick, shape=circle }
}
\newcommand*{\MyPic}[3]{%
\begin{tikzpicture}[remember picture]
\draw #1 rectangle #2;
\node[crcl] at #1 (c#3) {};
\end{tikzpicture}%
}
\def\MyPic(#1)#2(#3)#4{% remove this if new syntax is not wanted
\begin{tikzpicture}[remember picture] %
\draw (#1) rectangle (#3); %
\node[crcl] at (#1) (c#4) {}; %
\end{tikzpicture}% %
}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{tikzpicture}[remember picture]
\node[fill=blue] at (0,0) (a) {\MyPic (0,0) (1,1) {a}};
\node[fill=green, xshift=2cm] at (0,0) (b) {\MyPic (0,0) (1,1) {b}};
\draw[thick] (ca) -- (cb);
\end{tikzpicture}
\end{document}