I am new at algorithm2e
and I'm really struggling with the most simple things.
I would like to write an algorithm (pseudocode) with a few functions that call each other. For now, I have this minimal (not)working example:
\begin{algorithm}
\caption{Some algorithm}
\label{alg:some-algorithm}
\DontPrintSemicolon
\SetKwFunction{FMain}{Main}
\SetKwFunction{FTest}{Test}
\FMain{$f$, $a$, $b$, $\varepsilon$}{
a\;
b\;
\KwRet\;
}
\FTest{$f$, $a$, $b$}{
a\;
b\;
\KwRet\;
}
\end{algorithm}
As you can see, the first line of code a
is on the same line as the function definition. Also, nothing is indented and the whole thing looks rather ugly.
What am I doing wrong?
Best Answer
As the documentation (try
texdoc algorithm2e
on the command line or search foralgorithm2e.pdf
) says:In particular,
\FMain
andFTest
both have only a single argument. They are used to correctly typeset a function name, not to define a new function. To typeset a function definition, use\SetKwProg{Prog}{Title}{is}{end}
.EDIT: The following example is in the documentation:
leads to:
So the idea is that
\SetKwProg
defines a template for functions (e.g.\Fn
or\Def
).\Fn
and\Def
themselves now have two arguments, one for the header and one for the body of the function.To adapt your example:
gives