Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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} 
share|improve this question

1 Answer 1

up vote 9 down vote accepted

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

share|improve this answer
2  
Any reason not to just use \tikzmark which sort of packages this kind of solution? –  Peter Grill Dec 20 '12 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. –  Qrrbrbirlbel Dec 20 '12 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. –  Peter Grill Dec 20 '12 at 22:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.