Latex使用algorithm2e包写伪代码

本文介绍了如何在Latex中使用Algorithm2e包来编写伪代码,包括导入包、设置选项、基本语法和注释的使用,以及展示了多个示例,包括中文伪代码的编写方法。
摘要由CSDN通过智能技术生成

用Latex写伪代码我们需要用到一个包,Algorithm2e,这个工具包的使用手册下载地址为(http://mlg.ulb.ac.be/files/algorithm2e.pdf)CSDN的链接为()

准备

导入该包

\usepackage[ruled,linesnumbered]{algorithm2e}
  • ruled 是让标题显示在上面,否则算法的标题则在下面。
  • linesnumbered 让算法中显示行号。

这种选项在使用手册的第四页到第六页

基本语法

代码含义
;行末添加分号并自动换行
\caption{输入信息}插入标题
\KwData{输入信息}Data:输入信息
\KwIn{输出入信息}In:输入信息
\KwOut{输出信息}Out:输出信息
\KwResult{输出信息}Result:输出信息
\For{条件}{循环语句}for 条件 do
        循环语句
endo
\If{条件}{肯定语句}if 条件 do
        肯定语句
end
\while{条件}{循环语句}while 条件 then
        循环语句
end
\tcc{注释}/* 注释 */
\tcp{注释}// 注释
\elf{条件}{肯定语句}{否定语句}if 条件 then
        肯定语句
else
        否定语句
end
  • 除了\If, \Else, \ElseIf之外,还有\uIf, \lIf, \uElse, \lElse, \uElseIf, \lElseIf等命令,他们的区别在于

  • \If, \Else, \ElseIf都是会以end结尾
    \uIf, \uElse, \uElseIf, 是不以end结尾的块级元素
    \lIf, \lElse, \lElseIf 是不以end为结尾的行内元素
    在If-else结构中,\eIf 自带else(即 if 和 else 共用一个 end),而只是用 \If 和 \Else 的话则会多出一个end给Else。

基本语法在手册的第六页到第九页。手册写的真的非常详细。

示例

我们还是从手册上摘抄几个例子

示例一
\def\SetClass{article}
\documentclass{\SetClass}
\usepackage[linesnumbered,lined,boxed,commentsnumbered]{algorithm2e}
\begin{document}
\IncMargin{1em}
\begin{algorithm}
  \SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
  \SetKwFunction{Union}{Union}\SetKwFunction{FindCompress}{FindCompress}
  \SetKwInOut{Input}{input}\SetKwInOut{Output}{output}

  \Input{A bitmap $im$ of size $w\times l$}
  \Output{A partition of the bitmap}
  \BlankLine
  \emph{special treatment of the first line}\;
  \For{$i\leftarrow 2$ \KwTo $l$}{
    \emph{special treatment of the first element of line $i$}\;
    \For{$j\leftarrow 2$ \KwTo $w$}{\label{forins}
      \Left$\leftarrow$ \FindCompress{$Im[i,j-1]$}\;
      \Up$\leftarrow$ \FindCompress{$Im[i-1,]$}\;
      \This$\leftarrow$ \FindCompress{$Im[i,j]$}\;
      \If(\tcp*[h]{O(\Left,\This)==1}){\Left compatible with \This}{\label{lt}
        \lIf{\Left $<$ \This}{\Union{\Left,\This}}
        \lElse{\Union{\This,\Left}}
      }
      \If(\tcp*[f]{O(\Up,\This)==1}){\Up compatible with \This}{\label{ut}
        \lIf{\Up $<$ \This}{\Union{\Up,\This}}
        \tcp{\This is put under \Up to keep tree as flat as possible}\label{cmt}
        \lElse{\Union{\This,\Up}}\tcp*[h]{\This linked to \Up}\label{lelse}
      }
    }
    \lForEach{element $e$ of the line $i$}{\FindCompress{p}}
  }
  \caption{disjoint decomposition}\label{algo_disjdecomp}
\end{algorithm}\DecMargin{1em}
\end{document}

请添加图片描述

示例二
\def\SetClass{article}
\documentclass{\SetClass}
\usepackage[ruled,linesnumbered]{algorithm2e}
\begin{document}
	\begin{algorithm}
		\caption{Simulation-optimization heuristic}\label{algorithm}
		\KwData{current period $t$, initial inventory $I_{t-1}$, initial capital $B_{t-1}$, demand samples}
		\KwResult{Optimal order quantity $Q^{\ast}_{t}$}
		$r\leftarrow t$\;
		$\Delta B^{\ast}\leftarrow -\infty$\;
		\While{$\Delta B\leq \Delta B^{\ast}$ and $r\leq T$}{$Q\leftarrow\arg\max_{Q\geq 0}\Delta B^{Q}_{t,r}(I_{t-1},B_{t-1})$\;
			$\Delta B\leftarrow \Delta B^{Q}_{t,r}(I_{t-1},B_{t-1})/(r-t+1)$\;
			\If{$\Delta B\geq \Delta B^{\ast}$}{$Q^{\ast}\leftarrow Q$\;
				$\Delta B^{\ast}\leftarrow \Delta B$\;}
			$r\leftarrow r+1$\;}
	\end{algorithm}
\end{document}

请添加图片描述

示例三

考虑到可能涉及到中文的伪代码,我们写一下中文的

\documentclass{article}
\usepackage[lined,boxed,commentsnumbered]{algorithm2e}
\usepackage{xeCJK}
\begin{document}
	\begin{algorithm}[H]
		\SetAlgoLined
		\KwResult{怎样使用 algorithm \LaTeX2e }
		
		初始化所有寄存器\;
		\While{永不停止}{
			读取寄存器值\;
			\eIf{寄存器值为1}{
				写日志\;
				跳出循环\;
			}{
				读硬盘\;
			}
		}
	\end{algorithm}
\end{document}

请添加图片描述

其实是一样的,只是部分英文换成了中文。

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LyaJpunov

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值