Latex 中的格式化-伪代码

Latex 中的格式化-伪代码

我正在尝试在 Latex 中创建一种类似于下面显示的伪代码图像

我尝试的是:

1. Convert R to a binary region using the threshold $\theta_{0}$\\

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

3. If N is larger than a predefined threshold $\beta$, then\\

            \begin{center}
            $\theta_{0} = \theta_{0} + \Delta\theta$\\
            Repeat the procedure from step 1
            \end{center}
   Else

            \begin{center}
            $\theta = \theta_{0}$
            \end{center}
   End

但结果并不如我所愿。有人能建议如何解决这个问题吗?

答案1

一个“手工制作”的解决方案,重现您展示的布局

\documentclass{article}
\usepackage{enumitem}
\newlist{pseudocode}{enumerate}{2}
\setlist[pseudocode,1]{label=\arabic*.,ref=\arabic*}
\setlist[pseudocode,2]{label={},ref=\arabic*,nosep}

\begin{document}

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

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

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

\end{document}

在此处输入图片描述

答案2

这是一个algorithm2e执行:

在此处输入图片描述

\documentclass{article}
\usepackage{algorithm2e}
\LinesNumbered% Number algorithm lines
\usepackage{amsmath}
\begin{document}

\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{$R$, $\beta$}
  \KwResult{$\theta_0$}
  Convert~$R$ to a binary region using the threshold $\theta_{0}$\; \label{first_step}
  Assume~$N$ is the sum of the number of non-zero pixels within~$F_l$ and~$F_r$\;
  \eIf{\text{$N$ is larger than a predefined threshold~$\beta$}}{
    $\theta_{0} = \theta_{0} + \Delta\theta$\;
    Repeat the procedure from Step~\ref{first_step}\;
  }{
    $\theta = \theta_{0}$\;
  }  
  \caption{How to write algorithms}
\end{algorithm}

\end{document}

答案3

最简单的方法是使用{enumerate}和普通方程环境(在本例中 –{equation}可防止编号)。文档类选项将方程设置为左对齐;省略此选项可使其居中。{gather}*fleqn

简单的伪代码

\documentclass[fleqn]{article}

\usepackage{mathtools}

\begin{document}
\begin{enumerate}
\item Convert R to a binary region using the threshold $\theta_{0}$
\item Assume N is the sum of the number of non-zero pixels within $F_{l}$ and $F_{r}$.
\item If N is larger than a predefined threshold $\beta$, then
    \begin{gather*}
    \theta_{0} = \theta_{0} + \Delta\theta\\
    \text{Repeat the procedure from step 1}
    \end{gather*}
    Else
    \begin{equation*}
    \theta = \theta_{0}
    \end{equation*}
    End
\end{enumerate}
\end{document}

更多信息可以在包装手册和 LaTeX 初学者书籍中找到 ;-)

答案4

宾果!!!我找到了解决方案!效果非常好 :) 记得使用:\usepackage{algpseudocode}

\begin{algorithmic}
\State Convert R to a binary region using the threshold $\theta_{0}$\\

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

\If {$N \textgreater \beta$}
    \State $\theta_{0} = \theta_{0} + \Delta\theta$
    \State Repeat the procedure from step 1
\Else
    \State $\theta = \theta_{0}$
\EndIf

\end {algorithmic}  

相关内容