[Tex/LaTex] How to remove \EndIf and \EndFor in algorithmicx

algorithmicxalgorithmspseudocode

For space consideration, I wish to condense my pseudocode.

I find it a bit annoying to me to always have a \EndIf or \EndFor at the end of every block, especially when inside the block only lies one statement. e.g.

\documentclass[a4paper]{article}

\usepackage{algorithmicx}
\usepackage{algorithm} % http://ctan.org/pkg/algorithms
\usepackage{algpseudocode} % http://ctan.org/pkg/algorithmicx
\newcommand*{\Let}[2]{\State #1 $\gets$ \parbox[t]{\linegoal}{#2\strut}}
\algnewcommand\algorithmicinput{\textbf{INPUT: }}
\algnewcommand\Input{\item[\algorithmicinput]}
\algnewcommand\algorithmicoutput{\textbf{OUTPUT: }}
\algnewcommand\Output{\item[\algorithmicoutput]}

\begin{document}
\begin{algorithm}
\begin{algorithmic}[1]
\Procedure{A}{}
    \ForAll{$a\in A$}
        \If{$a\geq b$}
            \State\Return $a$
        \EndIf
    \EndFor
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

To save the space, I wish to remove them, although it may be non-standard.

Once I do it, the code cannot be compiled due to errors.

Is there any "illegal" trick to kinda remove them from the output PDF?

Best Answer

Preventing end keywords from being typeset

Simply pass the noend option to the algpseudocode package: substitute \usepackage[noend]{algpseudocode} for \usepackage{algpseudocode} in your code and you get

enter image description here

To avoid having to use \EndIf and \Endfor in your input file

(After clarification by the OP, this turns out to be off-topic.)

Missing \EndIf and \EndFor will generate errors; you cannot simply omit them in your input file. If you want to save yourself the hassle of having to use those macros in your input file in order to close control-flow statements, you can always define a macro that combines \If and \EndIf, another that combines \ForAll and \EndFor, etc.

enter image description here

\documentclass[a4paper]{article}

\usepackage{algorithmicx}
\usepackage{algorithm}
\usepackage{algpseudocode}
\newcommand*{\Let}[2]{\State #1 $\gets$ \parbox[t]{\linegoal}{#2\strut}}
\algnewcommand\algorithmicinput{\textbf{INPUT: }}
\algnewcommand\Input{\item[\algorithmicinput]}
\algnewcommand\algorithmicoutput{\textbf{OUTPUT: }}
\algnewcommand\Output{\item[\algorithmicoutput]}

\begin{document}
\newcommand\sForAll[2]{ \ForAll{#1}#2\EndFor} % snappy version of \ForAll...\EndFor
\newcommand\sIf[2]{ \If{#1}#2\EndIf}          % snappy version of \If...\EndIf

\begin{algorithm}
\begin{algorithmic}[1]
\Procedure{A}{}
    \sForAll{$a\in A$}{
        \sIf{$a\geq b$}{
            \State\Return $a$
        }
    }
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}