LATEX编写算法的代码

LaTeX算法排版例子
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
  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}

排版结果如下:

你可能感兴趣的:(latex)