当前位置: 首页 > news >正文

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}

排版结果如下:



 

相关文章:

  • 高斯卷积
  • Matlab PCA 算法
  • Matlab排序函数
  • Matlab取整函数
  • Matlab图像直方图相关函数
  • Matlab中find函数的使用说明
  • Matlab中arrayfun函数
  • Matlab数形结合求解不等式
  • Matlab中的fzero和fsolve函数
  • Matlab 如何用legend标记部分曲线
  • Matlab中绘制带箭头的坐标轴
  • Matlab删除cell数组中全为NaN的行和列
  • Ubuntu环境变量PATH设置
  • 【MLF Week 7 上】Overfitting和Regularization
  • Windows 7 下查询设备详细信息
  • 【技术性】Search知识
  • 【知识碎片】第三方登录弹窗效果
  • CentOS7 安装JDK
  • CSS中外联样式表代表的含义
  • JAVA多线程机制解析-volatilesynchronized
  • Java反射-动态类加载和重新加载
  • Mithril.js 入门介绍
  • orm2 中文文档 3.1 模型属性
  • Webpack4 学习笔记 - 01:webpack的安装和简单配置
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 软件开发学习的5大技巧,你知道吗?
  • 深度学习在携程攻略社区的应用
  • 思否第一天
  • 最简单的无缝轮播
  • $forceUpdate()函数
  • (007)XHTML文档之标题——h1~h6
  • (16)Reactor的测试——响应式Spring的道法术器
  • (3)STL算法之搜索
  • (板子)A* astar算法,AcWing第k短路+八数码 带注释
  • (四)图像的%2线性拉伸
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (转)shell调试方法
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • (轉貼) 2008 Altera 亞洲創新大賽 台灣學生成果傲視全球 [照片花絮] (SOC) (News)
  • (轉貼)《OOD启思录》:61条面向对象设计的经验原则 (OO)
  • .NET WebClient 类下载部分文件会错误?可能是解压缩的锅
  • .set 数据导入matlab,设置变量导入选项 - MATLAB setvaropts - MathWorks 中国
  • /3GB和/USERVA开关
  • /etc/apt/sources.list 和 /etc/apt/sources.list.d
  • ::什么意思
  • [ vulhub漏洞复现篇 ] Apache APISIX 默认密钥漏洞 CVE-2020-13945
  • [AIGC] 广度优先搜索(Breadth-First Search,BFS)详解
  • [Android]使用Android打包Unity工程
  • [Angular] 笔记 8:list/detail 页面以及@Input
  • [C++] sqlite3_get_table 的使用
  • [CareerCup] 14.5 Object Reflection 对象反射
  • [CISCN2019 华东南赛区]Web11
  • [DM复习]Apriori算法-国会投票记录关联规则挖掘(上)
  • [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
  • [HTML]Web前端开发技术7(HTML5、CSS3、JavaScript )CSS的定位机制——喵喵画网页