[Tex/LaTex] ForEach loop with \usepackage[noend]{algpseudocode}

algorithmspseudocode

I used the perfect answer from here How can I define a ForEach loop on the basis of the existing ForAll loop?
to define for each loop.

Once I replace \usepackage{algpseudocode} with \usepackage[noend]{algpseudocode} I still see end for.

How do I modify the code so that it will work with \usepackage[noend]{algpseudocode}?

My code:

\documentclass{llncs}

\usepackage{graphicx}

\usepackage{tikz}
\usetikzlibrary{patterns, arrows,matrix, positioning, decorations.markings, calc, arrows.meta}

\usepackage{algcompatible}

\usepackage{environ}
\usepackage{textcomp}

\usepackage[hyphens]{url}
\usepackage[hidelinks]{hyperref}
\hypersetup{breaklinks=true}
\urlstyle{same}
\usepackage{csquotes}

\usepackage{siunitx}
\usepackage{gensymb}

\usepackage{cleveref}
\usepackage{listings}

\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage[noend]{algpseudocode}% http://ctan.org/pkg/algorithmicx

\algnewcommand\algorithmicinput{\textbf{Input:}}
\algnewcommand\INPUT{\item[\algorithmicinput]}

\algnewcommand\algorithmicoutput{\textbf{Output:}}
\algnewcommand\OUTPUT{\item[\algorithmicoutput]}

\algnewcommand\algorithmicassume{\textbf{Assume:}}
\algnewcommand\ASSUME{\item[\algorithmicassume]}

\renewcommand{\COMMENT}[2][.4]{%
  \leavevmode\hfill\makebox[#1\linewidth][l]{$\triangleright$~#2}}

\let\emptyset\varnothing

\algblockdefx[Foreach]{Foreach}{EndForeach}[1]{\textbf{for each} #1 \textbf{do}}{\textbf{end for}}

% avoid breaking "N-d" on separate lines
\usepackage[shortcuts]{extdash}

\newcommand{\Nd}{}% To make sure that it isn't already defined 
\def\Nd/{$N$\=/d array}

\makeatletter
\def\HiLi{\hskip-\ALG@thistlm\leavevmode\rlap{\hbox to \linewidth{\color{gray!15}\leaders\hrule height .8\baselineskip depth .8ex\hfill}}\hskip+\ALG@thistlm}
\makeatother

\makeatletter
\newcommand{\algcolor}[2]{%
    \hskip-\ALG@thistlm\colorbox{#1}{\parbox{\dimexpr\linewidth-2\fboxsep}{\hskip\ALG@thistlm\relax #2}}%
}
\newcommand{\algemph}[1]{\algcolor{gray}{#1}}
\makeatother

% similar to binary operator $\bmod$
\makeatletter
\newcommand*{\bdiv}{%
  \nonscript\mskip-\medmuskip\mkern5mu%
  \mathbin{\operator@font div}\penalty900\mkern5mu%
  \nonscript\mskip-\medmuskip
}
\makeatother


\usepackage{epsfig}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}

\usepackage{epstopdf}
\usepackage[labelsep=period]{caption}

\begin{document}


\section{G}

\begin{algorithm}[t]
  \caption{G}
  \label{alg:ge}

  \begin{algorithmic}[1]
    \Procedure{R}{$P, s_i, \varepsilon_i$}
        \Foreach{$p \in P$}
            \textproc{Cut-One}($p, s_i, \varepsilon_i$)
        \EndForeach
    \EndProcedure
  \end{algorithmic}
\end{algorithm}

\end{document}

Best Answer

You define a new block statement Foreach by using

\algblockdefx[Foreach]{Foreach}{EndForeach}[1]{\textbf{for each} #1 \textbf{do}}{\textbf{end for}}

In contrast to the proposal in the other post that you link to you do not reuse \endfor (which is affected by the noend option), but introduce a new command for closing, \EndForeach, which is not affected by the noend option. To make it sensitive, add the following lines below the above one:

\makeatletter
\ifthenelse{\equal{\ALG@noend}{t}}{\algtext*{EndForeach}}{}
\makeatother