使用algorithm2e宏包实现算法排版

宏包使用 2019-10-26 10:39  浏览 :7841
算法是技术报告、学术论文、学位论文等文体中一个重要的构成元素,在LaTeX中,可以使用algorithm2e宏包实现**算法排版**。 ## 引入宏包 algorithm2e宏包已包含在TeXLive发行版中(其它发行版请自查),无需再独立安装。在导言区中引入宏包: ```tex \\usepackage{algorithm2e} ``` 之后,便可以使用该宏包提供的环境和命令实现算法排版。 ## 算法环境 algorithm2e宏包提供了3组环境用于实现算法排版: ### algorithm(algorithm*)环境 该环境是算法排版的主要环境,带星号环境用于在双栏排版中实现跨栏算法排版。 ### procedure(procedure*)环境 procedure环境用于算法中的过程排版,带星号环境用于在双栏排版中实现跨栏算法排版。 ### function(function*)环境 function环境用于算法中的函数排版,带星号环境用于在双栏排版中实现跨栏算法排版。 这些算法环境都是基于浮动体实现的,可以像使用`figure`和`table`浮动体环境一样设置浮动参数、添加标题、添加标签,也能够使用交叉引用。并且可以像使用类似`\\listoffigures`和`\\listoftables`命令一样,使用`\\listofalgorithms`命令排版算法目录。 ## 排版设置 使用algorithm2e宏包提供的功能,可以方便地对算法排版样式进行必要的个性化设置。 ### 宏包参数 在引入宏包时,可以通过为宏包指定必要的可选参数,以实现需求的排版样式,详情请参阅宏包手册第7节。 ### 设置命令 在算法排版中,可以使用宏包提供的命令在导言区进行全局样式设置,也可以在算法排版环境中随时使用命令对算法排版样式进行局部修改,详情请参阅宏包手册排版样例及命令详解。 ### 关键字自定义 在算法排版中,也可以通过宏包提供的命令设置个性化的关键字命令和语言,从而实现本地化排版,详情请参阅宏包手册第11节。 ## 排版样例 例如如下代码: ```tex \\documentclass{ctexart} \\usepackage{algorithm2e} \\usepackage{xfrac} % \\sfrac斜分式命令 % 框线颜色设置 \\usepackage{xcolor} \\makeatletter \\newcommand{\\setalgotoprulecolor}[1]{\\colorlet{toprulecolor}{#1}} \\let\\old@algocf@pre@ruled\\@algocf@pre@ruled % Adjust top rule colour \\renewcommand{\\@algocf@pre@ruled}{\\textcolor{toprulecolor}{\\old@algocf@pre@ruled}} \\newcommand{\\setalgobotrulecolor}[1]{\\colorlet{bottomrulecolor}{#1}} \\let\\old@algocf@post@ruled\\@algocf@post@ruled % Adjust middle rule colour \\renewcommand{\\@algocf@post@ruled}{\\textcolor{bottomrulecolor}{\\old@algocf@post@ruled}} \\newcommand{\\setalgomidrulecolor}[1]{\\colorlet{midrulecolor}{#1}} \\renewcommand{\\algocf@caption@ruled}{% \\box\\algocf@capbox{\\color{midrulecolor}\\kern\\interspacetitleruled\\hrule width\\algocf@ruledwidth height\\algotitleheightrule depth0pt\\kern\\interspacealgoruled}} \\makeatother \\setalgotoprulecolor{blue!90}% Default \\setalgobotrulecolor{red!90}% Default \\setalgomidrulecolor{green!90}% Default \\begin{document} 计算分数累加和的for循环实现如算法\\ref{algo:sumwithfor}所示。 \\begin{algorithm}[htp] \\SetAlgoLined % 绘制区域垂直线 \\LinesNumbered % 排版行号 \\KwIn{$x_i=\\{1,\\sfrac{1}{2},\\sfrac{1}{3},\\sfrac{1}{4},\\sfrac{1}{5},\\cdots,\\sfrac{1}{100}\\}$} \\KwOut{$sum=\\sum\\limits_{i=1}^{100} x_i$} \\BlankLine % 空白线 Initialize $sum=0$\\; \\For{$i\\leftarrow1$ \\KwTo $100$}{ $sum=sum+\\sfrac{1}{i}$\\; $i=i+1$\\; } Output $sum$\\; \\caption{sum of frac}\\label{algo:sumwithfor} % 标题 \\end{algorithm} 计算分数累加和的while循环实现如算法\\ref{algo:sumwithwhile}所示。 \\RestyleAlgo{ruled} % 设置排版样式 % 设置文字关键字 \\SetAlgorithmName{算法}{算法}{算法列表} \\SetKwInput{KwIn}{输入} \\SetKwInput{KwOut}{输出} \\begin{algorithm}[htp] \\KwIn{$x_i=\\{1,\\sfrac{1}{2},\\sfrac{1}{3},\\sfrac{1}{4},\\sfrac{1}{5},\\cdots,\\sfrac{1}{100}\\}$} \\KwOut{$sum=\\sum\\limits_{i=1}^{100} x_i$} \\AlgoDisplayBlockMarkers \\SetAlgoBlockMarkers{begin}{end}% \\SetAlgoNoEnd \\SetKwFunction{FSum}{getSum}% \\SetKwProg{Fn}{}{}{} \\Fn{}{ $sum = 0$\\; $i = 1$\\; \\While{i <= 100}{ $sum = sum + \\sfrac{1}{i}$\\; $i = i + 1$\\; } print $sum$\\; } \\caption{计算分数累加和}\\label{algo:sumwithwhile} \\end{algorithm} \\end{document} ``` 其排版结果为:
有关algorithm2e宏包的使用细节,请在命令行通过`texdoc algorithm2e`命令查阅其使用手册。 Happy LaTeXing!
发布评论
登录后方可评论!点击登录
全部评论 (0)
暂无评论, 快来抢沙发!