【转】LaTeX插入伪代码1

来源:互联网 发布:炉石传说记牌器mac 编辑:程序博客网 时间:2024/04/27 21:59

Table of Contents

  • 1 可用的宏包
  • 2 clrscode
  • 3 algorithm, algorithmic
    • 3.1 Preamble
    • 3.2 algorithm环境
    • 3.3 效果
  • 4 Trick

1 可用的宏包

常用的排版伪代码包有clrscode, algorithm, algorithmic, algorithmicx, algorithm2e

2 clrscode

clrscode是著名的算法教材 Introduction to Algorithms, 2nd ed. 的作者排版该书时自己制定的。【c l r s 分别是该书四个作者的姓的第一个字母,因此人们常以 clrs 指称该书。】
示例:
该包支持显示行号,加入注释,能够每行 label 及引用。
以下是 Insertion-Sort 算法的 code

 

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

3 algorithm, algorithmic

 

3.1 Preamble

 

\usepackage{caption}\usepackage{algorithm}\usepackage{algpseudocode}

3.2 algorithm环境

 

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

3.3 效果


4 Trick

先用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}

Author: visaya fan <visayafan[AT]gmail.com>

Date: 2012-06-14 23:25:48

HTML generated by org-mode 6.33x in emacs 23

0 0
原创粉丝点击