博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LaTeX 算法代码排版 --latex2e范例总结
阅读量:4286 次
发布时间:2019-05-27

本文共 4326 字,大约阅读时间需要 14 分钟。

 

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 - 1---------------------------------------------        \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 - 2------------------------------------------------    \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=1;j \le n;j \ne i$}         {             float $maxSim=0$\;             $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\SetKwInOut{KIN}{输入}\SetKwInOut{KOUT}{输出}%\BlankLine  %空一行    \caption{标准DE算法 }    \label{DE_algo} %    \KIN{Population: $ M$; Dimension: $ D $; Genetation: $ T $ }    \KOUT{The best vector (solution)  $ \varDelta $ }    $ t \leftarrow 1 (initialization) $\;    \For{$i=1$ to $ M $    }    {\For{$j=1$ to $ D $}        {            $  {x}_{i,t}^j=x_{min}^j + rand(0,1)\cdotp (x_{max}^j-x_{min}^j) $\;        }    }            %--------------------------------------------------    \While{$(|f(\varDelta)| \geq\varepsilon )$      or     $(t \leq T )$}    {        \For{$ i=1$  to $M$}        {\emph{$ \blacktriangleright $ (Mutation and Crossover)}\\            %\textit{ $ \blacktriangleright $ (Mutation and Crossover) }\\            \For{$j=1$ 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+1 $\;        }    %While            \Return the best vector  $\varDelta$\;\end{algorithm}

 

转载地址:http://vsxgi.baihongyu.com/

你可能感兴趣的文章
一台linux操作系统上配置多台Tomcat服务
查看>>
maven入门浅析(二)-----maven集成eclipse、jboss
查看>>
正则表达式入门教程(三)
查看>>
正则表达式入门教程(四)
查看>>
JAVA程序员成长之路的总结
查看>>
javaEE工程师学习路线图
查看>>
java工程师进阶之路
查看>>
linux系统一个tomcat配置两个域名,每个域名对应一个项目
查看>>
javaScript使用Lodop实现网页表格套打功能
查看>>
技术大牛如何寻找下一个风口
查看>>
大数据学习路线大纲
查看>>
使用http请求,中文乱码问题--解决方法
查看>>
mybatis入门之接口式编程
查看>>
html页面中iframe嵌套页面的父页面和子页面js方法互相调用
查看>>
求字符串中变换位置问题
查看>>
求这个字符串中的最大的数字字符串
查看>>
json.loads
查看>>
快速截屏小工具
查看>>
selenium-webdriver(python) (十)
查看>>
swichomege安装
查看>>