[Tex/LaTex] Defining a new keyword in algorithm2e

algorithm2e

I'm trying to define a new keyword for my algorithm:

\documentclass{article}
\usepackage[]{algorithm2e}
\begin{document}
\SetKw{kwInit}{Init:}
\begin{algorithm}
\kwInit{$\alpha_n=1$ for all $n$}
\While{not at end of this document}{
do this and that
}
\end{algorithm}
\end{document}       

The problem is the colon after Init is not automatically generated and I have to define it like this {Init:} which will generate a bold colon instead of a normal one. How to fix this?
Also, the argument "for all n" will be italic and immediately followed by "while" of the next line. How can I fix this?

Thanks.

Best Answer

Sounds like you are after \SetKwInput, with syntax

\SetKwInput{<name>}{<keymord>}

which defines the macro \<name>{<arg>} to print <keyword> in keyword typography followed by :, and then argument <arg>.

A complete example:

\documentclass{article}
\usepackage[]{algorithm2e}

\SetKwInput{kwInit}{Init}

\begin{document}
\begin{algorithm}
\kwInit{$\alpha_n=1$ for all $n$}
\While{not at end of this document}{
do this and that
}
\end{algorithm}
\end{document}  

The result:

enter image description here