[Tex/LaTex] Vertical lines in algpseudocode

algorithm2ealgorithmicxalgorithmsline

I am trying to get vertical lines to appear in this:

\begin{spacing}{0.8}
\begin{algorithm}
\caption{Cost-Vector Algorithm}\label{costalgorithm}
\begin{algorithmic}[1]
\Require \begin{varwidth}[t]{\linewidth}
                  Training sentences $S_{i}$\par      
               \end{varwidth}
\Ensure Sentence instances with cost-vectors for training $S_{i,c_i}$
\Function{generateCosts}{$EV_{i,v,r}$}
\State $S_{i,c_{i}} = \left[\right]$
\ForAll{$s \in S , v \in EV $}
\State $c_{i} = \left\{\right\}$
\State set $region \;r= EV_{i,r}$
\For {$p \leftarrow 1, properties$}
\State $c_{i,p}:=cost(kb_{r,p},v_{i,r}$
\If {$c_{p} > Cost_t$}
\State {$c_{p}:=\infty$}
\Else 
\State continue
\EndIf
\EndFor
\If {$min(c) > APE_t$}
\State $c_{i,no\_property}:=0$
\Else 
\State $c_{i,no\_property}:=\infty$
\EndIf
\State push($S_{i,c_{i}},(s,c_{i}))$
\EndFor\label{endfor}
\State \textbf{return} $S_{i,c_{i}}$
\EndFunction
\end{algorithmic}
\end{algorithm}
\end{spacing}

And this is loaded before my document starts:

\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}%http://ctan.org/pkg/algorithmicx
% \usepackage[linesnumbered,ruled]{algorithm2e}
\algrenewcommand\textproc{}% Used to be \textsc
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\usepackage{calc} % for \widthof
\algrenewcommand\algorithmicensure{%
  \makebox[\widthof{\textbf{Require:}}][l]{\textbf{Ensure:}}} 

Currently it looks like:

enter image description here

I am trying to emulate this link: https://cl.ly/0d3g3k1c3D1g.

Best Answer

You can use \usepackage[ruled, vlined, linesnumbered]{algorithm2e}.

Please give a MWE next time.

I hope, this is what you want:

\documentclass[a4paper, 12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[ngerman]{babel}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage[ruled, vlined, linesnumbered]{algorithm2e}

\begin{document}

\begin{algorithm}
\DontPrintSemicolon
\KwIn{Training sentences $S_{i}$}
\KwOut{Sentence instances with cost-vectors for training $S_{i,c_i}$}
\SetKwBlock{Begin}{function}{end function}
\Begin($\text{generateCosts} {(} EV_{i,v,r} {)}$)
{
  $S_{i,c_{i}} = \left[ \right]$\;
  \ForAll{$s \in S, v \in EV $}
  {
    $c_{i} = \left\lbrace \right\rbrace$\;
    set $region \; r = EV_{i,r}$\;
    \For{$p \leftarrow 1, properties$}
    {
      $c_{i,p} \coloneqq cost \left( kb_{r,p},v_{i,r} \right)$\;
      \uIf{$c_{p} > Cost_t$}
      {
        $c_{p} \coloneqq \infty$
      }
      \Else
      {
        continue
      }
    }
    \uIf{$\min \left( c \right)  > APE_t$}
    {
      $c_{i,no\_property} \coloneqq 0$
    }
    \Else
    {
      $c_{i,no\_property} \coloneqq \infty$
    }
    $\text{push} \left( S_{i,c_{i}}, \left( s,c_{i} \right) \right)$
  }\label{endfor}
  \Return{$S_{i,c_{i}}$}
}
\caption{Cost-Vector Algorithm}\label{costalgorithm}
\end{algorithm}
\end{document}

I changed some little things, like:

  • coloneqq instead of :=
  • missing bracket in line 7 added
  • \min instead of min
  • ...

Output: enter image description here