列表和 Tikz 的问题

列表和 Tikz 的问题

当我尝试合并 listings、tikz 和 beamer 包时遇到了问题。

我想要的:1. 能够在我的源代码中放置一些“标记点”;2. 能够在它们之间画一个箭头;

我尝试这样做:如何在列表中画箭头来解释位置关联?但它对我不起作用:'(

这是我的最低源代码:

\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}

提前致谢。

答案1

两个问题:

  1. *@)不是您定义的结束字符

  2. ()节点周围缺失。


\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}

在此处输入图片描述

相关内容