[Tex/LaTex] Define function in pseudocode

algorithmspseudocode

Could you help me please how can I write a piece of pseudocode with function in latex?

    \begin{algorithmic}
\STATE $\mathrm{maxWeight} \leftarrow \mathrm{value of e.weight where e has maximum value of weight from all elements}$
\STATE $\mathrm{minWeight} \leftarrow \mathrm{value of e.weight where e has minimum value of weight from all elements}$
\STATE $\mathrm{maxDrawValue} \leftarrow \mathrm{value of element maximum size in visualization}$
\STATE $\mathrm{minDrawValue} \leftarrow \mathrm{ value of element minimum size in visualization}$
\Function{CountDrawValue}{elementWeight}
  \IF maxWeight = minWeight
\RETURN $\frac{maxWeight + minWeight}{2}$
\EndFunction
\end{algorithmic}

I get error message

! Undefined control sequence.
l.631 \Function
               {CountDrawValue}{elementWeight}
? 

Best Answer

Perhaps you're after something that looks like this:

enter image description here

\documentclass{article}
\usepackage{algpseudocode,amsmath}
\newcommand{\var}{\texttt}
\newcommand{\assign}{\leftarrow}
\newcommand{\multilinestate}[1]{%
  \parbox[t]{\linewidth}{\raggedright\hangindent=\algorithmicindent\hangafter=1
    \strut#1\strut}}
\begin{document}

\begin{algorithmic}
  \State \multilinestate{$\var{maxWeight} \assign{}$value of \var{e.weight} where~\var{e} has maximum value of weight from all elements}
  \State \multilinestate{$\var{minWeight} \assign{}$value of \var{e.weight} where~\var{e} has minimum value of weight from all elements}
  \State $\var{maxDrawValue} \assign{}$value of element maximum size in visualization
  \State $\var{minDrawValue} \assign{}$value of element minimum size in visualization
  \Function{CountDrawValue}{\var{elementWeight}}
    \If{$\var{maxWeight} = \var{minWeight}$}
      \State\Return $(\var{maxWeight} + \var{minWeight}) / 2$
    \EndIf
  \EndFunction
\end{algorithmic}

\end{document}

\Function is supplied by algpseudocode, while the use of \STATE is only available through algcompatible or algorithmic. I've converted your input to something that is totally compatible with algpseudocode.