Will's Blog
653 字
3 分钟
在 LaTeX 中使用算法伪代码

关于算法伪代码#

算法伪代码是一种用不拘泥于形式的自然语言的方式来描述算法的方法.它的目的是使算法更容易理解,同时也使得算法的描述更加清晰.

在 LaTeX 中使用算法伪代码#

在 LaTeX 中使用算法伪代码的方案有很多, 比如:

  • algorithm+algorithmic
  • algorithm+algorithmicx
  • algorithm2e

在前两种方案中, algorithm宏包用于生成算法的环境 (标题之类) algorithmic宏包用于定义算法的命令. 而algorithm2e宏包则是直接实现了算法伪代码的功能.

本文介绍的是 algorithm+algorithmicx 方案.

使用#

导入宏包#

在导言区使用\usepackage{algorithm, algpseudocode}导入宏包.

其中, algpseudocode宏包提供了格式. algorithm宏包提供了算法环境.

根据algorithmicx宏包的文档, 已经不需要再使用\usepackage{algorithmicx}导入algorithmicx宏包了.

算法环境#

一个实例:

\begin{algorithm}
\caption{算法标题}
\label{alg:label}
\begin{algorithmic}[1]
\Procedure{ProcedureName}{$arg1, arg2$}
\State $state1$
\State $state2$
\EndProcedure
\end{algorithmic}
\end{algorithm}

其中:

  1. 伪代码内容使用algorithmic环境包裹.
  2. \begin{algorithmic}[1]中的[1]表示每一行编号一次.留空则不编号.
  3. \caption, \label用于设置标题和标签,和其他盒子环境一样.

语法#

注意:

  • 伪代码中的关键字都是首字母大写的.
  • 陈述语句使用\State命令开头来实现自动缩进.
  • 如果空行,使用Statex命令.

赋值语句#

\State $var \gets expr$

注意:\gets命令需要数学模式.

条件语句#

\If{<condition>}
    \State <statement>
\ElsIf{<condition>}
    \State <statement>
\Else
    \State <statement>
\EndIf

循环语句#

  • while 循环:
\While{<condition>}
    \State <statement>
\EndWhile
  • for 循环:
\For{<condition>}
    \State <statement>
\EndFor
  • while 循环
\While{<condition>}
    \State <statement>
\EndWhile
  • repeat 循环
\Repeat
    \State <statement>
\Until{<condition>}

代码块#

\Begin
    \State <statement>
\End

函数#

\Fuction{<functionname>}{<arg1>, <arg2>, ...}
    \State <statement>
\EndFunction

过程#

\Procedure{<procedurename>}{<arg1>, <arg2>, ...}
    \State <statement>
\EndProcedure

输入输出#

\Require{<condition>}
\Ensure{<condition>}

注意:\Require\Ensure命令默认显示的是RequireEnsure两个单词, 如果想要显示成InputOutput, 可以重定义这两个命令.

\renewcommand{\algorithmicrequire}{\textbf{Input:}}
% Use Input in the format of Algorithm
\renewcommand{\algorithmicensure}{\textbf{Output:}}
% Use Output in the format of Algorithm

注释#

\State $x\gets0$\Comment{This is a comment}
\If {$x\leq1$}
\State $x\gets x+1$
\EndIf

样例#

\begin{algorithm}
    \caption{test}
    \label{alg:label}
    \begin{algorithmic}[1]
        \Require $G=(V,E)$, $u \in V$, $l \in V \rightarrow \mathbb{R}$, $p \in V \rightarrow V$
        \Ensure $l \in V \rightarrow \mathbb{R}$, $p \in V \rightarrow V$
        \Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$}
        \ForAll {$v \in V(G)$}\Comment{this is a comment}
        \State $l(v) \leftarrow \infty$
        \EndFor
        \State $l(u) \leftarrow 0$
        \Repeat
        \For {$i \leftarrow 1, n$}
        \State $min \leftarrow l(v_i)$
        \For {$j \leftarrow 1, n$}
        \If {$min > e(v_i, v_j) + l(v_j)$}
        \State $min \leftarrow e(v_i, v_j) + l(v_j)$
        \State $p(i) \leftarrow v_j$
        \EndIf
        \EndFor
        \State $l'(i) \leftarrow min$
        \EndFor
        \State $changed \leftarrow l \not= l’$
        \State $l \leftarrow l’$
        \Until{$\neg changed$}
        \EndProcedure
        \Statex
        \Function {FindPathBK}{$v$, $u$, $p$}
        \If {$v = u$}
        \State \textbf{Write} $v$
        \Else
        \State $w \leftarrow v$
        \While {$w \not= u$}
        \State \textbf{Write} $w$
        \State $w \leftarrow p(w)$
        \EndWhile
        \EndIf
        \EndFunction
    \end{algorithmic}
\end{algorithm}

参考#

在 LaTeX 中使用算法伪代码
https://will-c137.vercel.app/posts/在latex中使用算法伪代码/
作者
Will
发布于
2023-04-05
许可协议
CC BY-NC-SA 4.0