4

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.

3

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.

  • I thought remember picture was used if you want to define coordinates (or nodes) which you want to reuse later. So why do I need it for the edge connecting the two previously defined nodes? – Misch May 3 '14 at 14:26
  • @Misch You need to add the remember picture option to all pictures that contain nodes that you wish to reference and also to all pictures from which you wish to reference a node in another picture. – Gonzalo Medina May 3 '14 at 14:44
  • Thanks, that explains a lot. Additionally I just found a better way to prevent nested tikzpictures: subnodes – Misch May 3 '14 at 14:47
  • For some reason I can get this to work with pdflatex, but not with latex. With the latter the two boxes are not connected and a small segment is drawn in the middle of the page. – adl Jul 26 '14 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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