加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

latex插入伪代码

(2014-04-24 08:37:15)
分类: tex

方法一:

需要宏包:

 \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}

 

 效果:

http://s6/mw690/001t3Vlyzy6IlLcqZMhf5&690

方法二:

需要宏包:

\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 \not= l’$
    \EndProcedure
  \end{algorithmic}
\end{algorithm}

 

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

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有