算法设计风格的伪代码(Kleinberg & Tardos)

算法设计风格的伪代码(Kleinberg & Tardos)

如何用下面的样式编写伪代码?我尝试使用环境,verbatim但似乎无法使变量斜体化和符号\in正常工作。我也尝试使用algorithmicalgorithm2e包,但它们似乎比本书中使用的样式限制性更强。

在此处输入图片描述

答案1

也许您不需要额外的算法包,tabbing环境(标准 LaTeX)就足够了。

在此处输入图片描述

\documentclass{article}
\newcommand\tab{\hspace{1.5em}}
\begin{document}
{\ttfamily
\begin{tabbing}
  \tab\=\tab\=\tab\=\tab\=\tab\=\tab\=\kill
  Initially, all $m\in M$ and $w\in W$ are free\\
  While there is a man $m$ who is free and hasn't proposed to every woman\\
  \>Choose such a man $m$\\
  \>Let $w$ be the highest-ranked woman in $m$'s preference list\\
  \>\>to whom $m$ has not yet proposed\\
  \>If $w$ is free then\\
  \>\>$(m,w)$ become engaged\\
  \>Else $w$ is currently engaged to $m'$\\
  \>\>If $w$ prefers $m'$ to $m$ then\\
  \>\>\>$m$ remains free\\
  \>\>Else $w$ prefers $m$ to $m'$\\
  \>\>\>$(m,w)$ become engaged\\
  \>\>\>$m'$ becomes free\\
  \>\>Endif\\
  \>Endif\\
  Endwhile\\
  Return the set $S$ of engaged pairs
\end{tabbing}%
}
\end{document}

相关内容