• Lyx/LaTeX笔记04---插入伪代码


     

    1 可用的宏包

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

     

    2 clrscode

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

    egin{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 	wodots 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环境

    egin{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
    %
    ewcommandli{fontspec{LiSu}}
    %
    ewcommandhei{fontspec{SimHei}}
    %
    ewcommandyou{fontspec{YouYuan}}
    %
    ewcommandsun{fontspec{SimSun}}
    %
    ewcommandfangsong{fontspec{仿宋_GB2312}}
    %
    ewcommandkai{fontspec{KaiTi_GB2312}}
    %%
    umberwithin
    %%
    umberwithin{equation}{section}
    
    egin{document}
    egin{algorithm}
      caption{A test algorithm (Part I)}
      egin{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}
    
    egin{algorithm}
      caption*{A test algorithm (Part II)}
      egin{algorithmic}[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}
    
    %egin{algorithm}
    %  caption*{线搜索技术算法}
    %  egin{algorithmic}[1]
    %  State 计算$g_{k}=-
    abla 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】

    egin{algorithm}
      caption*{yyyyyyyyyy}
      egin{algorithmic}[1]
      xxxxxxxxx
      end{algorithmic}
    end{algorithm}
    

     

  • 相关阅读:
    数据类型之间的转换(int ,str ,bool)
    字符串的操作及其部分格式化
    逻辑运算符
    格式化输出
    @Resource、@Autowired、@Qualifier
    maven环境搭建
    oracle 用户管理
    volatile关键字
    Ajax
    拷贝InputStream
  • 原文地址:https://www.cnblogs.com/zhaoyang10/p/4543383.html
Copyright © 2020-2023  润新知