`
he91_com
  • 浏览: 373846 次
文章分类
社区版块
存档分类
最新评论

Latex:算法

 
阅读更多
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等并列放在一起

这两个文件在此下载

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics