latex插入伪代码
2014-04-24 08:37阅读:
方法一:
需要宏包:
\usepackage{clrscode}
常用命令:
\Procname 算法名
\li \zi: 换行, \zi不编号
\End 可以结束 \For \While if-then if-then-else 语句
例子:
\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}
另外:
1.
\usepackage[noend]{algorithm},\usepackage[noend]{algpseudocode}中的noend选项,可以使算法显示中看不到endif,end等字眼。更
节省空间。
2.[boxed]可以使算法外围加一个大框。
基本命令:
\State
\If{}
\EndIf
\If{}
\Else
\EndIf
\If{}
\ElsIf{}
\Else
\EndIf
\For{}
\EndFor
\ForAll{}
\EndFor
\While{}
\EndWhile
\Repeat
\Until{}
\Loop
\EndLoop
\Require
\Ensure
\Function{}{}
\EndFunction
\State \Return
\Comment{}
网上的两个例子:
\documentclass{article}
\usepackage{caption}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{A test algorithm
(Part
I)}
\begin{algorithmic}[1]
\Procedure {BellmanKalaba}{$G$, $u$, $l$,
$p$}
\For {$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] %[1]表示添加行号
\algrestore{bkbreak}
\State $p(i) \leftarrow v_j$
\State $l’(i) \leftarrow min$
\State $changed \leftarrow l ot= l’$
\EndProcedure
\end{algorithmic}
\end{algorithm}
效果:

注:公式内的空格用“~~” 如:$\alpha \gets i~~i\in(0,1)$