# Indention of vertical rules with algorithm2e

I am trying to create an algorithm in Latex with the package algorithm2e and algpseudocode. The code and a picture with my output is included below.

I have two goals:

1. I would like to unindent the last "end" and "Update $\gamma$"
2. The vertical lines under Statement 3 should be under the corresponding while and for loop.

However, I did not find any useful trick by now and appreciate any help.

Thank you

\documentclass[12pt,a4paper,twoside,openright]{report}
\usepackage{setspace}
\onehalfspacing
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage[ruled]{algorithm2e}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}[hbt!]
\caption{\textit{Imputation of missing values with CART}}\label{alg:two}
\KwData{$\mathbf{X}$ a $n$ x $p$ matrix with missing values, and a stopping criterion $\gamma$}
\KwResult{$\mathbf{X}^{imp}$ an imputed $n$ x $p$ data matrix}
\begin{algorithmic}[1]
\State initial guess for missing values in $\mathbf{X}$\
\State $\mathbf{k} \gets$ vector of sorted indices of columns in $\mathbf{X}$ w.r.t. increasing amount of missing values\
\State \While{not $\gamma$}{
$\mathbf{X}_{old}^{imp} \gets$  store previously imputed matrix\;
\For{$j$ in $\mathbf{k}$}{
Fit a random forest: CART $\gets \mathbf{x}_{obs}^{j} \sim \mathbf{x}_{obs}^{-j}$\;
Predict missing values: $\mathbf{x}_{miss}^{j} \gets$ CART($\mathbf{x}_{miss}^{-j}$)\;
Update $\mathbf{X}^{imp}$: $\mathbf{X}_{new}^{imp} \gets \mathbf{x}_{miss}^{j}$\;
}
Update $\gamma$\;
}
\State \textbf{return} $X^{imp}$\
\end{algorithmic}
\end{algorithm}

\end{document}


New contributor
dpendi is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• You're using markup commands from both algorithm2e and algpseudocode, which are not compatible. Keep algorithm package which provides the float environment algorithm, and choose only one between the other two. A short intro: overleaf.com/learn/latex/Algorithms Jun 23 at 19:06