1、用到的包有:
\usepackage[boxed,linesnumbered]{algorithm2e}
% 算法的宏包,注意宏包兼容性,先后顺序为float、hyperref、algorithm(2e),否则无法生成算法列表,现在大部分都在用这个包
说明文档:
http://mirror.bjtu.edu.cn/CTAN/macros/latex/contrib/algorithm2e/algorithm2e.pdf\usepackage{algorithm}
\usepackage{algorithmic} %format of the algorithm
用这两个包的不多了,好像都被algorothms包中了
说明文档:
http://mirrors.ustc.edu.cn/CTAN/macros/latex/contrib/algorithms/algorithms.pdf
我自己修改过的algorithm包,更名为myalgorithm,可以使用一种另一种显示方式plaintop
\usepackage[plaintop]{myalgorithm}
2、网上找到的algorithm2e使用的例子:
(1)、首先引用包
usepackage[lined,boxed,commentsnumbered, ruled]{algorithm2e}
(2)、其次在正文中添加如下使用范例代码:
begin{algorithm}
caption{$Cloak_{dp}(T_b, T_0)$}
SetKwData{Index}{Index}
KwIn{$T_b, T_0$}
KwOut{T}
BlankLine
$M[0][0] =$ 0;
For{$i = 1$ KwTo $T_b.len$}{
For{$j = i$ KwTo $T_0.len$}{
Index $=$ 0;
For{$k = i - 1$ KwTo $j-1$}{
If{$M[i-1][k] < M[i-1][Index]$} {
Index $= k$;
}
}
$M[i][j] = M[i-1][Index] + Area(MBC(T_b[i], T_0[j]))$;
$Pre[i][j] = Index$;
}
}
end{algorithm}
(3)、编译运行即可以生产插入代码效果。如下图:
图1
几点常见的简单说明:如果你不想结果代码中if或者for的缩进前面有竖线,那么在KwIn指令前面加上SetAlgoNoLine即可;如果你想要行号,那么在KwIn指令前面加上LinesNumbered。
源文档 <
http://www.zhongsisi.com/latex-algorithm2e/>
3、algorithm的用法
algorithmic和algorithms的用法不同,只要是一些关键字不同,如我自己写的算法:
\begin{algorithm}[htb]
\caption{I am algorithm.}
\label{alg:background}
\vspace{.1cm}
\hrule
\begin{algorithmic}[1]
\vspace{.2cm}
\REQUIRE ~\\
$L$ with $M \times N$;\\
value $(x,y)$ in $i$, $B_i(x,y)$;
\ENSURE ~\\
$M \times N = BG$;
\FOR{$x=0$ to $M-1$}
\FOR{$y=0$ to $N-1$}
\STATE $c=0;$
\STATE $t=0;$
\FOR{$i=1$ to $i=L-1$}
\IF{$<\delta$}
\STATE $c++;$
\STATE $t = t + B_i(x,y);$
\ENDIF
\ENDFOR
\STATE $BG(x,y)=t/c$;
\ENDFOR
\ENDFOR \RETURN $BG$;
\end{algorithmic}
\hrule
\end{algorithm}
图2
之前发论文的时候,编辑要求我写的算法的格式需要和表格的格式一样(标题放在算法上面,居中,且标题之上不能有横线,即上图),但是ctex自带的algorithm包只有三种格式:plain,ruled,boxed,默认是ruled,但这三种格式都不对,搜索半天,发现自己需要的格式叫做plaintop(看表格,图形和浮动环境的包中找到这个名词,属于floatstyle的一种),于是大胆修改了algorithm包,加入这种格式修改后的文件如下:
加入包时写成:
\usepackage[plaintop]{myalgorithm}
就可以得到图2的效果了。
另外,把require改成input,ensure改成output,导言区加入:
\renewcommand{\algorithmicrequire}{\textbf{Input:}} %Use Input in the format of Algorithm
\renewcommand{\algorithmicensure}{\textbf{Output:}} %UseOutput in the format of Algorithm
4、修改algorithmic和algorithmic
algorithm的标题只有英文的,为了满足自己写中文论文的需要,ok,我又把修改myalgorithm.sty了一下。对于algorithmic.sty,我也根据需要做了一些修改,重命名为
myalgorithmic.sty。所有修改总结如下:
myalgorithm.sty:
(1)增加标题标题top并居中选项,需要声明为:\usepackage[plaintop]{myalgorithm}
(2)增加中文标题:“算法”,并根据条件编译选择中文标题或英文标题,关于条件编译请看
Latex:其他用法记录,这里作部分说
%---myalgorithmic.sty----%%%%%%%
\def\myalg{john}
\ifx\myalg\mysub
\newcommand{\ALG@name}{算法}
\else
\newcommand{\ALG@name}{Algorithm}
\fi
在需要使用中文算法标题的文档开头增加定义\def\mysub{john},那么就会使用中文标题,反之不定义则使用英文标题。
myalgorithmic.sty
(1)增加命令\INITIAL,即初始化项,跟\REQUIRE等并列放在一起
分享到:
相关推荐
写文章描述算法的latex模板,简单实用
latex中编写算法,进行排版的最完美教程
algorithm.sty (1)增加标题标题top并居中选项 (2)增加中文标题:“算法”,并根据条件编译选择中文标题或英文标题 algorithmic.sty (1)增加命令\INITIAL,即初始化项
详细介绍了LaTeX写算法的过程,直接就可以编译的,对于初学者不懂算法的可以有很好的指导作用
之前发论文的时候,编辑要求我写的算法的格式需要和表格的格式一样(标题放在算法上面,居中,且标题之上不能有横线,即上图),但是ctex自带的algorithm包只有三种格式:plain,ruled,boxed,默认是ruled,但这三...
algorithm2e.sty — package for algorithms 用于编辑算法伪代码等。
NULL 博文链接:https://cherishlc.iteye.com/blog/1554793
Algorithm2e is an environment for writing algorithms in LATEX2e. An algorithm is defined as a floating object like figures. It provides macros that allow you to create different sorts of key words, ...
latex 算法宏包说明书
latex algorithm2e 算法伪代码 官方介绍文档 latex algorithm2e 算法伪代码 官方介绍文档
IEEE论文推荐使用,Windows系统可直接下载使用,亲身实践,好用
latex Algorithms伪代码规范, 详细介绍了伪代码需要调用的包,以及修改algorithms的默认设置。阐述了原理
Through a simple example, the beginner of Latex could learn it quickly.
latex cheat-sheet latex的公式编辑参考 就是Aurora的帮助文档
ieee access的论文写作latex模板;可以使用tes studio等进行编辑
算法导论答案solution to CLR
该文档中包含详细的LaTeX入门学习方法(其中包括CTeX下载链接,会议模版下载链接,在线LaTeX链接等众多在线学习链接),计算复杂度学习(科学术科研方面,及文献下载链接),EndNote入门学习(下载链接和教学视频...
一步一步写算法,一些好的算法。可以参考哈
K均值算法, 通俗易懂. 本人用latex排版, 看起来很清晰! 觉得好请给个赞
所有文章均使用LaTeX以Markdown格式编写,并且位于docs文件夹中。 演算法 算法文章的集合。 每篇文章的结构: 对算法的说明以及说明和可能的证明。 实现(C ++)。 应用。 任务。 课程 一切 测试服务器 储存库...