[Tex/LaTex] Remove “end” in algorithm2e

algorithm2e

When I write

\If{condition}
    {
        \textbf{return true}\;
    }

The output is like belowenter image description here

However, I don't want end line. If possible, I want like this:

if condition then return true;

Is it possible?

Best Answer

This is entirely covered in the algorithm2e documentation (section 10.4 if-then-else macros), and is provided by \lIf (short for line If):

enter image description here

\documentclass{article}
\usepackage{algorithm2e}
\newcommand{\True}{\textbf{true}}
\begin{document}
\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
    \lIf{condition}{\Return{} \True}
  }
  \caption{How to write algorithms}
\end{algorithm}

\end{document}