15

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.
}   
... ...
  • With this type of notation, your algorithm may become ambiguous... – Paul Gaborit Jan 24 '14 at 7:24
  • @PaulGaborit What kind of ambiguity? – herohuyongtao Jan 24 '14 at 7:27
  • Try with several if-then structures followed by one else line... – Paul Gaborit Jan 24 '14 at 7:31
  • @PaulGaborit I think there is no problem here because else will be aligned with its corresponding if. – herohuyongtao Jan 24 '14 at 7:48
16

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

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

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.