LaTeX 写作: 算法代码排版 --latex2e范例总结

latex2e 宏包的使用范例:

  • \usepackage[ruled]{algorithm2e}                                     %算法排版样式1
  • \usepackage[ruled,vlined]{algorithm2e}                          %算法排版样式2
  • \usepackage[linesnumbered,boxed]{algorithm2e}        %算法排版样式3

使用上述代码中三种不同的宏包编译后的代码排版样式预览:

样式一:

样式二:

样式三:

latex代码排版.tex文件: 

\documentclass{article}
\usepackage[ruled]{algorithm2e} %算法排版样式1
%\usepackage[ruled,vlined]{algorithm2e} %算法排版样式2
%\usepackage[linesnumbered,boxed]{algorithm2e} %算法排版样式3

\begin{document} How to use the algorithm2e in \LaTeX ~ file. Examples:
% ------------------------------Example - ---------------------------------------------
\begin{algorithm}[H]
\caption{How to write algorithms}
\KwIn{this text}
\KwOut{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\;
}
}
\end{algorithm} % ---------------------------Example - ------------------------------------------------
\begin{algorithm}
% \SetAlgoNoLine %去掉之前的竖线
\caption{identifyRowContext}
\KwIn{$r_i$, $Backgrd(T_i)$=${T_1,T_2,\ldots ,T_n}$ and similarity threshold $\theta_r$}
\KwOut{$con(r_i)$}
$con(r_i)= \Phi$\;
\For{$j=;j \le n;j \ne i$}
{
float $maxSim=$\;
$r^{maxSim}=null$\;
\While{not end of $T_j$}
{
compute Jaro($r_i,r_m$)($r_m\in T_j$)\;
\If{$(Jaro(r_i,r_m) \ge \theta_r)\wedge (Jaro(r_i,r_m)\ge r^{maxSim})$}
{
replace $r^{maxSim}$ with $r_m$\;
}
}
$con(r_i)=con(r_i)\cup {r^{maxSim}}$\;
}
return $con(r_i)$\;
\end{algorithm}
%--------------------------------------------------------------------------------------
some special information
The algorithm2e LaTeX package conflicts with several others over the use of the algorithm identifier. A common indicator
is something like this message:
To resolve the issues, simply put the following just before the inclusion of the algorithm2e package: %\makeatletter
%\newif\if@restonecol
%\makeatother
%\let\algorithm\relax
%
\let\endalgorithm\relax \end{document}

中文latex模式下,更改Input为输入,更改Output为输出的方法: 在算法内部插入、

\SetKwInOut{KIN}{输入}

\SetKwInOut{KOUT}{输出}

用我们定义的新的宏名“\KIN” 在算法开头相应位置替换掉自带的\KwInput,用"\KOUT" 替换掉\KwOutput 即可,实现中文的输入和输出.

具体事例如下:

 \renewcommand{\algorithmcfname}{算法}
\begin{algorithm}[H]
%\SetAlgoNoLine
4 \SetKwInOut{KIN}{输入}
5 \SetKwInOut{KOUT}{输出}
%\BlankLine %空一行
\caption{标准DE算法 }
\label{DE_algo} %
\KIN{Population: $ M$; Dimension: $ D $; Genetation: $ T $ }
\KOUT{The best vector (solution) $ \varDelta $ }
$ t \leftarrow (initialization) $\;
\For{$i=$ to $ M $ }
{\For{$j=$ to $ D $}
{
$ {x}_{i,t}^j=x_{min}^j + rand(,)\cdotp (x_{max}^j-x_{min}^j) $\;
}
}
%--------------------------------------------------
\While{$(|f(\varDelta)| \geq\varepsilon )$ or $(t \leq T )$}
{
\For{$ i=$ to $M$}
{
\emph{$ \blacktriangleright $ (Mutation and Crossover)}\\
%\textit{ $ \blacktriangleright $ (Mutation and Crossover) }\\
\For{$j=$ to $ D $}
{
$ v_{i,t}^j =Mutation(x_{i,t}^j)$\;
$ u_{i,t}^j =Crossover(x_{i,t}^j,v_{i,t}^j)$\;
}
\emph{$ \blacktriangleright $ (Greedy Selection)}\\
%\textit{ $ \blacktriangleright $ (Greedy Selection) }\\
\eIf{$ f(\textbf{u}_{i,t}) < f(\textbf{x}_{i,t}) $}
{
$ \textbf{x}_{i,t} \leftarrow\textbf{u}_{i,t}$\;
\If{$ f(\textbf{x}_{i,t}) < f(\varDelta)$}
{
$ \varDelta \leftarrow \textbf{x}_{i,t}$ \;
}
}
{
$ \textbf{x}_{i,t} \leftarrow \textbf{x}_{i,t} $\;
}
}
$ t \leftarrow t+ $\;
} %While
\Return the best vector $\varDelta$\;
\end{algorithm}


