Algorithm2e本身不支援Do-While結構(支援的是While-Do),需要自行定義。具體的定義方法請見最後一個例子
1。 準備
該工具包的使用手冊下載地址:
http://
mlg。ulb。ac。be/files/alg
orithm2e。pdf
algorithm2e。pdf
\usepackage[ruled,linesnumbered]{algorithm2e}
使用宏包時中括號的引數含義:
ruled
是讓標題顯示在上面,否則演算法的標題則在下面。
linesnumbered
讓演算法中顯示行號。
還可以新增
boxed
, 讓演算法排版時插入在一個盒子裡。
2。 基本語法
書寫程式碼時也有一些專門的命令:
強烈建議知乎支援Markdown表格
EX:
如果你不想讓你的虛擬碼叫做 ‘Algorithm 編號’, 可以使用
\renewcommand{\algorithmcfname}{演算法名}
命令來修改。
除了\If, \Else, \ElseIf之外,還有\uIf, \lIf, \uElse, \lElse, \uElseIf, \lElseIf等命令,他們的區別在於
\If, \Else, \ElseIf都是會以end結尾
\uIf, \uElse, \uElseIf, 是不以end結尾的塊級元素
\lIf, \lElse, \lElseIf 是不以end為結尾的行內元素
在If-else結構中,\eIf 自帶else(即 if 和 else 共用一個 end),而只是用 \If 和 \Else 的話則會多出一個end給Else。
3。 例子
這些例子可以直接放在一個空的。tex檔案中進行編譯。
例子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
現在,我們在輸入輸出中間加一點點間隔,然後給演算法的某些行進行強調,再給if條件加上註釋呢
\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
實際上,咱們用的最多的格式(論文裡常見)就還是這種:
\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}
例子4
Algorithm2e本身不支援Do-While結構(支援的是While-Do),需要自行定義。不過自行定義並不難,因為宏包中內建了Repeat-Until結構,在Algorithm2e中是“宏指令(Repeat macros)”的一種
[1]
自定義宏指令
\SetKwRepeat{Do}{do}{while}
定義完之後,就可以在虛擬碼塊中使用如下命令呼叫
\Do{<結束條件>}{<執行命令>}
完整例程:
\documentclass{article}
\usepackage[linesnumbered, ruled]{algorithm2e}
\SetKwRepeat{Do}{do}{while}%
\begin{document}
\begin{algorithm}[H]
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\Repeat{this end condition}{
do these things\;
}
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
\Do{this end condition}{
do these things\;
}
}
\caption{How to write algorithms}
\end{algorithm}
\end{document}
寫在最後
LaTeX,說難也難,說不難也不難。
說它難,是因為其本身相當於一種較為獨立的程式語言。熟練的掌握它,需要將以往我們在Word中“所見即所得”的寫作思維徹底轉變為“所想即所得”的思維。
但是,它也很簡單。實際上,我從下載第一個CTeX包,到本科第一篇論文寫出來,只花了三天。LaTeX雖然涉及很多複雜與繁瑣的設定,但是如果只是想粗略地入個門,只需要一個手把手帶你配環境的小白向教程以及一個可以作為工具書查閱的工具書即可。(當然,系統學習LaTeX將會讓你之後寫論文的速度和版面美觀度突飛猛進)
針對那些TeX小白使用者,或者不太熟練的使用者,我首先推薦我同門師兄小魚學長寫的教程~
最後,LaTeX作為理工科科研人員的必備工具,熟練掌握可以極大地提高論文寫作的效率。所以不論你是為了給日後寶貴的實驗節省時間,還是為了更加優美地排版出想要的書籍,
擁有一本詳盡且好懂的LaTeX教程,總比遇到問題了和無頭蒼蠅亂撞一樣翻知乎和部落格來得高效
。
參考文獻
https://
zhuanlan。zhihu。com/p/10
5582648
https://
blog。csdn。net/robert_ch
en1988/article/details/71512914
https://
blog。csdn。net/yq_foreve
r/article/details/89815562
本文轉載自:
薰風初入弦
,如有侵權請聯絡刪除。
參考
^
https://tex。stackexchange。com/questions/212301/do-while-loop-in-algorithm2e