3

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?

1
  • 1
    Similar to tex.stackexchange.com/q/299679. It seems this is a known and not solved problem that \vlined and \Indp do not work properly together. I will try to investigate this. May 18, 2020 at 6:42

2 Answers 2

2

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.
2
  • Thank you. I am not an advanced user of Latex, but it seems like you just debugged the package. Could you elaborate what \skiptext, \skiprule, and \algoskipindent are, or if there is a good read on this?
    – Liyuan Cao
    May 18, 2020 at 20:00
  • 1
    @LiyuanCao Added some notes to my answer. Hope that helps. May 19, 2020 at 1:18
1

As said in the comments using vertical lines with \indp and \indm is a little buggy. The alternative is to turn the Compute step size into a block and thus allow the algorithm2e package to handle the indentation on its own in a better way.

This is done using \SetKwBlock{Begin}{begin}{end} macro:

\SetKwBlock{NewBlock}{Compute step size $\alpha_k$ and update the iterate:}{}

This would indent the if nicely, however, it would also add a vertical line for the new Compute step size block. To remove that vertical line we add \SetAlgoNoLine and \SetAlgoVlined.

MWE:

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


\begin{document}

\begin{algorithm}[H]
\DontPrintSemicolon
\SetKwBlock{NewBlock}{Compute step size $\alpha_k$ and update the iterate:}{}
\caption{\bf Generic Line Search Algorithm}

\KwIn{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\SetAlgoNoLine\NewBlock{ \SetAlgoVlined\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}

And the outcome:

enter image description here

Notice the following improvements from OP's MWE:

  1. At the end of each line \; is used (no need for \\). In order not to print semicolons we use DontPrintSemicolon.
  2. We use algorithm2e built-in \KwIn macro.
2
  • This is how I wanted to solve this problem originally, but I was not familiar with keyword blocks enough. One question: what is the benefit of using \; instead of \\?
    – Liyuan Cao
    May 18, 2020 at 20:02
  • 1
    The package documentation stresses that you must not forget to end each line with \;. It does so in a couple of places, and I think that has to do with the way the package identifies an end of line.
    – Elad Den
    May 19, 2020 at 6:39

You must log in to answer this question.

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