經常使用的排版僞代碼包有clrscode, algorithm, algorithmic, algorithmicx, algorithm2ehtml
clrscode是著名的算法教材 Introduction to Algorithms, 2nd ed. 的做者排版該書時本身制定的。【c l r s 分別是該書四個做者的姓的第一個字母,所以人們常以 clrs 指稱該書。】
示例:
該包支持顯示行號,加入註釋,可以每行 label 及引用。
如下是 Insertion-Sort 算法的 codejava
\begin{codebox} \Procname{$\proc{Insertion-Sort(A)}$} \li \For $j \gets 2$ \To $\id{length}[A]$ \label{li:for} \li \Do $\id{key} \gets A[j]$ \label{li:for-begin} \li \Comment Insert $A[j]$ into the sorted sequence $A[1 \twodots j-1]$. \li $i \gets j-1$ \li \While $i>0$ and $A[i]>\id{key}$ \label{li:while} \li \Do $A[i+1] \gets A[i]$ \label{li:while-begin} \li $i \gets i-1$ \label{li:while-end} \End \li $A[i+1] \gets \id{key}$ \label{li:for-end} \End \end{codebox}
\usepackage{caption} \usepackage{algorithm} \usepackage{algpseudocode}
\begin{algorithmic}[1]中的[n]表編號間隔,爲1的話表示每行都要有編號 caption{內容}和caption*{內容}前者內容前面會加上Algorithm+編號,然後者不會 \State至關於\item
示例:算法
\documentclass{article} \usepackage{caption} \usepackage{algorithm} \usepackage{algpseudocode} %\usepackage{fontspec,xunicode,xltxtra} %\setmainfont[BoldFont=Microsoft YaHei]{FangSong_GB2312} %\setsansfont[BoldFont=Microsoft YaHei]{KaiTi_GB2312} %\setmonofont{Bitstream Vera Sans Mono} %\XeTeXlinebreaklocale "zh" % %\XeTeXlinebreakskip = 0pt plus 1pt minus 0.1pt %\newcommand\li{\fontspec{LiSu}} %\newcommand\hei{\fontspec{SimHei}} %\newcommand\you{\fontspec{YouYuan}} %\newcommand\sun{\fontspec{SimSun}} %\newcommand\fangsong{\fontspec{仿宋_GB2312}} %\newcommand\kai{\fontspec{KaiTi_GB2312}} %%\numberwithin %%\numberwithin{equation}{section} \begin{document} \begin{algorithm} \caption{A test algorithm (Part I)} \begin{algorithmic}[1] \Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$} \ForAll {$v \in V(G)$} \State $l(v) \leftarrow \infty$ \EndFor \algstore{bkbreak} \end{algorithmic} \end{algorithm} \begin{algorithm} \caption*{A test algorithm (Part II)} \begin{algorithmic}[1] \algrestore{bkbreak} \State $p(i) \leftarrow v_j$ \State $l’(i) \leftarrow min$ \State $changed \leftarrow l \not= l’$ \EndProcedure \end{algorithmic} \end{algorithm} %\begin{algorithm} % \caption*{線搜索技術算法} % \begin{algorithmic}[1] % \State 計算$g_{k}=-\nabla f(x_{k})$,若$\parallel g_{k}\parallel<\epsilon$,則以$x_{k}$做爲極小點,中止迭代 % \State 肯定降低方法$d_{k}$ % \State 用精確搜索或非精確搜索方法肯定搜索方向$\alpha_{k}$ % \State 更新迭代點$x_{k+1}=x_{k}+\alpha_{k}d_{k}$,$k=k+1$,轉至步1 % \end{algorithmic} %\end{algorithm} % \end{document}
先用Lyx用列表的形式把算法過程編輯好,而後查看TeX源碼,把list環境下的\item替換成\State放到下列模板的xxx位置。
把標題替換本身的標題。
爲了之後修改須要,能夠把原來編輯好的內容變成Lyx note【或者comment比較好,兩者的區別在於前者不會生成TeX源碼,因此若是想要再修改的不太方便】(此內容只顯示在Lyx編輯器中,不會生成在最終的pdf文件中)【在菜單Insert->Note->Lyx note】編輯器
\begin{algorithm} \caption*{yyyyyyyyyy} \begin{algorithmic}[1] xxxxxxxxx \end{algorithmic} \end{algorithm}