[Latex] 如何在Latex上寫algorithm
[转]
2011年1月21日星期五
做個紀錄吧,我下關鍵字都找不到清晰的教學XD
希望以後有人要用到的時候比較好查!
首先你需要灌一些package,灌package的方式,我以MiKTeX為例子。
簡單的來說,開始 -> MiKTeX -> Maintenance (Admin) -> Package Manager (Admin)
點進去之後,他會搜尋所有的Package。
然後你點選你需要的Package 再按那個"+"的按鈕就會更新package了!
通常Algorithm 會用到哪些package呢?
你可以灌以下這些Package:
algorithms, algorithm2e, program, alg, algorithmicx, pseudocode
安裝完成之後,回到你的tex檔。
在\begin{document}之前,加入以下這兩行:
\usepackage{algorithmic} - 對應algorithmicx
前兩行renewcommand 是:
將require以Input: 形式表示及ensure以Output:形式表示。
\makeatletter
\newif\if@restonecol
\makeatother
\let\algorithm\relax
\let\endalgorithm\relax
希望以後有人要用到的時候比較好查!
首先你需要灌一些package,灌package的方式,我以MiKTeX為例子。
簡單的來說,開始 -> MiKTeX -> Maintenance (Admin) -> Package Manager (Admin)
點進去之後,他會搜尋所有的Package。
然後你點選你需要的Package 再按那個"+"的按鈕就會更新package了!
通常Algorithm 會用到哪些package呢?
你可以灌以下這些Package:
algorithms, algorithm2e, program, alg, algorithmicx, pseudocode
安裝完成之後,回到你的tex檔。
在\begin{document}之前,加入以下這兩行:
\usepackage{algorithmic} - 對應algorithmicx
\usepackage{algorithm} - 對應algorithms
因為我目前只用到這兩個,相對應其他package的usepackage 稍微搜尋一下就會有了!
有時候加完之後complie 會出現algorithm.sty找不到的訊息。
這時候就手動將algorithm.sty放入你目前的資料夾中就可以了!
至於去哪找到algorithm.sty呢?
資料夾 "MiKTeX 2.8\tex\latex\algorithms" 底下就找得到。
另外附上簡單的Sample
\algsetup{indent=2em}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\newcommand{\factorial}{\ensuremath{\mbox{\sc Factorial}}}
\begin{algorithm}[h!]
\caption{$\factorial(n)$}\label{alg:factorial}
\begin{algorithmic}[1]
\REQUIRE An integer $n \geq 0$.
\ENSURE The value of $n!$.
\medskip
\IF {$n = 0$}
\RETURN $1$
\ELSE
\RETURN $n \cdot \factorial(n-1)$
\ENDIF
\end{algorithmic}
\end{algorithm
前兩行renewcommand 是:
將require以Input: 形式表示及ensure以Output:形式表示。
網路上範例一:
\begin{algorithm}[htb]
\caption{ Framework of ensemble learning for our system.}
\label{alg:Framwork}
\begin{algorithmic}[1]
\Require
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
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}
\begin{algorithm}[htb]
\caption{ Framework of ensemble learning for our system.}
\label{alg:Framwork}
\begin{algorithmic}[1]
\Require
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
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}
排版效果圖:
網路上範例二:
\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}
\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}
排版效果圖:
個人範例:
\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}
\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}
排版效果圖:
先前所使用的套件為algorithm或algorithmic
接下來介紹另一個寫algorithm的套件alogrithm2e
首先使用\usepackage指令
\usepackage[linesnumbered,boxed]{algorithm2e}
接下來是網路範例:
\begin{algorithm}
\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}
排版效果圖:
\usepackage[linesnumbered,boxed]{algorithm2e}
接下來是網路範例:
\begin{algorithm}
\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}
排版效果圖:
延伸幾個問題:
一、如何修改Algorithm的標題為中文的"演算法”?
在\begin{document}之前加入\renewcommand{\algorithmcfname}{算法} 即可(註:需先安裝中文字形)
二、如何去掉演算法中的豎線?
加入 \SetAlgoNoLine 指令在\begin{algorithm}之後
排版效果圖:
三、還可以使用其他標題樣式?
也可以使用\usepackage[ruled,vlined]{algorithm2e}
排版效果圖:
排版效果圖:
關於algorithm2e還有以下一些information
The algorithm2e LaTeX package conflicts with several others over the use of the algorithm identifier.
The algorithm2e LaTeX package conflicts with several others over the use of the algorithm identifier.
A common indicator is something like this message: Too many }'s.l.1616 }
To resolve the issues, simply put the following just before the inclusion of the algorithm2e package:
\makeatletter
\newif\if@restonecol
\makeatother
\let\algorithm\relax
\let\endalgorithm\relax
No comments:
Post a Comment