LaTex论文排版 | (18)算法流程图(伪代码)

之前自己尝试用visio+mathtype画算法流程图,但是总是存在着各种各样的问题,今天才知道最好的算法流程图是用latex写出来的,在网上找到相关资源,先记录下来,然后照着实践一下看看效果。

转载自:http://blog.csdn.net/u010485913/article/details/50609623

  1. \documentclass{article}  
  2. \usepackage{amsmath}  
  3. \usepackage{algorithm}  
  4. \usepackage{algpseudocode}  
  5.   
  6. \makeatletter  
  7. \def\BState{\State\hskip-\ALG@thistlm}  
  8. \makeatother  
  9.   
  10. \floatname{algorithm}{Procedure}  
  11. \renewcommand{\algorithmicrequire}{\textbf{Input:}}  
  12. \renewcommand{\algorithmicensure}{\textbf{Output:}}  
  13.   
  14. \begin{document}  
  15.     \begin{algorithm}  
  16.         \caption{My algorithm}\label{euclid}  
  17.         \begin{algorithmic}[1]  
  18.             \Procedure{MyProcedure}{}  
  19.               
  20.               
  21.                 abc  
  22.             \State $\textit{stringlen} \gets \text{length of }\textit{string}$  
  23.             \State $i \gets \textit{patlen}$  
  24.             \BState \emph{top}:  
  25.             \If {$i > \textit{stringlen}$} \Return false  
  26.             \EndIf  
  27.             \State $j \gets \textit{patlen}$  
  28.             \BState \emph{loop}:  
  29.             \If {$\textit{string}(i) = \textit{path}(j)$}  
  30.             \State $j \gets j-1$.  
  31.             \State $i \gets i-1$.  
  32.             \State \textbf{goto} \emph{loop}.  
  33.             \State \textbf{close};  
  34.             \EndIf  
  35.             \State $i \gets i+\max(\textit{delta}_1(\textit{string}(i)),\textit{delta}_2(j))$.  
  36.             \State \textbf{goto} \emph{top}.  
  37.             \EndProcedure  
  38.         \end{algorithmic}  
  39.     \end{algorithm}  
  40.       
  41.       
  42.       
  43.       
  44.     \begin{algorithm}[h]  
  45.         \caption{An example for format For \& While Loop in Algorithm}  
  46.         \begin{algorithmic}[1]  
  47.             \For{each $i\in [1,9]$}  
  48.             \State initialize a tree $T_{i}$ with only a leaf (the root);  
  49.             \State $T=T\cup T_{i};$  
  50.             \EndFor  
  51.             \ForAll {$c$ such that $c\in RecentMBatch(E_{n-1})$}  
  52.             \label{code:TrainBase:getc}  
  53.             \State $T=T\cup PosSample(c)$;  
  54.             \label{code:TrainBase:pos}  
  55.             \EndFor;  
  56.             \For{$i=1$; $i<n$; $i++$ }  
  57.             \State $//$ Your source here;  
  58.             \EndFor  
  59.             \For{$i=1$ to $n$}  
  60.             \State $//$ Your source here;  
  61.             \EndFor  
  62.             \State $//$ Reusing recent base classifiers.  
  63.             \label{code:recentStart}  
  64.             \While {$(|E_n| \leq L_1 )and( D \neq \phi)$}  
  65.             \State Selecting the most recent classifier $c_i$ from $D$;  
  66.             \State $D=D-c_i$;  
  67.             \State $E_n=E_n+c_i$;  
  68.             \EndWhile  
  69.             \label{code:recentEnd}  
  70.         \end{algorithmic}  
  71.     \end{algorithm}  
  72. \end{document}   

转自:http://www.myexception.cn/other/1259942.html

LaTeX算法排版例子

1)首先在导言区加入语句:
\usepackage{algorithm}
\usepackage{algorithmic}


2)例1

\begin{algorithm}
\caption{A}
\label{alg:A}
\begin{algorithmic}
\STATE {set $r(t)=x(t)$} 
\REPEAT 
\STATE set $h(t)=r(t)$ 
\REPEAT
\STATE set $h(t)=r(t)$ 
\UNTIL{B} 
\UNTIL{B}
\end{algorithmic}
\end{algorithm}

