準備工作:
1、先安裝 CTEX
https://
mirrors。tuna。tsinghua。edu。cn
/ctex/legacy/2。9/
2、下載 algorithm2e 包(裡面有詳細的使用說明文件)
https://www。
ctan。org/tex-archive/ma
cros/latex/contrib/algorithm2e
這個其實 CTEX裡已經包含了,不過不是最新的。想要最新版和一些官方提供的例子可以在 CTAN 下載 algorithm2e 包 (也就是上面的連結),下載完之後要解壓到 “C:\CTEX\MiKTeX\
doc
\latex\algorithm2e” 和 “C:\CTEX\MiKTeX\
tex
\latex\algorithm2e”,然後要 執行 “
開始選單 -> Ctex -> Settings(Admin) -> Refresh FNDB
”
3、安裝 Inkspace (將生成的 演算法步驟圖/虛擬碼。pdf 轉成能匯入word的格式)
https://
inkscape。org/release/in
kscape-0。92。4/
好吧,開始操作~~~
裝好 CTEX 後,開啟 “
開始選單 -> Ctex ->
WinEdt
”,新建一個 file
總的來說建立一個 latex 虛擬碼檔案 需遵循格式:
\def\SetClass{article}
\documentclass{\SetClass}
\usepackage[格式]{algorithm2e}
\begin{document}
演算法內容
\end{document}
我們直接貼幾個例子:
注意編譯的時候 選 "PDFTeX",然後點 Accessories 裡的 Compile
例子1:
這個例子來源於官方說明文件
\def\SetClass{article}
\documentclass{\SetClass}
\usepackage[lined,boxed,commentsnumbered]{algorithm2e}
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}
\end{document}
例子2:
這個例子也是官方說明文件裡的
\def\SetClass{article}
\documentclass{\SetClass}
\usepackage[linesnumbered,lined,boxed,commentsnumbered]{algorithm2e}
\begin{document}
\IncMargin{1em}
\begin{algorithm}
\SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
\SetKwFunction{Union}{Union}\SetKwFunction{FindCompress}{FindCompress}
\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
\Input{A bitmap $Im$ of size $w\times l$}
\Output{A partition of the bitmap}
\BlankLine
\emph{special treatment of the first line}\;
\For{$i\leftarrow 2$ \KwTo $l$}{
\emph{special treatment of the first element of line $i$}\;
\For{$j\leftarrow 2$ \KwTo $w$}{\label{forins}
\Left$\leftarrow$ \FindCompress{$Im[i,j-1]$}\;
\Up$\leftarrow$ \FindCompress{$Im[i-1,]$}\;
\This$\leftarrow$ \FindCompress{$Im[i,j]$}\;
\If(\tcp*[h]{O(\Left,\This)==1}){\Left compatible with \This}{\label{lt}
\lIf{\Left $<$ \This}{\Union{\Left,\This}}
\lElse{\Union{\This,\Left}}
}
\If(\tcp*[f]{O(\Up,\This)==1}){\Up compatible with \This}{\label{ut}
\lIf{\Up $<$ \This}{\Union{\Up,\This}}
\tcp{\This is put under \Up to keep tree as flat as possible}\label{cmt}
\lElse{\Union{\This,\Up}}\tcp*[h]{\This linked to \Up}\label{lelse}
}
}
\lForEach{element $e$ of the line $i$}{\FindCompress{p}}
}
\caption{disjoint decomposition}\label{algo_disjdecomp}
\end{algorithm}\DecMargin{1em}
\end{document}
例子3:
這個例子來源於部落格,而且博主對其中的一些用法給出了一些說明,詳見
https://
blog。csdn。net/robert_ch
en1988/article/details/71512914
\def\SetClass{article}
\documentclass{\SetClass}
\usepackage[ruled,linesnumbered]{algorithm2e}
\begin{document}
\begin{algorithm}
\caption{Simulation-optimization heuristic}\label{algorithm}
\KwData{current period $t$, initial inventory $I_{t-1}$, initial capital $B_{t-1}$, demand samples}
\KwResult{Optimal order quantity $Q^{\ast}_{t}$}
$r\leftarrow t$\;
$\Delta B^{\ast}\leftarrow -\infty$\;
\While{$\Delta B\leq \Delta B^{\ast}$ and $r\leq T$}{$Q\leftarrow\arg\max_{Q\geq 0}\Delta B^{Q}_{t,r}(I_{t-1},B_{t-1})$\;
$\Delta B\leftarrow \Delta B^{Q}_{t,r}(I_{t-1},B_{t-1})/(r-t+1)$\;
\If{$\Delta B\geq \Delta B^{\ast}$}{$Q^{\ast}\leftarrow Q$\;
$\Delta B^{\ast}\leftarrow \Delta B$\;}
$r\leftarrow r+1$\;}
\end{algorithm}
\end{document}
轉格式
好了,最後一步就是將生成的 PDF 轉成我們需要的格式了,開啟 Inkspace 轉就完事了,這裡不詳細說了~