4

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?

7

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}
  • When I use lIf, the semicolon is automatically inserted at the end of true and there is a space between the word and the semicolon, unlike your snapshot. – padawan Jun 17 '14 at 21:34
  • @cagirici: Why is that? Please provide us with a minimal working example (MWE) that replicates your problem. – Werner Jun 17 '14 at 21:37
  • it was because my mistake. That worked for me. Thank you very much. – padawan Jun 17 '14 at 21:43
  • why line number does not show up? How could I show the line number? – alper Mar 7 '17 at 14:44
  • 1
    @Avatar: The package option linesnumbered adds line numbers to each algorithm. – Werner Mar 7 '17 at 20:12
7

For that you should add noend option when declaring the package:

\usepackage[noend]{algorithm2e}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.