LaTeX 寫做: 算法代碼排版 --latex2e範例總結算法
latex2e 宏包的使用範例:ide
使用上述代碼中三種不一樣的宏包編譯後的代碼排版樣式預覽:this
樣式一:spa
樣式二:3d
樣式三:rest
latex代碼排版.tex文件: code
\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爲輸出的方法: 在算法內部插入、orm
\SetKwInOut{KIN}{輸入}blog
\SetKwInOut{KOUT}{輸出}ci
用咱們定義的新的宏名「\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}