[Tex/LaTex] How to add Function body to Latex algorithm pseudocode

algorithm2e

Does anyone know How to add Function body to Latex algorithm pseudocode?
Thanks in andvance!

Here is my code:

\usepackage[vlined, ruled, boxed]{algorithm2e}
\begin{algorithm}
 \KwData{model directory}
 \KwResult{multi-resolution model}
 enter directory\;
 geometrylist $\leftarrow$ search geometry files\;
 $np$ $\leftarrow$ number of total processes\;
 $ng$ $\leftarrow$ number of total geometry files\;
 \eIf{$ng < np$ and $processid < ng$}{
   GenSeparateLODFile(geometrylist[processid])\;
   }{
   sort geometrylist by file size\;
   \While{i*processid $<$ ng}{
        GenSeparateLODFile(geometrylist[processid*i])\;
        $i$++\;
    }
  }
  wait for all processes to end\;
    \If{isLastProcess}{
        assembly each LOD geometry\;
    }
\caption{PLG}
\end{algorithm}

The result:
enter image description here

What i want:
enter image description here

Best Answer

All you need is to create the block, which can be done using simply something like

\SetKwProg{Fn}{Function}{}{}

to have ready to use arguments. Then, in your algorithm you would say, for example,

\Fn{BuildOLD (directory)}{
<contents>
}

A complete example in which I also changed the fonts used to sans serif and the color of the rules so as to match the attached image for the desired result; of course, these changes are optional and can be safely removed.

\documentclass{article}
\usepackage[vlined,ruled]{algorithm2e}
\usepackage{xcolor}

%%% optional fonts and color configuration
\SetAlFnt{\sffamily}
\renewcommand\ArgSty{\normalfont\sffamily}
\renewcommand\KwSty[1]{\textnormal{\textbf{\sffamily#1}}\unskip}
\SetAlCapFnt{\normalfont\sffamily\large}
\renewcommand\AlCapNameFnt{\sffamily\large}

%%% vertical rules in cyan color
\makeatletter
\renewcommand{\algocf@Vline}[1]{%     no vskip in between boxes but a strut to separate them, 
  \strut\par\nointerlineskip% then interblock space stay the same whatever is inside it
  \algocf@push{\skiprule}%        move to the right before the vertical rule
  \hbox{\bgroup\color{cyan}\vrule\egroup%
    \vtop{\algocf@push{\skiptext}%move the right after the rule
      \vtop{\algocf@addskiptotal #1}\bgroup\color{cyan}\Hlne\egroup}}\vskip\skiphlne% inside the block
  \algocf@pop{\skiprule}%\algocf@subskiptotal% restore indentation
  \nointerlineskip}% no vskip after
%
\renewcommand{\algocf@Vsline}[1]{%    no vskip in between boxes but a strut to separate them, 
  \strut\par\nointerlineskip% then interblock space stay the same whatever is inside it
  \algocf@bblockcode%
  \algocf@push{\skiprule}%        move to the right before the vertical rule
  \hbox{\bgroup\color{cyan}\vrule\egroup%               the vertical rule
    \vtop{\algocf@push{\skiptext}%move the right after the rule
      \vtop{\algocf@addskiptotal #1}}}% inside the block
  \algocf@pop{\skiprule}% restore indentation
  \algocf@eblockcode%
}
%
\makeatother
%%% end of optional fonts and color configuration

\SetKwProg{Fn}{Function}{}{}

\begin{document}

\begin{algorithm}
\Fn{BuildOLD (directory)}{
 enter directory\;
 geometrylist $\leftarrow$ search geometry files\;
 $np$ $\leftarrow$ number of total processes\;
 $ng$ $\leftarrow$ number of total geometry files\;
 \eIf{$ng < np$ and $processid < ng$}{
   GenSeparateLODFile(geometrylist[processid])\;
   }{
   sort geometrylist by file size\;
   \While{i*processid $<$ ng}{
        GenSeparateLODFile(geometrylist[processid*i])\;
        $i$++\;
    }
  }
  wait for all processes to end\;
    \If{isLastProcess}{
        assembly each LOD geometry\;
    }}
\caption{PLG}
\end{algorithm}

\end{document}

The result:

enter image description here

Related Question