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-d3画图插件

    d3.js下载 官方网站:http://d3js.org/ github:https://github. com/mbostock/d3/tags forks最新:https://github.com ...

  2. 控件 UI: 字体的自动继承的特性, Style, ControlTemplate

    字体的自动继承的特性 Style 样式 ControlTemplate 控件模板 示例1.演示字体的自动继承的特性Controls/UI/FontInherit.xaml <Page x:Cla ...

  3. Visual Studio 2015正式版/产品密钥

    Visual Studio 2015  发行说明: https://visualstudio.com/zh-cn/news/vs2015-vs.aspx Visual Studio  2015 特性简 ...

  4. tarjan求桥、割顶

    若low[v]>dfn[u],则(u,v)为割边.但是实际处理时我们并不这样判断,因为有的图上可能有重边,这样不好处理.我们记录每条边的标号(一条无向边拆成的两条有向边标号相同),记录每个点的父 ...

  5. 使用redis避免客户端频繁提交数据

    避免客户端频繁向服务器提交表单的解决方案 使用redis 在order的model中增加函数 ) { try{ $key = "lock_" . $key; $num = $thi ...

  6. 面向对象_python

    面向对象_python 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定 ...

  7. java web项目导入到eclipse中变成了java项目的一种情况的解决办法

    前提,我把代码上传到github上之后,在另外一台电脑上拉下之后,先报出现的是jre不对,然后换成了当前的jre,然后红色的感叹号消失了但是之前项目上那个小地球不见了,也就是说变成了java项目. - ...

  8. 数论专项测试——约数个数和(lucas的数论)

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  9. asp.net生产环境和开发环境的错误日志包装策略

    对于错误日志的输出,我们借助web.config的两个标志位: <!--全局包装异常处理页面,只有在PageError和Application_Error做清除错误操作才可不跳转--> & ...

  10. editGrid分录表格

    waf("分录id").wafGrid("setCellEditorAllConfig", "字段名", "filteritem& ...