Latex中如何编辑算法
Posted DM--Tutor
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Latex中如何编辑算法相关的知识,希望对你有一定的参考价值。
LaTeX算法排版例子1)首先在导言区加入语句:
\\usepackagealgorithm
\\usepackagealgorithmic
2)例1
\\beginalgorithm
\\captionA
\\labelalg:A
\\beginalgorithmic
\\STATE set $r(t)=x(t)$
\\REPEAT
\\STATE set $h(t)=r(t)$
\\REPEAT
\\STATE set $h(t)=r(t)$
\\UNTILB
\\UNTILB
\\endalgorithmic
\\endalgorithm
排版结果如下:
3)例2
\\beginalgorithm
\\captionCalculate $y = x^n$
\\labelalg1
\\beginalgorithmic
\\REQUIRE $n \\geq 0 \\vee x \\neq 0$
\\ENSURE $y = x^n$
\\STATE $y \\Leftarrow 1$
\\IF$n < 0$
\\STATE $X \\Leftarrow 1 / x$
\\STATE $N \\Leftarrow -n$
\\ELSE
\\STATE $X \\Leftarrow x$
\\STATE $N \\Leftarrow n$
\\ENDIF
\\WHILE$N \\neq 0$
\\IF$N$ is even
\\STATE $X \\Leftarrow X \\times X$
\\STATE $N \\Leftarrow N / 2$
\\ELSE[$N$ is odd]
\\STATE $y \\Leftarrow y \\times X$
\\STATE $N \\Leftarrow N - 1$
\\ENDIF
\\ENDWHILE
\\endalgorithmic
\\endalgorithm
排版结果如下:
4)\\renewcommand 改变现有命令的定义。在导言区加入如下语句:
\\renewcommand\\algorithmicrequire \\textbfInput: %Use Input in the format of Algorithm
\\renewcommand\\algorithmicensure \\textbfOutput: %UseOutput in the format of Algorithm
使得原来软件包中定义的命令\\REQUIRE和\\ENSURE显示为Input:和Output:
一个例子如下:
\\beginalgorithm[htb]
\\caption Framework of ensemble learning for our system.
\\labelalg:Framwork
\\beginalgorithmic[1] %这个1 表示每一行都显示数字
\\REQUIRE ~~\\\\ %算法的输入参数:Input
The set of positive samples for current batch, $P_n$;\\\\
The set of unlabelled samples for current batch, $U_n$;\\\\
Ensemble of classifiers on former batches, $E_n-1$;
\\ENSURE ~~\\\\ %算法的输出:Output
Ensemble of classifiers on the current batch, $E_n$;
\\STATE Extracting the set of reliable negative and/or positive samples $T_n$ from $U_n$ with help of $P_n$;
\\label code:fram:extract %对此行的标记,方便在文中引用算法的某个步骤
\\STATE Training ensemble of classifiers $E$ on $T_n \\cup P_n$, with help of data in former batches;
\\labelcode:fram:trainbase
\\STATE $E_n=E_n-1\\cup E$;
\\labelcode:fram:add
\\STATE Classifying samples in $U_n-T_n$ by $E_n$;
\\labelcode:fram:classify
\\STATE Deleting some weak classifiers in $E_n$ so as to keep the capacity of $E_n$;
\\labelcode:fram:select
\\RETURN $E_n$; %算法的返回值
\\endalgorithmic
\\endalgorithm
排版结果如下:
5)最后一个例子
\\beginalgorithm[h]
\\captionAn example for format For \\& While Loop in Algorithm
\\beginalgorithmic[1]
\\FOReach $i \\in [1,9]$
\\STATE initialize a tree $T_i$ with only a leaf (the root);\\
\\STATE $T=T \\cup T_i;$\\
\\ENDFOR
\\FORALL $c$ such that $c \\in RecentMBatch(E_n-1)$
\\labelcode:TrainBase:getc
\\STATE $T=T \\cup PosSample(c)$;
\\labelcode:TrainBase:pos
\\ENDFOR
\\FOR$i=1$; $i<n$; $i++$
\\STATE $//$ Your source here;
\\ENDFOR
\\FOR$i=1$ to $n$
\\STATE $//$ Your source here;
\\ENDFOR
\\STATE $//$ Reusing recent base classifiers.
\\labelcode:recentStart
\\WHILE $(|E_n| \\leq L_1 )and( D \\neq \\phi)$
\\STATE Selecting the most recent classifier $c_i$ from $D$;
\\STATE $D=D-c_i$;
\\STATE $E_n=E_n+c_i$;
\\ENDWHILE
\\labelcode:recentEnd
\\endalgorithmic
\\endalgorithm
排版结果如下:
以上是关于Latex中如何编辑算法的主要内容,如果未能解决你的问题,请参考以下文章