18

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?

3 Answers 3

12

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

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

\usepackage[noend]{algorithm2e}
1
  • 3
    Why is this answer not higher? Jun 30, 2021 at 22:32
4

Use \uIf{condition} instead:

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

From the official alorithm2e documentation (Section: if-then-else macros):

\uIf{condition}{then block without end}

Gives the following output:

if then without end

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .