如何在列表中画箭头来强调方法调用?

如何在列表中画箭头来强调方法调用?

我想通过使用 tikz 绘制箭头来强调清单中包含的方法调用。例如,类似于我使用图像编辑器所做的操作,如下所示 在此处输入图片描述

任何关于更好的表达风格的提示都会被接受。这里有一个你可以尝试的 MWE

\documentclass[border=1pt]{standalone}

\usepackage{listings}
\usepackage{color}
\usepackage{caption}

\begin{document}

\definecolor{mymauve}{rgb}{0.6,0,0.82}
\definecolor{lightgray}{gray}{0.9}
\definecolor{mygreen}{rgb}{0.0, 0.5, 0.0}

\lstset{
    emphstyle=\color{blue}\bfseries,
    numbers=left,
    basicstyle=\linespread{0.7}\fontsize{6}{12}\selectfont\ttfamily,
    numberstyle=\scriptsize,
    stepnumber=1,
    language=Java,
    numbersep=8pt,
    keywordstyle=\color{blue}\bfseries,
    commentstyle=\color{mygreen},
    stringstyle=\color{mymauve},
    backgroundcolor=\color{lightgray},
    frame=lines,
    captionpos=b,
    tabsize=2,
    breaklines=true,
    numberblanklines=false,
}

\begin{lstlisting}[]
    // page object code
    public HomePage login(String usr, String pwd){
        username.sendKeys(usr);
        password.sendKeys(pwd);
        login.click();
        return new HomePage(driver);
    }

    // test code
    @Test
    public void testLoginEmpty(){
        IndexPage ip = new IndexPage(driver);
        HomePage hp = ip.login("", "");
        assertThat(driver.getTitle(), containsString("index"));
    }

    @Test
    public void testLoginKo(){
        IndexPage ip = new IndexPage(driver);
        HomePage hp = ip.login("admin", "wrongpwd");
        assertThat(driver.getTitle(), containsString("index"));
    }

    @Test
    public void testLoginOk(){
        IndexPage ip = new IndexPage(driver);
        HomePage hp = ip.login("admin", "secret");
        assertEquals(hp.getLoggedUser(), "(admin)");
    }
\end{lstlisting}
\end{document}

答案1

在这里尝试一下,你可以添加节点,,A然后使用,你就可以连接它们BCDescapeinside

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usepackage{listings}
\usepackage{color}
\usepackage{caption}

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

\begin{document}

\definecolor{mymauve}{rgb}{0.6,0,0.82}
\definecolor{lightgray}{gray}{0.9}
\definecolor{mygreen}{rgb}{0.0, 0.5, 0.0}

\lstset{
    emphstyle=\color{blue}\bfseries,
    numbers=left,
    escapeinside={*}{*},
    basicstyle=\linespread{0.7}\fontsize{6}{12}\selectfont\ttfamily,
    numberstyle=\scriptsize,
    stepnumber=1,
    language=Java,
    numbersep=8pt,
    keywordstyle=\color{blue}\bfseries,
    commentstyle=\color{mygreen},
    stringstyle=\color{mymauve},
    backgroundcolor=\color{lightgray},
    frame=lines,
    captionpos=b,
    tabsize=2,
    breaklines=true,
    numberblanklines=false,
}

\begin{lstlisting}[]
    // page object code
    public HomePage login(String usr, String pwd){
        username.sendKeys(usr);
        password.sendKeys(pwd);
        login.click();*\tikzmark{A}*
        return new HomePage(driver);
    }

    // test code
    @Test
    public void testLoginEmpty(){
        IndexPage ip = new IndexPage(driver);
        HomePage hp = ip.login("", "");*\tikzmark{B}*
        assertThat(driver.getTitle(), containsString("index"));
    }

    @Test
    public void testLoginKo(){
        IndexPage ip = new IndexPage(driver);
        HomePage hp = ip.login("admin", "wrongpwd");*\tikzmark{C}*
        assertThat(driver.getTitle(), containsString("index"));
    }

    @Test
    public void testLoginOk(){
        IndexPage ip = new IndexPage(driver);
        HomePage hp = ip.login("admin", "secret");*\tikzmark{D}*
        assertEquals(hp.getLoggedUser(), "(admin)");
    }
\end{lstlisting}
\tikzset{Empharrow/.style={dashed,red,thick,->,arrows={-Triangle[width=3pt]}}}
\begin{tikzpicture}[remember picture,overlay]
\draw[Empharrow](D)--+(2cm,0)|-(A);
\draw[Empharrow](C)--+(1.5cm,0)|-(A);
\draw[Empharrow](B)--+(1cm,0)|-(A);
\end{tikzpicture}
\end{document}

相关内容