TransWikia.com

algorithm2e get rid of newline after then

TeX - LaTeX Asked on October 5, 2021

I’m having this code

documentclass[11pt,a4paper]{article}
usepackage[]{algorithm2e}

begin{document}
begin{algorithm}[H]
                If{cond}{
                        stmt1;
                }
end{algorithm}
end{document}

Which gives me this:

This makes some algorithms much shorter, I’m searching for a way to get rid of the newline after the then (for ifs where the body is only a oneliner). Is this possible with algorithm2e and if yes how?

One Answer

For a one line if-statement you can use lIf (line if). The semicolon is added automatically.

documentclass[11pt,a4paper]{article}
usepackage{algorithm2e}

begin{document}
begin{algorithm}
                If{cond}{
                        stmt1;
                }
                lIf{cond}{
                        stmt2
                }
                other things
end{algorithm}
end{document}

enter image description here

See page 32 of the package manual for the different macros for if-then-else constructs.

Correct answer by Marijn on October 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP