2

How can I draw an arrow between two nodes that are defined in a pic please. For example, how can I draw an edge from node A in the left hand side to the node A in the right hand side? Thank you.

enter image description here

My code:

\documentclass[border=10pt]{standalone}    
\usepackage{tikz}
\usetikzlibrary{arrows, calc}
\begin{document}
\tikzset{
arrow/.style={draw, -latex},
mypic/.pic = {  
  \node[draw] (a) {A};
  \node[draw, below of=a] (b) {B};}
}
\begin{tikzpicture}
  \pic at (0,0) {mypic};
  \pic at (1,1) {mypic}; 
  \draw[arrow] (a.north east)--(a.south west); % unsurprisingly this doesn't work
\end{tikzpicture}
\end{document}
user20650
  • 339

1 Answers1

2

You overwrite the node a. You need to give it unique names, e.g. with.

\documentclass[border=10pt]{standalone}    
\usepackage{tikz}
\usetikzlibrary{arrows, calc}
\begin{document}
\tikzset{
arrow/.style={draw, -latex},
mypic/.pic = {  
  \node[draw] (#1-a) {A};
  \node[draw, below of=#1-a] (#1-b) {B};}
}
\begin{tikzpicture}
  \path (0,0) pic {mypic=L};
  \path (1,1) pic {mypic=R}; 
  \draw[arrow] (L-a.north east)--(R-a.south west); 
\end{tikzpicture}
\end{document}

enter image description here

In principle you could also use the names of the pics, see the example on top of p. 263 of the pgf manual. However, in practice difficulties arise, so I decided to go the way above.