LaTex算法代码排版-algorithm2e

1.使用如下宏包

\usepackage[ruled]{algorithm2e} %带竖线
%\usepackage[ruled,vlined]{algorithm2e} %带竖线和折线
%\usepackage[linesnumbered,boxed]{algorithm2e} %方框格式

2.示例代码

\begin{algorithm}
%\SetAlgoLined
%\SetAlgoNoLine %去掉竖线
\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}

3.生成效果

3.1. \usepackage[ruled]{algorithm2e}
在这里插入图片描述

3.2.\usepackage[ruled,vlined]{algorithm2e}
在这里插入图片描述
3.3.\usepackage[linesnumbered,boxed]{algorithm2e}
在这里插入图片描述
3.4. \SetAlgoNoLine
在这里插入图片描述

4.参考链接

  1. LaTeX 写作: 算法代码排版 --latex2e范例总结
  2. LaTeX技巧630:用LaTeX寫algorithm
  3. LaTeX/Algorithms-WIKIBOOKS
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值