[Tex/LaTex] tikz inside lstlisting inside tikz

listingstikz-pgf

Here is the minimum working example:

\documentclass{article}
\usepackage{tikz}
\usepackage{float}
\usepackage{listings}
\usepackage{color}
\usepackage{textcomp}
\usetikzlibrary{shapes,positioning}
\begin{document}
\centering
\begin{tikzpicture}
  \node[draw] (PersonLocation Graph) {
    \begin{minipage}{.85\linewidth}
\begin{lstlisting}[language=XML,escapechar=!]
  <collection name=PersonLocation BiMap>
  <edge> 
  <ref>//collection[name=Locations]/loc[name=XXYY]
  ! \tikz \node[shape=circle,draw](srchead) {H};!
  </ref>
  <ref>//collection[name=Persons]/person[id=4564]
  ! \tikz \node[shape=circle,draw](deshead) {E};
     %(srchead) edge[<->, bend left] (deshead.south); 
  ! 

  </ref>

  </edge>
  ...
  ...
  ...
  </collection>
\end{lstlisting}
\end{minipage}

};
\end{tikzpicture}
\end{document}

I want to draw an edge from node H to E. I tried using statement srchead) edge[<->, bend left] (deshead.south); (commented out in the code). Thanks in advance for your input.

enter image description here

Best Answer

Instead of using nested tikzpictures, I would suggest you another approach using a variant of the ubiquitous \tikzmark; first you place the nodes, and then you draw the path:

\documentclass{article}
\usepackage{tikz}
\usepackage{float}
\usepackage{listings}
\usepackage{color}
\usepackage{textcomp}
\usetikzlibrary{shapes,positioning}

\newcommand\tikzmark[2]{%
  \tikz[remember picture,overlay]\node[circle,draw] (#1) {#2};}

\begin{document}

\begin{lstlisting}[
  language=XML,
  escapechar=!,
  linewidth=.85\textwidth,
  frame=tblr
]
  <collection name=PersonLocation BiMap>
  <edge> 
  <ref>//collection[name=Locations]/loc[name=XXYY]
  !\tikzmark{srchead}{H}!

  </ref>
  <ref>//collection[name=Persons]/person[id=4564]
  !\tikzmark{deshead}{E}! 

   </ref>

  </edge>
  ...
  ...
  ...
  </collection>
\end{lstlisting}
\begin{tikzpicture}[remember picture,overlay]
\path (srchead) edge[<->, bend right] (deshead.south);
\end{tikzpicture}

\end{document}

enter image description here

The frame is now obtained with the option frame=tblr, and the width is controlled with linewidth=o.85\textwidth.