【Latex】如何在Latex中插入偽代碼 —— clrscode3e


1. 簡介
clrscode3e是《算法導論(第三版)》使用的偽代碼的宏包,clrs其實表示的是Cormen、Leiserson、Rivest和Stein。
它有個更老的版本clrscode,這個宏包CTEX自帶。《算法導論》中的偽代碼風格與clrscode3e的風格完全一致,要比lstlistings更適合寫偽代碼。
源碼及手冊鏈接http://www.cs.dartmouth.edu/~thc/clrscode/

2. 安裝
clrscode3e並沒有預裝在CTEX中,因此需要手動加入DB中。其實很簡單。
首先,下載clrscode3e.sty,將其放置在XXX\CTEX\MiKTeX\tex\latex\clrscode3e文件夾下,注意這里可能需要創建clrscode3e文件夾。
這里,可以將clrscode3e.pdf放置在XXX\CTEX\MiKTeX\source\latex\clrscode3e文件夾方便檢索。

然后,打開WinEdt,依次點擊Tex $\rightarrow$ MikTex $\rightarrow$ MikTex Options,將它打開后,單機FNDB,會更新宏包的數據庫。
等待完成即可,如下圖所示:

3. 基本類型
CLRS3e風格的偽代碼包含四種基本類型:
(1)\id:變量;
(2)\proc:函數;
(3)\const:常量,如nil、true等;
(4)\func:可以將其理解成庫函數,如sin、sqrt等。
這四種類型可以在使用模式下使用,也可以在非公式編輯模式下使用。如下圖:

4. 基本屬性
CLRS3e將對象的屬性主要可以分成三類:

  • $\textit{i-string}$,主要指使用\id修飾的字符串,並且不包含希臘字母;
  • $\textit{x-string}$,主要指使用\id修飾的字符串,包含希臘字母或者公式下表;
  • 不屬於上述兩種,有時會把$i-string$用作$x-string$。

\attrib命令是對象名是$x-string$,屬性名是$i-string$的常用命令。如

此外,\attribxi、\attribxx、\attribii、\attribix用來表示這兩個參數是$i-string$或$x-string$的全排列組合。(第一個字符表示對象的類型,第二字符表示屬性的類型)
使用的時候,要注意的是$i-string$可以當做$x-string$用,但是一般來說$x-string$不會用作$i-string$。如

\attribb命令包含三個參數,用來表示級聯屬性,如

此外,\attribe用來表示邊$(u,v)$的屬性信息,\attribe表示屬性是$i-string$,\attribex表示屬性是$x-string$,如:

5. 雜項
下面三個命令僅適用於公式編輯模式

  • \twodots 表示".."(\cdtos等表示$\cdots$)
  • \gets 表示賦值運算符
  • \isequal 表示相等運算發,3e中等價於$==$但是長度略小於


6. codebox環境
codebox是編寫偽代碼的主要環境:

  • 使用\Procname指定函數名
  • 使用\li表示有編號的新行
  • 使用\zi表示不使用編號的行
    \begin{codebox}
    \Procname{$\proc{Insertion-Sort}(A)$}
    \li \For $j \gets 2$ \To $\attrib{A}{length}$
    \li \Do
            $\id{key} \gets A[j]$
    \li     \Comment Insert $A[j]$ into the sorted sequence
    \li     $i \gets j-1$
    \li     \While $i > 0$ and $A[i] > \id{key}$
    \li         \Do
                    $A[i+1] \gets A[i]$
    \li             $i \gets i-1$
                \End
    \li     $A[i+1] \gets \id{key}$
        \End
    \end{codebox}

生成的偽代碼如下圖所示:

codebox環境定義了一些關鍵字,具體實現可以參考源代碼,可以定制自己喜歡的關鍵字。

  • Loop相關:\For, \To, \By, \Downto, \Do, \While, \Repeat, \Until
  • Selection相關:\If, \Then, \Else, \ElseIf, \ElseNoIf
  • Jumps相關:\Return, \Error, \Goto
  • Multithreading相關:\Spawn, \Sync, \Parfor
  • Comments相關:\Comment(注釋"//"),\RComment(右對齊注釋),\CommentSymbol(注釋符號)
  • Indentation相關:\Indentmore(增加縮進),\Startalign(后續行從指定字符串開始對齊),\Stopalign(停止前面的\Startalign命令)

其中,\Do與\Then表示的命令是完全一樣的(詳見源碼)並增加1單位的縮進,都可與\End(減少1單位的縮進)配合使用。
需要注意的是使用\li對\Do和\Then所在行編號,但是它們的下一行不編號(因為\Do與\Then及\End都僅僅修改縮進並不實際顯示)。

    \begin{codebox}
    \Procname{$\proc{KMP-Matcher}(T,P)$}
    \li $n \gets \attrib{T}{length}$
    \li $m \gets \attrib{P}{length}$    
    \li $\pi \gets \proc{Compute-Prefix-Function}(P)$
    \li $q \gets 0$                             \RComment number of characters matched
    \li \For $i \gets 1$ \To $n$                \RComment scan the text from left to right
    \li     \Do
                \While $q > 0$ and $P[q+1] \neq T[i]$
    \li             \Do $q \gets \pi[q]$        \RComment next character does not match
                    \End
    \li         \If $P[q+1] \isequal x[i]$
    \li             \Then $q \gets q+1$         \RComment next character matches
                    \End
    \li         \If $q \isequal m$              \RComment is all of $P$ matched?
    \li             \Then
                        print ``Pattern occurs with shift'' $i-m$
    \li                 $q \gets \pi[q]$        \RComment look for the next match
                    \End
            \End
    \end{codebox}

上面代碼實際運行效果:

可以使用\label{li:xxx}給某行偽代碼做標記,然后在正文交叉引用該行所在編號。
同時可以使用\setlinenumber{li:xxx}直接設定偽代碼中的當前行所引用的行號。
CLRS居然是沒有break、continue這些關鍵詞的,除了自己定制新關鍵詞外也可以使用\kw命令實現$\texttt{break}$、$\texttt{continue}$的效果。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM