带有箭头的花括号指向具体代码

带有箭头的花括号指向具体代码

我想在代码环境中使用水平括号,如下例所示:在此处输入图片描述

这就是我所知道的一切。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usepackage{listings}
\usepackage{xcolor}


\def\tikzmark#1{\tikz[remember picture,overlay]\node[yshift=2pt](#1){};}

% Command to place a TikZ anchor at the current position
\newcommand{\mytikzmark}[1]{%
  \tikz[overlay,remember picture,baseline] \coordinate (#1) at (0,0) {};}

\newcommand{\highlighty}[2]{%
  \draw[yellow,line width=14pt,opacity=0.4]%
    ([yshift=4pt]#1) -- ([yshift=4pt]#2);%
}

\newcommand{\highlightg}[2]{%
  \draw[green,line width=14pt,opacity=0.4]%
    ([yshift=4pt]#1) -- ([yshift=4pt]#2);%
}


\usepackage{xcolor}


\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
    escapeinside={*}{*},
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

\lstset{style=mystyle}

\begin{document}

\begin{lstlisting}[language=Python,title=\small \textbf{Programm:}~\texttt{L1\_2\_2\_Funktion\_mit\_Parameter}, escapechar=*]
def nutzer_gruessen(*\mytikzmark{hl1Start}*p_vorname*\tikzmark{A}*, p_nachname*\mytikzmark{hl1End}*):
   print("Herzlich willkommen,", p_vorname, p_nachname)

vorname = input("Bitte Vorname eingeben: ")
nachname = input("Bitte Nachname eingeben: ")
nutzer_gruessen(*\mytikzmark{hl2Start}*vorname*\tikzmark{B}*, nachname*\mytikzmark{hl2End}*)
\end{lstlisting}
\tikzset{Empharrow/.style={dashed,red,thick,->,arrows={-Triangle[width=3pt]}}}
\begin{tikzpicture}[remember picture,overlay]
\highlighty{hl1Start}{hl1End}
\highlightg{hl2Start}{hl2End}
%\draw[Empharrow](B)-- ++(0, -3cm)|-(A);
%\draw[Empharrow](B)--+(5.5cm,0)|-(A.south east);
%\draw [Empharrow] (B) |-([shift={(8cm,-3mm)}]B.south east)-- ([shift={(6.5cm,3mm)}]A.north east)-|(A);
\draw [Empharrow] (B) |-([shift={(7cm,-2mm)}]B.south east)-- ([shift={(5.8cm,3mm)}]A.north east)-|(A);
\end{tikzpicture}

\end{document}

输出:

在此处输入图片描述

如何在列表环境中插入水平括号?

我还想知道如何管理突出显示,以便不必为每种新颜色创建新命令。

答案1

我们重新组织一下吧。

在您的代码中,您使用\tmS(start) 和\tmT(target) 来标记和突出显示给定区域。值sttt用于更改矩形的大小。可选参数\tmS应用于,您可以使用它来更改颜色(或任何实际绘图)以及此单独突出显示的\fillst和。tt

这个矩形的所有四个角都用四个坐标标记(这里最好使用一个节点,但这更容易),稍后将使用 和 ,oBraceuBrace又使用样式bracy。这些在括号的尖端添加了进一步的坐标,然后最终与Empharrow键一起使用,这也安装了自定义的to path。不过,这是硬编码的,来自 UnderBrace 并转到 OverBrace。

我正在使用aboveskipabovecaptionskip值,以便列表环境为您的箭头留出更多空间。

代码

\documentclass{article}
\usepackage{listings}
\usepackage{tikz} % loads xcolor
\usetikzlibrary{arrows.meta}
\usetikzlibrary{tikzmark}
\usetikzlibrary{decorations.pathreplacing}
\tikzset{
  st/.initial={-.1em,-.3em}, tt/.initial={.1em,.8em},
  /pgf/decoration/raise/.append code=\pgfkeyssetvalue{/pgf/decoration/raise}{#1},
  bracy/.style={decorate, thick, decoration={name=brace, raise=-.1em}},
  uBrace/.style={
    bracy,
    insert path={
      (#1-br) -- coordinate[midway, yshift=
        -\pgfdecorationsegmentamplitude-\pgfkeysvalueof{/pgf/decoration/raise}] (#1-b)
      (#1-bl)}},
  oBrace/.style={
    bracy,
    insert path={
      (#1-tl) -- coordinate[midway, yshift=
        \pgfdecorationsegmentamplitude+\pgfkeysvalueof{/pgf/decoration/raise}] (#1-t)
      (#1-tr)}},
  Empharrow/.style={
    dashed, red, thick, arrows={-Triangle[width=3pt, length=4pt]},
    shorten >=\pgflinewidth, shorten <=\pgflinewidth,
    to path={-- ++(down:.7em) -- ++ (right:#1) |- ([yshift=.7em]\tikztotarget) -- (\tikztotarget)}
  },
}
\newcommand*\tmS[2][]{%
  \tikz[remember picture,overlay,baseline=0pt]
  \fill[#1](\pgfkeysvalueof{/tikz/st}) coordinate (#2-bl)
    rectangle ([shift={(\pgfkeysvalueof{/tikz/tt})}]pic cs:mytm-#2)
    coordinate (#2-tr) coordinate (#2-tl) at (#2-tr-|#2-bl)
    coordinate (#2-br) at (#2-tr|-#2-bl);%
}
\newcommand*\tmT[1]{\tikzmark{mytm-#1}}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
  escapeinside={*}{*},
  backgroundcolor=\color{backcolour},
  commentstyle=\color{codegreen},
  keywordstyle=\color{magenta},
  numberstyle=\tiny\color{codegray},
  stringstyle=\color{codepurple},
  basicstyle=\ttfamily\footnotesize,
  breakatwhitespace=false,
  breaklines=true,
  captionpos=b,
  keepspaces=true,
  numbers=left,
  numbersep=5pt,
  showspaces=false,
  showstringspaces=false,
  showtabs=false,
  tabsize=2
}
\lstset{style=mystyle}

\begin{document}
\noindent Foo \hrulefill Bar
\begin{lstlisting}[
  language=Python,
  title=\small\textbf{Programm:}~\texttt{L1\_2\_2\_Funktion\_mit\_Parameter},
  aboveskip=\dimexpr.5em+\medskipamount,         % extra space for brace and line
  abovecaptionskip=\dimexpr.5em+\smallskipamount,% extra space for brace and line
]
def nutzer_gruessen(*\tmS[yellow]{hl1}*p_vorname, p_nachname*\tmT{hl1}*):
   print("Herzlich willkommen,", p_vorname, p_nachname)

vorname = input("Bitte Vorname eingeben: ")
nachname = input("Bitte Nachname eingeben: ")
nutzer_gruessen(*\tmS[green]{hl2}*vorname, nachname*\tmT{hl2}*)
\end{lstlisting}
\noindent Foo \hrulefill Bar
\begin{tikzpicture}[remember picture,overlay]
\draw[yellow!50!black, oBrace=hl1];
\draw[green!50!black, uBrace=hl2];
\draw[Empharrow=6cm] (hl2-b) to (hl1-t);
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容