- algorithmic是第一代算法排版环境
- algorithmicx是第二代算法排版环境
- algorithm2e是第三版算法排版环境
以上三种算法排版环境中algorithmic比较老了,现在用的较多的是algorithmicx和algorithm2e排版环境。并且这三种排版环境不能混用。我个人比较喜欢algorithm2e的排版样式。具体参考这个链接
一般latex模板文件夹有一个cls的文件,里面会说明支持的Algorithm排版环境,用Notepad++打开搜索algorithm会找到类似于“\usepackage{algorithm, algorithmicx, algpseudocode}”的代码,定义了该latex模板支持的排版环境。我用的latex模板就不支持algorithm2e,只能使用algorithmicx。
- algorithmicx实例
\documentclass{article}
\title{Test title}
\author{ Mary\thanks{E-mail:*****@***.com}
\and Ted\thanks{Corresponding author}
\and Louis}
\date{\today}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}[h]
\caption{Conjugate Gradient Algorithm with Dynamic Step-Size Control}
\label{alg::conjugateGradient}
\begin{algorithmic}[1]
\Require
$f(x)$: objective funtion;
$x_0$: initial solution;
$s$: step size;
\Ensure
optimal $x^{*}$
\State initial $g_0=0$ and $d_0=0$;
\Repeat
\State compute gradient directions $g_k=\bigtriangledown f(x_k)$;
\State compute Polak-Ribiere parameter $\beta_k=\frac{g_k^{T}(g_k-g_{k-1})}{\parallel g_{k-1} \parallel^{2}}$;
\State compute the conjugate directions $d_k=-g_k+\beta_k d_{k-1}$;
\State compute the step size $\alpha_k=s/\parallel d_k \parallel_{2}$;
\Until{($f(x_k)>f(x_{k-1})$)}
\end{algorithmic}
\end{algorithm}
\end{document}
效果如下:
想要将Require和Ensure替换为Input和Output需要在document开始前添加如下两行代码:
\renewcommand{\algorithmicrequire}{\textbf{Input:}} % Use Input in the format of Algorithm
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm
- algorithmi2e实例
\documentclass{article}
\title{Test title}
\author{ Mary\thanks{E-mail:*****@***.com}
\and Ted\thanks{Corresponding author}
\and Louis}
\date{\today}
\usepackage[ruled]{algorithm2e} %ruled控制布局格式
\begin{document}
\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}
\end{document}
效果如下所示: