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. git 删除和重命名文件

    1. 删除文件 git rm a.txt git rm--cached filename 会删除索引中的文件并把它保留在工作目录中 而 git rm 则会将文件从索引和工作目录中都删除 文件删除后的恢 ...

  2. jquery.ui.widget详解

    案例详解 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  3. 内存溢出OOM与内存泄漏ML

    附, 微信团队原创分享:Android内存泄漏监控和优化技巧总结 一.如何避免OOM 异常 想要避免OOM 异常首先我们要知道什么情况下会导致OOM 异常. 1.图片过大导致OOM Android 中 ...

  4. HQL多种查询实现

    1.返回int public int countByUsername(String username) { String hql = "select count(*) from BeanCa ...

  5. 79.Android之动画基础

    转载:http://a.codekk.com/detail/Android/lightSky/%E5%85%AC%E5%85%B1%E6%8A%80%E6%9C%AF%E7%82%B9%E4%B9%8 ...

  6. Leetcode 98. Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  7. Chrome编辑了样式或者JS之后自动同步回本地工程

    比如我用HBuilder进行调试,在Chrome上修改了几个样式,我们通常的做法是,拷贝修改后的地方,然后再进行覆盖. 现在有了全自动的操作,在Chrome上修改了样式之后,通过Sources的Sav ...

  8. 解析window.open链接的参数

    ); var arr = new Array(); arr = str.split("&"); ){ ; i<arr.length; i++){ ){ ); if(p ...

  9. Myeclipse的show in breadcrumb

    m如何取消eclipse的show in breadcrumb 不小心点了show in breadcrumb,在编辑器界面上面多一层路径条,多余碍事,不晓得怎么取消,搞了半天终于弄好,方法如下: 点 ...

  10. POJ1025 Department

    POJ1025 是一道模拟题. 这题第一个障碍是现在少见的循环电梯 ('pater-noster' elevator) ”The building has `pater-noster' elevato ...