用LaTeX优雅地书写伪代码:Algorithm2e简明指南
点击蓝字
关注我们

极市导读
1. 准备
\usepackage[ruled,linesnumbered]{algorithm2e}
ruled是让标题显示在上面,否则算法的标题则在下面。linesnumbered让算法中显示行号。还可以添加 boxed, 让算法排版时插入在一个盒子里。
2. 基本语法

如果你不想让你的伪代码叫做 'Algorithm 编号', 可以使用
\renewcommand{\algorithmcfname}{算法名}命令来修改。除了\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。
3. 例子
例子1
\def\SetClass{article}\documentclass{\SetClass}\usepackage[lined,boxed,commentsnumbered]{algorithm2e}\begin{document}\begin{algorithm}[H]\SetAlgoLined\KwData{this text}\KwResult{how to write algorithm with \LaTeX2e }initialization\;\While{not at end of this document}{read current\;\eIf{understand}{go to next section\;current section becomes this one\;}{go back to the beginning of current section\;}}\caption{How to write algorithms}\end{algorithm}\end{document}

例子2
\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}

例子3
\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}

例子4
\SetKwRepeat{Do}{do}{while}
\Do{<结束条件>}{<执行命令>}
\documentclass{article}\usepackage[linesnumbered, ruled]{algorithm2e}\SetKwRepeat{Do}{do}{while}%\begin{document}\begin{algorithm}[H]\KwData{this text}\KwResult{how to write algorithm with \LaTeX2e }initialization\;\While{not at end of this document}{read current\;\Repeat{this end condition}{do these things\;}\eIf{understand}{go to next section\;current section becomes this one\;}{go back to the beginning of current section\;}\Do{this end condition}{do these things\;}}\caption{How to write algorithms}\end{algorithm}\end{document}


写在最后
https://zhuanlan.zhihu.com/p/58293008
https://blog.csdn.net/robert_chen1988/article/details/71512914
https://blog.csdn.net/yq_forever/article/details/89815562
推荐阅读
发SCI很难,但这套方法帮我读研期间发了5篇SCI,所有的方法和技巧都在这了
SCI写作常用句式总结,帮你迅速提升paper档次
PyTorch Trick集锦
收藏!撰写 SCI 论文时好用的软件技巧总结

评论