排版结果如下:

3)例2

\begin{algorithm}
\caption{Calculate $y = x^n$} 
\label{alg1}
\begin{algorithmic}
\REQUIRE $n \geq 0 \vee x \neq 0$ 
\ENSURE $y = x^n$ 
\STATE $y \Leftarrow 1$ 
\IF{$n < 0$} 
\STATE $X \Leftarrow 1 / x$ 
\STATE $N \Leftarrow -n$ 
\ELSE 
\STATE $X \Leftarrow x$ 
\STATE $N \Leftarrow n$
\ENDIF 
\WHILE{$N \neq 0$} 
\IF{$N$ is even} 
\STATE $X \Leftarrow X \times X$ 
\STATE $N \Leftarrow N / 2$ 
\ELSE[$N$ is odd] 
\STATE $y \Leftarrow y \times X$ 
\STATE $N \Leftarrow N - 1$ 
\ENDIF 
\ENDWHILE
\end{algorithmic}
\end{algorithm}

排版结果如下:

4)\renewcommand 改变现有命令的定义。在导言区加入如下语句:

\renewcommand{\algorithmicrequire}{ \textbf{Input:}} %Use Input in the format of Algorithm
\renewcommand{\algorithmicensure}{ \textbf{Output:}} %UseOutput in the format of Algorithm

使得原来软件包中定义的命令\REQUIRE和\ENSURE显示为Input:和Output:

一个例子如下: 

\begin{algorithm}[htb] 
\caption{ Framework of ensemble learning for our system.} 
\label{alg:Framwork} 
\begin{algorithmic}[1] %这个1 表示每一行都显示数字
\REQUIRE ~~\\ %算法的输入参数:Input
The set of positive samples for current batch, $P_n$;\\
The set of unlabelled samples for current batch, $U_n$;\\
Ensemble of classifiers on former batches, $E_{n-1}$;
\ENSURE ~~\\ %算法的输出:Output
Ensemble of classifiers on the current batch, $E_n$;
\STATE Extracting the set of reliable negative and/or positive samples $T_n$ from $U_n$ with help of $P_n$; 
\label{ code:fram:extract }%对此行的标记,方便在文中引用算法的某个步骤
\STATE Training ensemble of classifiers $E$ on $T_n \cup P_n$, with help of data in former batches; 
\label{code:fram:trainbase}
\STATE $E_n=E_{n-1}\cup E$; 
\label{code:fram:add}
\STATE Classifying samples in $U_n-T_n$ by $E_n$; 
\label{code:fram:classify}
\STATE Deleting some weak classifiers in $E_n$ so as to keep the capacity of $E_n$; 
\label{code:fram:select}
\RETURN $E_n$; %算法的返回值
\end{algorithmic}
\end{algorithm}

排版结果如下:

 5)最后一个例子

\begin{algorithm}[h]
\caption{An example for format For \& While Loop in Algorithm}
\begin{algorithmic}[1]
\FOR{each $i \in [1,9]$}
\STATE initialize a tree $T_{i}$ with only a leaf (the root);\
\STATE $T=T \cup T_{i};$\
\ENDFOR
\FORALL {$c$ such that $c \in RecentMBatch(E_{n-1})$} 
\label{code:TrainBase:getc}
\STATE $T=T \cup PosSample(c)$; 
\label{code:TrainBase:pos}
\ENDFOR
\FOR{$i=1$; $i<n$; $i++$ }
\STATE $//$ Your source here;
\ENDFOR
\FOR{$i=1$ to $n$}
\STATE $//$ Your source here;
\ENDFOR
\STATE $//$ Reusing recent base classifiers. 
\label{code:recentStart}
\WHILE {$(|E_n| \leq L_1 )and( D \neq \phi)$}
\STATE Selecting the most recent classifier $c_i$ from $D$;
\STATE $D=D-c_i$;
\STATE $E_n=E_n+c_i$;
\ENDWHILE 
\label{code:recentEnd}
\end{algorithmic}
\end{algorithm}

排版结果如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值