6

Is it possible to draw a line between two circles in the below code?

\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 Answer 1

9

This works with the remember picture key and distinctive node names.

This needs at least two compilations.

As an extra, I redefined \MyPic in a more TikZ-way:

\MyPic (<p1>) (<p2>) {<name>}

Code

\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}

Output

enter image description here

3
  • 2
    Any reason not to just use \tikzmark which sort of packages this kind of solution? Commented Dec 20, 2012 at 19:45
  • 1
    @PeterGrill Now that you say it, I have tried it: The best I got was an overlayed circle (the bounding box (blue/green) didn’t got updated). The \subnode command sounds promising, though the closest I got was \subnode{salt-#3}{\node[crcl] at #1 (c#3) {};} where I had to use again (ca) and (cb). salt-#3 gives only the center (#1). I want the node “marked” not the point. Commented Dec 20, 2012 at 20:06
  • Now that I look at it more carefully I guess you'd have to do a custom \tikzmark which places the node. Commented Dec 20, 2012 at 22:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .