21

I can easily generate the following algorithm statements using algorithm2e package. As can be seen from the image, return statement (should hold for other statements too) will be placed in a new line even it is very short. How can I write if-then-return or else-return in one line?

enter image description here

P.S. the code to generate the above is as follows:

... ...
\uIf{$\max_{\mathcal{AV}} > \theta_5$}
{
    \Return the tracklet with $\max_{\mathcal{AV}}$.
}
\Else
{
    \Return no speaker.
}   
... ...
4
  • With this type of notation, your algorithm may become ambiguous... Jan 24, 2014 at 7:24
  • @PaulGaborit What kind of ambiguity? Jan 24, 2014 at 7:27
  • Try with several if-then structures followed by one else line... Jan 24, 2014 at 7:31
  • @PaulGaborit I think there is no problem here because else will be aligned with its corresponding if. Jan 24, 2014 at 7:48

1 Answer 1

26

You can use \lIf instead of \uIf and \lElse instead of \Else.

MWE

\documentclass{report}
\usepackage{algorithm2e}

\begin{document}

\begin{algorithm}
\lIf{$\max_{\mathcal{AV}} > \theta_5$}
{
    \Return the tracklet with $\max_{\mathcal{AV}}$.
}
\lElse
{
    \Return no speaker.
}  
\end{algorithm}

\end{document} 

Output

enter image description here


EDIT

If you don't want to print the semicolon at the end of each line, simply issue the command

\DontPrintSemicolon

MWE

\documentclass{report}
\usepackage{algorithm2e}

\begin{document}

\begin{algorithm}
\DontPrintSemicolon
\lIf{$\max_{\mathcal{AV}} > \theta_5$}
{
    \Return the tracklet with $\max_{\mathcal{AV}}$.
}
\lElse
{
    \Return no speaker.
}
\end{algorithm}

\end{document} 

Output

enter image description here

3
  • I just noticed that there will be an extra ; on each line. Is there any way to avoid this? Dec 1, 2014 at 8:51
  • @herohuyongtao See the edit Dec 3, 2014 at 6:25
  • @herohuyongtao You're welcome Dec 3, 2014 at 8:32

You must log in to answer this question.

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