Latex 中进行算法的排版

本文详细介绍了如何在LaTeX中使用algorithm和algorithmic这两个宏包进行算法描述的排版,包括设置格式、控制缩进、使用Input和Output以及自定义命令。通过实例演示了各种排版选项和技巧。

在论文写作和排版过程中,常常会用到算法描述,在LaTex中,算法描述块的排版会用到两个宏包 \usepackage{algorithm}  和  \usepackage{algorithmic}。算法的排版,主要在于控制缩进、粗体、横线等格式,这些都会在这篇博客中进行介绍。

在开始算法排版之前,首先在文档开头加入下面两句,以导入宏包:

\usepackage{algorithm}  
\usepackage{algorithmic}  

例1

那么,我们首先看一个例子:

\begin{algorithm}  
\caption{A}  
\label{alg:A}  
\begin{algorithmic}  
\STATE {set $r(t)=x(t)$}   
\REPEAT   
\STATE set $h(t)=r(t)$   
\REPEAT  
\STATE set $h(t)=r(t)$   
\UNTIL{B}   
\UNTIL{B}  
\end{algorithmic}  
\end{algorithm}  

在编译之后,显示为:

使用algorithmic包时,关键字全部大写,如果使用的是algorithmicx包,那么关键字首字母大写,后面小写。

例2

第二个例子更加详细的展示了缩进的控制,可以自己编译一下:

\begin{algorithm}  
\caption{Calculate $y = x^n$}   
\label{alg1}  
\begin{algorithmic}  
\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  
\end{algorithmic}  
\end{algorithm}

例3

如果需要显示Input和Output:

\begin{algorithm}  
\caption{Fourier-Mellin Based KCF}  
\label{alg:A}  
\hspace*{0.02in}{\bf Input:}
Image $I$\\preprocessed kernelized template $T_\kappa$\\
\hspace*{0.02in}{\bf Output:} 
 scale $\sigma$, angle $\theta$ relation between $I$ and $T$ 

\begin{algorithmic}[1] 
\STATE {fourier transform: $F=\mathcal{F}(I)$}
\STATE {high pass filter: $F_h=\mathcal{H}(F)$\\$\mathcal{H}(x,y)=(1.0-cos(\pi x)cos(\pi y))(2.0-cos(\pi x)cos(\pi y))$}
\STATE {log-polar transform: $F_{lp}=\mathcal{L}(F_h)$}
\STATE {apply kernel function: $F_\kappa=\mathcal{K}(F_{lp})$}
\STATE {phase correlation: $(\Delta x, \Delta y)=\mathcal{C}(F_\kappa, T_\kappa)$}
\STATE {resolove scale and rotation:\\
$\theta=\alpha \Delta x$, $\sigma=log(\Delta y)$\\
where $\alpha$ is translation factor of pixel translation on fourier domain and polar angle on origin image
}
\end{algorithmic}  
\end{algorithm}

这样,就在开头显示了输入和输出。{algorithmic}[1]表示显示行号,当然,还可以显示竖线,不过要使用额外的宏包,请参考文后链接。

例4

还可以使用\renewcommand 改变现有命令,在导言区加入下列语句

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

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

\begin{algorithm}[htb]   
\caption{ Framework of ensemble learning for our system.}   
\label{alg:Framwork}   
\begin{algorithmic}[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;   
\label{code:fram:trainbase}  
\STATE $E_n=E_{n-1}\cup E$;   
\label{code:fram:add}  
\STATE Classifying samples in $U_n-T_n$ by $E_n$;   
\label{code:fram:classify}  
\STATE Deleting some weak classifiers in $E_n$ so as to keep the capacity of $E_n$;   
\label{code:fram:select}  
\RETURN $E_n$; %算法的返回值  
\end{algorithmic}  
\end{algorithm}  

排版结果如下:

例5

最后一个例子:

\begin{algorithm}[h]  
\caption{An example for format For \& While Loop in Algorithm}  
\begin{algorithmic}[1]  
\FOR{each $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})$}   
\label{code:TrainBase:getc}  
\STATE $T=T \cup PosSample(c)$;   
\label{code: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.   
\label{code: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   
\label{code:recentEnd}  
\end{algorithmic}  
\end{algorithm}  

排版结果为:

 内容参考博主

使用LaTeX进行算法排版有多种方法,以下为你详细介绍: ### 利用`algorithm`与`algorithmic`包 可以使用`algorithm`和`algorithmic`包进行算法排版,这种方式能很好地控制缩进。以下是一个计算 $y = x^n$ 的算法示例代码: ```latex \begin{algorithm} \caption{Calculate $y = x^n$} \label{alg1} \begin{algorithmic} \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 \end{algorithmic} \end{algorithm} ``` 该示例展示了如何使用`\REQUIRE`定义输入条件,`\ENSURE`定义输出条件,以及`\IF`、`\ELSE`、`\ENDIF`、`\WHILE`、`\ENDWHILE`等进行条件和循环的排版 [^2]。 ### 修改现有命令定义 可以通过`\renewcommand`改变现有命令的定义,在导言区加入如下语句能修改算法输入输出的显示格式: ```latex \renewcommand{\algorithmicrequire}{ \textbf{Input:}} %Use Input in the format of Algorithm \renewcommand{\algorithmicensure}{ \textbf{Output:}} %UseOutput in the format of Algorithm ``` 这样可以将输入输出的显示分别改为`Input:`和`Output:` [^3]。 ### 其他示例 还有一些其他的算法排版示例,如下面的`Myalgorithm`: ```latex \makeatletter\def\BState{\State\hskip-\ALG@thistlm}\makeatother \begin{algorithm} \caption{Myalgorithm} \label{euclid} \begin{algorithmic}[1] \Procedure{MyProcedure}{} \State stringlen ← lengthof string \textit{stringlen}\gets\text{lengthof}\textit{string} stringlen←lengthofstring \State i ← patlen i\gets\textit{patlen} i←patlen \BState\emph{top}: \If{ i > stringlen i>\textit{stringlen} i>stringlen} \Return false \EndIf \State j ← patlen j\gets\textit{patlen} j←patlen \BState\emph{loop}: \If{ string ( i ) = path ( j ) \textit{string}(i)=\textit{path}(j) string(i)=path(j)} \State j ← j − 1 j\getsj-1 j←j−1. \State i ← i − 1 i\getsi-1 i←i−1. \State\textbf{goto}\emph{loop}. \State\textbf{close}; \EndIf \State i ← i + \max(\textit{delta}\_1(\textit{string}(i)),\textit{delta}\_2(j)) i\getsi+\max(\textit{delta}\_1(\textit{string}(i)),\textit{delta}\_2(j)) i←i+max(delta_1(string(i)),delta_2(j)). \State\textbf{goto}\emph{top}. \EndProcedure \end{algorithmic} \end{algorithm} ``` 此示例展示了如何定义过程、使用状态语句以及条件判断等 [^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值