3

I am using the package algorithm2e and notice that it has a great feature where vertical lines can be connected within "blocks" of code (see Vertical loop/block lines in algorithmicx with 'noend' option for examples). I am trying to add similar vertical lines connecting code that are not necessarily in blocks of if/else/while etc. but just to keep track of indents.

Below is a reproducible example:

\documentclass{article}
\usepackage[vlined]{algorithm2e}
\begin{document}

\DontPrintSemicolon
\begin{algorithm}[H]
\SetAlgoLined
\KwData{Example keyword}
\KwResult{Example result}

/* \textit{Create first function}\;
function 1 = function(input, output)\{\;
\Indp/* \textit{Create inner function}\;
function 2 = function(input, output)\{\;
\Indp/* \textit{Perform main task}\;
mainTask(function1, function2)\;
\Indm\})\;
\Indm\}\;

\caption{Psuedocode for interactive scatterplot matrix}
\end{algorithm}
\end{document}

I am trying to add vertical lines at the indents. An example of my goal end product (where I superimposed lines in green) looks as below:

enter image description here

Any suggestions on how to achieve these type of lines in this minimal working example would be greatly appreciated!

1
  • Do you want to insert the lines manually for specific blocks, should it be done only for specific blocks (like functions in your example), or should it work for anything that is indented via \Indp/\Indm?
    – siracusa
    Commented Jun 1, 2019 at 17:54

2 Answers 2

4

In the following, algorithm2e requires a defined block to add vertical lines. That requires a keyword. Because the first line of each function has no keyword, I use \SetKwBlock{arg1}{arg2}{arg3} to define a keyword for each first line. arg1 holds the keyword, arg2 holds the text for the first line, and arg3 has a single space. Note that the text following each of these keywords is enclosed in braces to provide nesting.

Additional notes are provided in the code.

\documentclass{article}
\usepackage[lined]{algorithm2e} %Changed vlined to lined
\begin{document}

\DontPrintSemicolon
\begin{algorithm}[H]
%\SetAlgoLined May be removed as unnecessary
\SetInd{0.2em}{1.3em} %Moved vertical bar to the left, default is 0.5 and 1.0
\KwData{Example keyword}
\KwResult{Example result}

/* \textit{Create first function} \;
\SetKwBlock{Fna}{\textnormal{function 1 = function(input, output)\{ }}{}
\Fna{
  /* \textit{Create inner function} \;
  \SetKwBlock{Fnb}{\textnormal{function 2 = function(input, output)\{ }}{}
  \Fnb{
    /* \textit{Perform main task} \;
    mainTask(function1, function2) \;
  }\}\; %Removed unmatch parenthesis
}\}\;

\caption{Psuedocode for interactive scatterplot matrix}
\end{algorithm}
\end{document}

Algorithm 1

2

Here is an alternative solution that uses the internal \algocf@group macro to create a new block scope. The code for defining a function also was moved into a new macro \Func to give a more legible algorithm definition. Its first parameter is the arguments list, its second parameter is the function body.

\documentclass{article}
\usepackage[vlined]{algorithm2e}

\makeatletter
\newcommand\Func[2]{%
    function(#1)\{%
    \algocf@group{#2}%
    \}\;%
}
\makeatother

\SetInd{0.15em}{1em}
\DontPrintSemicolon

\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\KwData{Example keyword}
\KwResult{Example result}

/* \textit{Create first function}\;
function 1 = \Func{input, output}{
    /* \textit{Create inner function}\;
    function 2 = \Func{input, output}{
        /* \textit{Perform main task}\;
        mainTask(function1, function2)\;
    }
}

\caption{Psuedocode for interactive scatterplot matrix}
\end{algorithm}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .