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. C#-WinForm-进程、线程

    进程:一个程序就是一个进程,也有可能一个程序需要多个进程来支持的情况,比如QQ 点击按钮打开记事本,静态方法 public partial class Form3 : Form { public Fo ...

  2. 数据库开发基础-SQl Server 存储过程

    存储过程: 存储过程(stored procedure)有时也称为sproc.存储过程存储于数据库中而不是在单独的文件中,有输入参数.输出参数以及返回值等. 在数据库中,创建存储过程和创建其他对象的过 ...

  3. Linux命令:修改文件权限命令chmod、chgrp、chown详解

    Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作. 文件或目录的访问权 限分为只读,只写和可执行三种.以文件为例,只读权限表示只允许读其内容,而 ...

  4. iis Server Error in '/' Application

    1.开始-运行-cmd-输入cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727-回车-aspnet_regiis.exe -i 回车 2.如果不是检查链接 ...

  5. perl sub

    #/usr/bin/perl -w use strict; my $usage = "\n\nusage: $0 <length>\n\n"; my $length = ...

  6. Java多线程与并发库高级应用-工具类介绍

    java.util.concurrent.Lock 1.Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也应该是一个对象.两个线程执行的代码片段要实现同步互 ...

  7. [cf140e]New Year Garland

    Description 用$m$种颜色的彩球装点$n$层的圣诞树.圣诞树的第$i$层恰由$l[i]$个彩球串成一行,且同一层内的相邻彩球颜色不同,同时相邻两层所使用彩球的颜色集合不同. 求有多少种装点 ...

  8. 【BZOJ-1218】激光炸弹 前缀和 + 枚举

    1218: [HNOI2003]激光炸弹 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1778  Solved: 833[Submit][Statu ...

  9. BZOJ 1088 扫雷Mine

    今天做了几道BZOJ的题,发现统观题目时还是很多很多都不会的,不过还是有几道时可以作的,以后要慢慢加强,争取多做题 BZOJ 1088 扫雷 其实本人平常不大玩扫雷的,就算玩也不是很好,不过看n*2的 ...

  10. 负载均衡下的资源文件配置/多站点下的资源文件夹共享(Windows IIS)

    前言: 负载均衡用的是NLB,微软的方案不太靠谱,举个例子吧,AB两台服务器负载出C,如果用户访问访问C之后分配的是A,那么如果A挂了,是不会自动切换到B的.据说后来还有一种NLB的方案可以实现,也不 ...