用偽代碼定義函數

用偽代碼定義函數

你能幫我嗎,我怎麼能在乳膠中寫一段帶有函數的偽代碼?

    \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}

我收到錯誤訊息

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

答案1

也許你正在尋找看起來像這樣的東西:

在此輸入影像描述

\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是由提供algpseudocode\STATE,而只能透過algcompatible或使用algorithmic。我已將您的輸入轉換為與algpseudocode.

相關內容