latex 伪代码_用LaTeX优雅地书写伪代码:Algorithm2e简明指南

公众号关注 “ ML_NLP ” 设为 “ 星标 ”,重磅干货,第一时间送达!

cf4572743f821346900bcb0ca27d9b1b.png

作者丨薰风初入弦 来源|https://zhuanlan.zhihu.com/p/166418214 本文已获作者授权,禁止二次转载

极市导读

  LaTeX作为理工科科研人员的必备工具,熟练掌握可以极大地提高论文写作的效率。本文介绍了一个好用的工具包,帮助你优雅地书写伪代码。

1. 准备

该工具包的使用手册下载地址:http://mlg.ulb.ac.be/files/algorithm2e.pdf
\usepackage[ruled,linesnumbered]{algorithm2e}
使用宏包时中括号的参数含义:
  • ruled 是让标题显示在上面,否则算法的标题则在下面。
  • linesnumbered 让算法中显示行号。
  • 还可以添加 boxed, 让算法排版时插入在一个盒子里。

2. 基本语法

书写代码时也有一些专门的命令:
320db293c9a3ae337fdada48a49d6b16.png
强烈建议知乎支持Markdown表格EX:
  • 如果你不想让你的伪代码叫做 '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. 例子

这些例子可以直接放在一个空的.tex文件中进行编译。

例子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}
549c3be4ad73ebad43f900cd8b6d3f07.png

例子2

现在,我们在输入输出中间加一点点间隔,然后给算法的某些行进行强调,再给if条件加上注释呢
\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 $        \lElse{\Union{\This,\Left}}      }      \If(\tcp*[f]{O(\Up,\This)==1}){\Up compatible with \This}{\label{ut}        \lIf{\Up $        \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}
125e9ec860e132b75cc532c9f7612dbe.png

例子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}
7f5d4cf620b35f2e709d4a6e7e52ca05.png

例子4

Algorithm2e本身不支持Do-While结构(支持的是While-Do),需要自行定义。不过自行定义并不难,因为宏包中内置了Repeat-Until结构,在Algorithm2e中是“宏指令(Repeat macros)”的一种[1]自定义宏指令
\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}
1f47d1799fcacfec8b23185b2fd1ab7d.png
7fdfe4e153524c075f5ac53d247bb948.png

写在最后

LaTeX,说难也难,说不难也不难。说它难,是因为其本身相当于一种较为独立的编程语言。熟练的掌握它,需要将以往我们在Word中“所见即所得”的写作思维彻底转变为“所想即所得”的思维。但是,它也很简单。实际上,我从下载第一个CTeX包,到本科第一篇论文写出来,只花了三天。LaTeX虽然涉及很多复杂与繁琐的设定,但是如果只是想粗略地入个门,只需要一个手把手带你配环境的小白向教程以及一个可以作为工具书查阅的工具书即可。(当然,系统学习LaTeX将会让你之后写论文的速度和版面美观度突飞猛进)针对那些TeX小白用户,或者不太熟练的用户,我首先推荐我同门师兄小鱼学长写的教程~白小鱼:【小白向】LaTeX 中文入门
https://zhuanlan.zhihu.com/p/58293008最后,LaTeX作为理工科科研人员的必备工具,熟练掌握可以极大地提高论文写作的效率。所以不论你是为了给日后宝贵的实验节省时间,还是为了更加优美地排版出想要的书籍, 拥有一本详尽且好懂的LaTeX教程,总比遇到问题了和无头苍蝇乱撞一样翻知乎和博客来得高效。因此,我在这里推荐我当时入门LaTeX使用的教材,可以在学有余力的时候系统性学习也可以在遇到问题时作为高效查阅的工具书:《LATEX入门》(刘海洋著) 参考文献 https://zhuanlan.zhihu.com/p/105582648
https://blog.csdn.net/robert_chen1988/article/details/71512914
https://blog.csdn.net/yq_forever/article/details/89815562 参考 1.https://tex.stackexchange.com/questions/212301/do-while-loop-in-algorithm2e更新于2020/8/10:感谢@纯粹大佬的补充,Algorithm2e本身不支持Do-While结构(支持的是While-Do),需要自行定义。具体的定义方法请见最后一个例子。

仓库地址共享:

在机器学习算法与自然语言处理公众号后台回复“代码”

即可获取195篇NAACL+295篇ACL2019有代码开源的论文。开源地址如下:https://github.com/yizhen20133868/NLP-Conferences-Code

重磅!忆臻自然语言处理-Pytorch交流群已正式成立

群内有大量资源,欢迎大家进群学习!

注意:请大家添加时修改备注为 [学校/公司 + 姓名 + 方向]

例如 —— 哈工大+张三+对话系统。

号主,微商请自觉绕道。谢谢!

6491d9af4f0623ad4005ce59ff4b8b56.png

21be921c9bb06814643f2a29ae714c31.png

推荐阅读:

常用 Normalization 方法的总结与思考:BN、LN、IN、GN

人人都能看懂的LSTM

Python “偏函数” 用法全方位解析

3f821816a3de038cbf2980fd65c073e3.png

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值