3

I'd like to emphasize the method calls contained in a listing, by drawing arrows with tikz. For instance, something similar to what I've done with an image editor as follows enter image description here

Any hints on better presentational styles are well accepted too. Here is a MWE you can work on

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

Here an attempt, you can add nodes A, B, C and D using escapeinside then you have the possibility to connect them

\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}
  • It is the simple solution I was looking for. Thank you. – tsigalko18 Dec 4 '16 at 13:09

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.