格式化 - 算法

格式化 - 算法

我有以下伪代码:

我想将其写成算法,但无法正确格式化。以下是我的尝试:

\documentclass{article}
\usepackage{algorithm,amsmath,algpseudocode}
\begin{document}

\begin{algorithm}
  \caption{Adaptive Thresholding}\label{euclid}

  \begin{algorithmic}[1]

    \State Convert $R$ to a binary region using the threshold $\theta_{0}$. \label{step:convert}

    \State Assume $N$ is the sum of the number of non-zero pixels within $F_{l}$ and $F_{r}$.

    \State If $N$ is larger than a predefined threshold $\beta$, then
             \State $\theta_{0} = \theta_{0} + \Delta\theta$
             \State Repeat the procedure from step \ref{step:convert}
    \State Else
             \State $\theta = \theta_{0}$
    \State End
    \State where $\Delta\theta$, $\beta$ are two constants.

  \end{algorithmic}
\end{algorithm}

\end{document}

这就是我得到的:

有什么想法可以格式化第 4-8 行吗?我必须以算法格式显示伪代码。

答案1

您的代码中的主要问题是您没有将 if-else 作为命令调用。如果您修复了这个问题,那么问题就解决了。

\begin{algorithm}
  \caption{Adaptive Thresholding}\label{euclid}

  \begin{algorithmic}[1]

    \State Convert $R$ to a binary region using the threshold $\theta_{0}$. \label{step:convert}

    \State Assume $N$ is the sum of the number of non-zero pixels within $F_{l}$ and $F_{r}$.

    \If{$N$ is larger than a predefined threshold $\beta$}
              \State $\theta_{0} = \theta_{0} + \Delta\theta$
              \State Repeat the procedure from step \ref{step:convert}
    \Else 
              \State $\theta = \theta_{0}$
    \EndIf
  \end{algorithmic}
\end{algorithm}


Where $\Delta\theta$, $\beta$ are two constants.

相关内容