LaTeX 写作: 算法代码排版 --latex2e范例总结

本来以为这个人写的挺好,但是发现似乎有些问题,而且比较繁琐。这个人也是。
https://www.cnblogs.com/empty16/p/6222885.html
https://www.cnblogs.com/coolqiyu/p/5580290.html
后来发现这个人写还算不错,至少能够用起来,没有报错,但里面有些用法感觉查了资料才会理解。
https://www.cnblogs.com/tsingke/p/5833221.html
https://www.pianshen.com/article/4718305185/

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 即可,实现中文的输入和输出.

具体事例如下:

1 \renewcommand{\algorithmcfname}{算法}
 2 \begin{algorithm}[H]
 3 %\SetAlgoNoLine
 4 \SetKwInOut{KIN}{输入}
 5 \SetKwInOut{KOUT}{输出}
 6 %\BlankLine  %空一行
 7     \caption{标准DE算法 }
 8     \label{DE_algo} %
 9     \KIN{Population: $ M$; Dimension: $ D $; Genetation: $ T $ }
10     \KOUT{The best vector (solution)  $ \varDelta $ }
11     $ t \leftarrow 1 (initialization) $\;
12     \For{$i=1$ to $ M $    }
13     {\For{$j=1$ to $ D $}
14         {
15             $  {x}_{i,t}^j=x_{min}^j + rand(0,1)\cdotp (x_{max}^j-x_{min}^j) $\;
16         }
17     }        
18     %--------------------------------------------------    
19 \While{$(|f(\varDelta)| \geq\varepsilon )$      or     $(t \leq T )$}
20     {
21         \For{$ i=1$  to $M$}
22         {
23 \emph{$ \blacktriangleright $ (Mutation and Crossover)}\\            
24 %\textit{ $ \blacktriangleright $ (Mutation and Crossover) }\\
25             \For{$j=1$ to $ D $}
26             {    
27                 $ v_{i,t}^j =Mutation(x_{i,t}^j)$\;    
28                 $ u_{i,t}^j =Crossover(x_{i,t}^j,v_{i,t}^j)$\;
29             }
30 \emph{$ \blacktriangleright $ (Greedy Selection)}\\
31             %\textit{ $ \blacktriangleright $ (Greedy Selection) }\\
32             \eIf{$ f(\textbf{u}_{i,t}) <  f(\textbf{x}_{i,t}) $}
33             {
34                 $  \textbf{x}_{i,t} \leftarrow\textbf{u}_{i,t}$\;    
35                 \If{$  f(\textbf{x}_{i,t}) < f(\varDelta)$}
36                 {
37                     $ \varDelta \leftarrow \textbf{x}_{i,t}$ \;
38                 }
39             }
40             {
41                 $  \textbf{x}_{i,t} \leftarrow \textbf{x}_{i,t} $\;
42             }
43         }
44         $ t \leftarrow t+1 $\;    
45     }    %While        
46     \Return the best vector  $\varDelta$\;
47 \end{algorithm}

附加工具:

显示直立文本: \textup{文本}
意大利斜体: \textit{文本}
slanted斜体: \textsl{文本}
显示小体大写文本:  \textsc{文本}
中等权重: \textmd{文本}
加粗命令: \textbf{文本}
默认值: \textnormal{文本}

斜体字:\textit{italic},或者 \emph{italic}

细体字:\textlf{light font}

使用等宽字体:\texttt{code}

使用无衬线字体:\textsf{sans-serif}

所有字母大写:\uppercase{CAPITALS}

所有字母大写,但小写字母比较小:\textsc{Small Capitals}

参考:https://www.cnblogs.com/xueqiuqiu/articles/9086713.html
https://blog.csdn.net/u014630987/article/details/70156489

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值