4

I have a problem when I try to combine listings, tikz and beamer packages.

What I want : 1. be able to put some "mark points in my source code"; 2. be able to draw an arrow between them;

I tried to do this : How can I draw arrows within a listing to explain positional association? but it doesn't work for me :'(

here's my minimum source code :

\documentclass{beamer}

\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage[utf8]{inputenc}

\usepackage{listings} %http://texdoc.net/texmf-dist/doc/latex/listings/listings.pdf
\lstset{language=C++}

%https://tex.stackexchange.com/questions/204417/how-can-i-draw-arrows-within-a-listing-to-explain-positional-association
\usepackage{tikz}
\usetikzlibrary{decorations.text,calc}
\newcommand{\tikzmark}[2]{%
   \tikz[overlay,remember picture] \node[text=black,
       inner sep=2pt] (#1) {#2};}

\begin{document}
 \begin{frame}[fragile]{}
 \begin{lstlisting}[escapeinside={(*@}{@*)}]
   for([e1]; [e2]; [e3])
   {
     instruction(s);
     if (condition) break;(*@\tikzmark{aBeg}{}@*)
     instruction(s);(*@\tikzmark{aEnd}{}@*)
   }
   (*@
    \begin{tikzpicture}[overlay,remember picture]
     \node (Start) at (aBeg.{east}) {};
     \node  (Ending) at (aEnd.{north west}) {};
     %\draw Start -- Ending;
      \end{tikzpicture}
    *@) 
 \end{lstlisting}

\end{frame}
\end{document}

Thanks a lot in advance.

3

Two problems:

  1. *@) is not what you defined as closing character

  2. missing () around the nodes.


\documentclass{beamer}

\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage[utf8]{inputenc}

\usepackage{listings} %http://texdoc.net/texmf-dist/doc/latex/listings/listings.pdf
\lstset{language=C++}

%http://tex.stackexchange.com/questions/204417/how-can-i-draw-arrows-within-a-listing-to-explain-positional-association
\usepackage{tikz}
\usetikzlibrary{decorations.text,calc}
\newcommand{\tikzmark}[2]{%
   \tikz[overlay,remember picture] \node[text=black,
       inner sep=2pt] (#1) {#2};}

\begin{document}
 \begin{frame}[fragile]{}
 \begin{lstlisting}[escapeinside={(*@}{@*)}]
   for([e1]; [e2]; [e3])
   {
     instruction(s);
     if (condition) break;(*@\tikzmark{aBeg}{}@*)
     instruction(s);(*@\tikzmark{aEnd}{}@*)
   }
   (*@
    \begin{tikzpicture}[overlay,remember picture]
     \node (Start) at (aBeg.{east}) {};
     \node  (Ending) at (aEnd.{north west}) {};
     \draw[->] (Start) -- (Ending);
      \end{tikzpicture}
    @*) 
 \end{lstlisting}

\end{frame}
\end{document}

enter image description here

|improve this answer|||||

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.