LaTeX 算法代码排版 --latex2e范例总结的更多相关文章

  1. Latex 算法过长 分页显示方法

    参考: Algorithm tag and page break Latex 算法过长 分页显示方法 1.引用algorithm包: 2.在\begin{document}前加上以下Latex代码: ...

  2. latex算法步骤如何去掉序号

    想去掉latex算法步骤前面的序号,如下 我想去掉每个算法步骤前面的数字序号,1,2,3,因为我已经写了step.我们只需要引用a lgorithmic这个包就可以了,代码如下: \usepackag ...

  3. KMP算法代码

    以下是本人根据上一篇博客随笔http://www.cnblogs.com/jiayouwyhit/p/3251832.html,所写的KMP算法代码(暂未优化),个人认为在基于上一篇博客的基础上,代码 ...

  4. 算法代码[置顶] 机器学习实战之KNN算法详解

    改章节笔者在深圳喝咖啡的时候突然想到的...之前就有想写几篇关于算法代码的文章,所以回家到以后就奋笔疾书的写出来发表了 前一段时间介绍了Kmeans聚类,而KNN这个算法刚好是聚类以后经常使用的匹配技 ...

  5. swift代码排版-参考

    代码排版包括: 空行.空格.断行和缩进等内容.代码排版内容比较多工作量很多,但是非常重要. 空行 空行将逻辑相关的代码段分隔开,以提高可读性.下列情况应该总是添加空行: 类型声明之前. import语 ...

  6. 经常使用MD5算法代码

    经常使用的MD5算法代码日期: 2014年8月4日作者: 铁锚 MD5,全称为 Message Digest Algorithm 5(消息摘要算法第五版).详情请參考 维基百科:MD5  MD5加密后 ...

  7. 带你找到五一最省的旅游路线【dijkstra算法代码实现】

    算法推导过程参见[dijkstra算法推导详解] 此文为[dijkstra算法代码实现] https://www.cnblogs.com/Halburt/p/10767389.html package ...

  8. L3-019 代码排版 (30 分)

    某编程大赛中设计有一个挑战环节,选手可以查看其他选手的代码,发现错误后,提交一组测试数据将对手挑落马下.为了减小被挑战的几率,有些选手会故意将代码写得很难看懂,比如把所有回车去掉,提交所有内容都在一行 ...

  9. LDPC译码算法代码概述

    程序说明 V0.0 2015/1/24 LDPC译码算法代码概述   概述   本文介绍了包括LDPC_Simulation.m, ldpcdecoderbp1.m,ldpcdecoderminsum ...

随机推荐

  1. JS跳出框架返回上一页

    链接部分 <a class="link" href="javascript:;" target="_top" onclick=&quo ...

  2. 纯CSS下拉导航菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="h ...

  3. cygwin-安装断点续传

    本文转载http://blog.chinaunix.net/uid-20178959-id-1731456.html 本文主要介绍正常下载安装(http://www.cnblogs.com/hwagg ...

  4. MWeb

    专业的 Markdown 写作支持 极简 UI.Dark Mode.漂亮的 Markdown 语法高亮.列表缩进优化,提供 5 种主题选择. 除了支持基本的 Markdown 语法外,还支持大量 Ma ...

  5. 【BZOJ-3712】Fiolki LCA + 倍增 (idea题)

    3712: [PA2014]Fiolki Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 303  Solved: 67[Submit][Status] ...

  6. HTLM4与HTML5的区别

    准确的说应该是HTML4与HTML5的区别 主要区别: 1.doctype头的改变 2.html5中多出了很多标签和属性 还有一个要注意的是,我们通常说html5时会自动关联上css3,其实我理解的, ...

  7. Uva10881 Piotr's Ants

    蚂蚁相撞会各自回头.←可以等效成对穿而过,这样移动距离就很好算了. 末状态蚂蚁的顺序和初状态其实是相同的. 那么剩下的就是记录每只蚂蚁的标号,模拟即可. /*by SilverN*/ #include ...

  8. dataGrid查询表格

    waf("#Gird").dataGrid("reloadGrid") //重新发请求去装载数据 waf("#Grid").wafGrid( ...

  9. ansible模块lineinfile

    示列: sshd_set.yaml --- - hosts: test remote_user: root gather_facts: False tasks: - name: set hostnam ...

  10. 简单二维元胞自动机 MATLAB实现

    20世纪50年代,乌尔姆和冯·诺依曼(对此人真是崇拜的五体投地)为了研究机器人自我复制的可能性,提出了一种叫做元胞自动机(Cellular Automaton,CA)的算法.该算法采用局相互作用规则, ...