LaTeX算法排版例子

LaTeX算法排版例子

转自http://blog.csdn.net/lqhbupt/article/details/8723478

1)首先在导言区加入语句:

  1. \usepackage{algorithm}  
  2. \usepackage{algorithmic}  


2)例1

  1. \begin{algorithm}  
  2. \caption{A}  
  3. \label{alg:A}  
  4. \begin{algorithmic}  
  5. \STATE {set $r(t)=x(t)$}   
  6. \REPEAT   
  7. \STATE set $h(t)=r(t)$   
  8. \REPEAT  
  9. \STATE set $h(t)=r(t)$   
  10. \UNTIL{B}   
  11. \UNTIL{B}  
  12. \end{algorithmic}  
  13. \end{algorithm}  

排版结果如下:

3)例2

  1. \begin{algorithm}  
  2. \caption{Calculate $y = x^n$}   
  3. \label{alg1}  
  4. \begin{algorithmic}  
  5. \REQUIRE $n \geq 0 \vee x \neq 0$   
  6. \ENSURE $y = x^n$   
  7. \STATE $y \Leftarrow 1$   
  8. \IF{$n < 0$}   
  9. \STATE $X \Leftarrow 1 / x$   
  10. \STATE $N \Leftarrow -n$   
  11. \ELSE   
  12. \STATE $X \Leftarrow x$   
  13. \STATE $N \Leftarrow n$  
  14. \ENDIF   
  15. \WHILE{$N \neq 0$}   
  16. \IF{$N$ is even}   
  17. \STATE $X \Leftarrow X \times X$   
  18. \STATE $N \Leftarrow N / 2$   
  19. \ELSE[$N$ is odd]   
  20. \STATE $y \Leftarrow y \times X$   
  21. \STATE $N \Leftarrow N - 1$   
  22. \ENDIF   
  23. \ENDWHILE  
  24. \end{algorithmic}  
  25. \end{algorithm}  

排版结果如下:

4)\renewcommand 改变现有命令的定义。在导言区加入如下语句:

  1. \renewcommand{\algorithmicrequire}{ \textbf{Input:}} %Use Input in the format of Algorithm  
  2. \renewcommand{\algorithmicensure}{ \textbf{Output:}} %UseOutput in the format of Algorithm  

使得原来软件包中定义的命令\REQUIRE和\ENSURE显示为Input:和Output:

一个例子如下: 

  1. \begin{algorithm}[htb]   
  2. \caption{ Framework of ensemble learning for our system.}   
  3. \label{alg:Framwork}   
  4. \begin{algorithmic}[1] %这个1 表示每一行都显示数字  
  5. \REQUIRE ~~\\ %算法的输入参数:Input  
  6. The set of positive samples for current batch, $P_n$;\\  
  7. The set of unlabelled samples for current batch, $U_n$;\\  
  8. Ensemble of classifiers on former batches, $E_{n-1}$;  
  9. \ENSURE ~~\\ %算法的输出:Output  
  10. Ensemble of classifiers on the current batch, $E_n$;  
  11. \STATE Extracting the set of reliable negative and/or positive samples $T_n$ from $U_n$ with help of $P_n$;   
  12. \label{ code:fram:extract }%对此行的标记,方便在文中引用算法的某个步骤  
  13. \STATE Training ensemble of classifiers $E$ on $T_n \cup P_n$, with help of data in former batches;   
  14. \label{code:fram:trainbase}  
  15. \STATE $E_n=E_{n-1}\cup E$;   
  16. \label{code:fram:add}  
  17. \STATE Classifying samples in $U_n-T_n$ by $E_n$;   
  18. \label{code:fram:classify}  
  19. \STATE Deleting some weak classifiers in $E_n$ so as to keep the capacity of $E_n$;   
  20. \label{code:fram:select}  
  21. \RETURN $E_n$; %算法的返回值  
  22. \end{algorithmic}  
  23. \end{algorithm}  

排版结果如下:

 5)最后一个例子

  1. \begin{algorithm}[h]  
  2. \caption{An example for format For \& While Loop in Algorithm}  
  3. \begin{algorithmic}[1]  
  4. \FOR{each $i \in [1,9]$}  
  5. \STATE initialize a tree $T_{i}$ with only a leaf (the root);\  
  6. \STATE $T=T \cup T_{i};$\  
  7. \ENDFOR  
  8. \FORALL {$c$ such that $c \in RecentMBatch(E_{n-1})$}   
  9. \label{code:TrainBase:getc}  
  10. \STATE $T=T \cup PosSample(c)$;   
  11. \label{code:TrainBase:pos}  
  12. \ENDFOR  
  13. \FOR{$i=1$; $i<n$; $i++$ }  
  14. \STATE $//$ Your source here;  
  15. \ENDFOR  
  16. \FOR{$i=1$ to $n$}  
  17. \STATE $//$ Your source here;  
  18. \ENDFOR  
  19. \STATE $//$ Reusing recent base classifiers.   
  20. \label{code:recentStart}  
  21. \WHILE {$(|E_n| \leq L_1 )and( D \neq \phi)$}  
  22. \STATE Selecting the most recent classifier $c_i$ from $D$;  
  23. \STATE $D=D-c_i$;  
  24. \STATE $E_n=E_n+c_i$;  
  25. \ENDWHILE   
  26. \label{code:recentEnd}  
  27. \end{algorithmic}  
  28. \end{algorithm}  

排版结果如下:


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值