[Tex/LaTex] Parameter section similar to Input and Output

algorithm2e

I would like to create a "Parameter: …" section, similar to the "Input: …" and "Output: …" sections in my algorithm description. The parameters are global, user-defined constants, known before the algorithm is run.

I have tried to define a new keyword with the text "Parameter" but of course that does the wrong thing (among others: the line is numbered but it should not be, everything behind the "Parameter:" is in italics but I don't want it to be).

How can I create a parameter section similar to Input and Output?


Gonzalo Medina's answer almost does the right thing but I get a superfluous colon on a separate line.

enter image description here

Code:

\documentclass[preprint,3p,12pt,times]{elsarticle}
\usepackage{ecrc}
\usepackage[linesnumbered,algoruled,boxed,lined]{algorithm2e}
\volume{00}
\firstpage{1}
\journalname{Some journal}

% Would work correctly with these lines:
% \documentclass{article}
% \usepackage[linesnumbered,algoruled,boxed,lined]{algorithm2e}

\SetKwInOut{Parameter}{Parameters}

\begin{document}

\begin{algorithm}[H]
  \KwIn{Some input}
  \Parameter{Some parameter}
  \KwOut{Some output}    
\caption{Some algorithm}\label{alg.mainLoop}
\end{algorithm}

\end{document}

I guess it is some macro clash among the packages. Any workaround would be greatly appreciated.

Best Answer

You can use

\SetKwInOut{Parameter}{parameter}

and then

\Parameter{<text>}

in you algorithms. A complete example:

\documentclass{article}
\usepackage{algorithm2e}

\SetKwInOut{Parameter}{parameter}

\begin{document}

\begin{algorithm}
\SetKwData{Left}{left}
\SetKwData{This}{this}
\SetKwData{Up}{up}
\SetKwFunction{Union}{Union}
\SetKwFunction{FindCompress}{FindCompress}
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}
\Input{A bitmap $Im$ of size $w\times l$}
\Output{A partition of the bitmap}
\Parameter{A parameter for the algorithm}
\BlankLine
\emph{special treatment of the first line}\;
\For{$i\leftarrow 2$ \KwTo $l$}{
\emph{special treatment of the first element of line $i$}\;
\For{$j\leftarrow 2$ \KwTo $w$}{\label{forins}
\Left$\leftarrow$ \FindCompress{$Im[i,j-1]$}\;
\Up$\leftarrow$ \FindCompress{$Im[i-1,]$}\;
\This$\leftarrow$ \FindCompress{$Im[i,j]$}\;
\If(\tcp*[h]{O(\Left,\This)==1}){\Left compatible with \This}{\label{lt}
\lIf{\Left $<$ \This}{\Union{\Left,\This}}\;
\lElse{\Union{\This,\Left}\;}
}
\If(\tcp*[f]{O(\Up,\This)==1}){\Up compatible with \This}{\label{ut}
\lIf{\Up $<$ \This}{\Union{\Up,\This}}\;
\tcp{\This is put under \Up to keep tree as flat as possible}\label{cmt}
\lElse{\Union{\This,\Up}}\tcp*[r]{\This linked to \Up}\label{lelse}
}
}
\lForEach{element $e$ of the line $i$}{\FindCompress{p}}
}
\caption{disjoint decomposition}\label{algo_disjdecomp}
\end{algorithm}

\end{document}

enter image description here

After the edit to the question, there's some incompatibility between elsarticle and the new definition; in this case, you can move the new definition inside the algorithm environment which can be done on a on-per-one basis or, as Werner suggested in his comment, can be automated using

\makeatletter 
\g@addto@macro{\@algocf@init}{\SetKwInOut{Parameter}{Parameters}} 
\makeatother

A complete example:

\documentclass[preprint,3p,12pt,times]{elsarticle}
\usepackage[linesnumbered,algoruled,boxed,lined]{algorithm2e}

\makeatletter 
\g@addto@macro{\@algocf@init}{\SetKwInOut{Parameter}{Parameters}} 
\makeatother

\begin{document}

\begin{algorithm}[H]
\KwIn{Some input}
\Parameter{Some parameter}
\KwOut{Some output}    
\caption{Some algorithm}\label{alg.mainLoop}
\end{algorithm}

\end{document}

enter image description here

Related Question