[Tex/LaTex] algorithm2e vertical line indentation

algorithm2eindentation

I really like the vertical line feature of algorithm2e, but I ran into trouble with it when I try to indent an if statement. I used the following code.

\documentclass{article}
\usepackage{amssymb,amsthm,fullpage,amsmath}
\usepackage[ruled,vlined,boxed]{algorithm2e}

\begin{document}

\begin{algorithm}[H]
 \caption{\bf Generic Line Search Algorithm}

 {\bf Inputs:} Starting point $x_0$, initial step size parameter $\alpha_0$. \\
 \For{$k = 0,1,2,\dots$}{
    \nl {\bf Gradient approximation $g(x_k)$:}\\ \Indp
    Compute an approximation $g(x_k)$ of $\nabla \phi(x_k)$. \\ \Indm
    \nl {\bf Construct a search direction $d_k$:}\\ \Indp
    Construct a search direction $d_k$, e.g., $d_k = - g(x_k)$.\\ \Indm
    \nl {\bf Compute step size $\alpha_k$ and update the iterate:} \\ \Indp
    \eIf{sufficient decrease condition is satisfied}{
        $x_{k+1} = x_k - \alpha_k d_k$ and $\alpha_{k+1} \gets \tau^{-1} \alpha_k$
    }{
        $x_{k+1} = x_k$ and $\alpha_{k+1} \gets \tau \alpha_k$
    } 
 }
\end{algorithm}

\end{document}

The texts were indented but the vertical lines in the if statement were not, as shown in the picture.
vertical line indentation issues in algorithm2e
Does anyone know how to indent the vertical lines?

Best Answer

Try this:

  • \skiptext (before vline space) and \skiprule (after vline space) are additionally updated inside indent adjusting command.
  • No change to algorithm input.
\documentclass{article}
%\usepackage{amssymb,amsthm,fullpage,amsmath}
\usepackage[ruled,vlined,boxed]{algorithm2e}

\makeatletter
\renewcommand{\Indentp}[1]{%
  \advance\leftskip by #1
  \advance\skiptext by -#1
  \advance\skiprule by #1}%
\renewcommand{\Indp}{\algocf@adjustskipindent\Indentp{\algoskipindent}}
\renewcommand{\Indpp}{\Indentp{0.5em}}%
\renewcommand{\Indm}{\algocf@adjustskipindent\Indentp{-\algoskipindent}}
\renewcommand{\Indmm}{\Indentp{-0.5em}}%
\makeatother

\begin{document}
\begin{algorithm}[H]
 \caption{\bfseries OP's example}

 \textbf{Inputs:} Starting point $x_0$, initial step size parameter $\alpha_0$. \\
 \For{$k = 0,1,2,\dots$}{
    \nl \textbf{Gradient approximation $g(x_k)$:}\\ \Indp
    Compute an approximation $g(x_k)$ of $\nabla \phi(x_k)$. \\ \Indm
    \nl \textbf{Construct a search direction $d_k$:}\\ \Indp
    Construct a search direction $d_k$, e.g., $d_k = - g(x_k)$.\\ \Indm
    \nl \textbf{Compute step size $\alpha_k$ and update the iterate:} \\ \Indp
    \eIf{sufficient decrease condition is satisfied}{
        $x_{k+1} = x_k - \alpha_k d_k$ and $\alpha_{k+1} \gets \tau^{-1} \alpha_k$
    }{
        $x_{k+1} = x_k$ and $\alpha_{k+1} \gets \tau \alpha_k$
    }
 }
\end{algorithm}


\begin{algorithm}
  \caption{More tests}
  \nl test \\
  \eIf{sufficient decrease condition is satisfied}{
    if clause
  }{
    else clause
  }
  \nl test \\ \Indp\Indp
  \eIf{sufficient decrease condition is satisfied}{
    if clause
  }{
    else clause
  } \Indm
  \nl test \\
  \eIf{sufficient decrease condition is satisfied}{
    if clause
  }{
    else clause
  }
\end{algorithm}
\end{document}

enter image description here enter image description here

Notes:

  • \Indp (or \Indm) increases (or reduces) indentation by \algoskipindent (aka "step").
  • This might not be the perfect solution, since I have not fully understand the algorithm2e internals \algocf@Vline and \algocf@Vsline.
Related Question