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 youenter image description here



    \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}
            \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}$\


enter image description here

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


Your Answer

dpendi is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.