[Tex/LaTex] Overlay edge between two tikz nodes

nodestikz-pgf

I want to have two nodes in Tikz, filled with text. Then I want an edge from one position in the first node to another position in the second node:

What I want

This is what I tried: Inside the tikz nodes text, I use another nested tikzpicture with remember picture, where I define coordinates, which should later be reusable as edge start end end points. (Here I used filled nodes instead for debugging)

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{frame}{Tikz Test}
    \begin{tikzpicture}
        \draw node [text width=60mm,fill=black!5] (n1) {Multi\\line \begin{tikzpicture}[remember picture]\node[fill=black,text width=2mm](start){};\end{tikzpicture} \\text};
        \draw node [text width=60mm,fill=black!5,below=of n1] {Another\\multi\\line \begin{tikzpicture}[remember picture]\node[fill=black,text width=2mm](end){};\end{tikzpicture}\\text};
    \end{tikzpicture}
    \begin{tikzpicture}[overlay]
        \draw (start) -- (end);
    \end{tikzpicture}
\end{frame}

\end{document}

This is what I get:

What I get

The nodes (black rectangles) are where they should be, but the edge connecting them is never where I want it to be. I tried multiple variations, (trying different places where I define the edge, and whether I use the overlay attribute or not), but neither version yields the desired output.

Best Answer

You need remember picture for the tikzpicture connecting the nodes:

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{frame}{Tikz Test}


\begin{tikzpicture}[remember picture]
\draw node [text width=60mm,fill=black!5] (n1) 
  {Multi\\line \begin{tikzpicture}[remember picture]
\node[fill=black,text width=2mm](start){};\end{tikzpicture} \\text};
\draw node [text width=60mm,fill=black!5,below=of n1] {Another\\multi\\line \begin{tikzpicture}[remember picture]\node[fill=black,text width=2mm](end){};\end{tikzpicture}\\text};
    \end{tikzpicture}
\begin{tikzpicture}[remember picture,overlay]
        \draw (start) -- (end);
\end{tikzpicture}
\end{frame}

\end{document}

enter image description here

By the way, is best not to nest tikzpictures.

Related Question