LaTeX各种算法排版

2023-10-31

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}

排版结果如下:

6)开始中头部加入

\makeatletter
\newenvironment{breakablealgorithm}
  {% \begin{breakablealgorithm}
   \begin{center}


     \refstepcounter{algorithm}% New algorithm
     \hrule height.8pt depth0pt \kern2pt% \@fs@pre for \@fs@ruled
     \renewcommand{\caption}[2][\relax]{% Make a new \caption


       {

\raggedright\textbf{\ALG@name~\thealgorithm} ##2\par}%
       \ifx\relax##1\relax % #1 is \relax
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##2}%
       \else % #1 is not \relax
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##1}%
       \fi
       \kern2pt\hrule\kern2pt
     }


  }{% \end{breakablealgorithm}
     \kern2pt\hrule\relax% \@fs@post for \@fs@ruled


   \end{center}
  }
\makeatother

算法中使用:

\begin{breakablealgorithm}
    \caption{Multi task balanced scheduling algorithm}
    \label{alg1}
    \begin{algorithmic}[1]

%%%REQUIRE 输入
    \REQUIRE ~~\\ 
    $G$ = 250;
    Size = 30;
    CodeL = 10;
    \FOR{$i = 1:3$}
        \STATE Min $X(i)$ = 0.1 * ones(1);
        \STATE Max $X(i)$ = 3 * ones(1);
    \ENDFOR
    \FOR{$i = 4:1:9$}
        \STATE Min $X(i)$ = -3 * ones(1);
        \STATE Max $X(i)$ = 3 * ones(1);
    \ENDFOR
    \FOR{$i = 10:1:12$}
        \STATE Min $X(i)$ = -ones(1);
        \STATE Max $X(i)$ = ones(1);
    \ENDFOR
    \STATE $E$ = round(rand(Size,12*CodeL));
    \STATE BsJ = 0;
    \FOR{$kg = 1:1:G$}
        \STATE time(kg) = kg
        \FOR{$s$ = 1:1:Size}
            \STATE $m = E(s,:)$;
            \FOR{$j = 1:1:12$}
                \STATE $y(j) = 0;$
                \STATE $mj = m((j-1)$ * CodeL + $1:1:j$ * CodeL);
                \FOR{$i = 1:1:$CodeL}
                    \STATE $y(j) = y(j) + mj(i) * 2^(i - 1);$
                \ENDFOR
                \STATE $f(s,j) =$ (Max $X(j)$ - Min $X(j)$) * $y(j)/1023$ + Min $X(j)$;
            \ENDFOR
            \STATE $p = f(s,:);$
            \STATE [p,BsJ] = fitness(p,BsJ);
            \STATE BsJi(s) = BsJ;
        \ENDFOR
        \STATE [OderJi,IndexJi] = sort(BsJi);
        \STATE BestJ(kg) = OderJi(1);
        \STATE BJ = BestJ(kg);
        \STATE Ji = BsJi + 1e-10;

        \STATE fi = 1./Ji;
        \STATE [Oderfi,Indexfi] = sort(fi);
        \STATE Bestfi = Oderfi(Size);
        \STATE BestS = E(Indexfi(Size),:);
        \STATE kg
        \STATE p
        \STATE BJ

        \STATE fi\_sum = sum(fi);
        \STATE fi\_Size = (Oderfi/fi\_sum) * Size;
        \STATE fi\_S = floor(fi\_Size);
    \ENDFOR

   %%%%%注意下句 ensure 输出 

\ENSURE
    \STATE kk = 1;
    \FOR{$i = 1:1:$Size}
        \FOR{$j = 1:1:$fi\_S(i)}
            \STATE TempE(kk,:) = E(Indexfi(i),:);
            \STATE kk = kk + 1;
        \ENDFOR
    \ENDFOR
    \end{algorithmic}
\end{breakablealgorithm}

样式:

7)例子7

\begin{algorithm}[!htb]
    \caption{Low latency video coding rate control algorithm}


    \label{alg1}
    \begin{algorithmic}[1]
        \FOR{$n=1$ to $N$}
            \STATE $G n=S B S_{n}$;
        \ENDFOR


        \STATE $G=\left\{G_{1}, G_{2}, \ldots G_{N}\right\}$
        \STATE $N=\{G, G, \ldots, G\}$


        \WHILE{The minimum value of $L$ is less than $K$}
            \STATE Find out the subscripts of the two groups corresponding to the minimum value of $n^{*}, m^{*}$;
            \STATE $G_{m^{*}}$ and $G_{n^{*}}$ were removed from $G$;
            \STATE Merge $\left\{G_{n^{*}}, G_{m^{*}}\right\}$ into $G$;
            \STATE Reset $L$ to null;


            \FOR{$G_{n} \subset G$ and $n>0$}
                \FOR{$G_{m} \subset G$ and $m>n$}
                    \STATE $L^{\prime} \leftarrow L\left(G_{n}, G_{m}\right)$; $m$\hspace{2pt}-\hspace{-2pt} -; $n$\hspace{2pt}-\hspace{-2pt} -
                \ENDFOR


            \ENDFOR
        \ENDWHILE


        \ENSURE group set $G$;
    \end{algorithmic}
\end{algorithm}

样式:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

LaTeX各种算法排版 的相关文章

随机推荐

  • 2、模板方法

    文章目录 概念 demo 概念 父类定义抽象 的 template method 并由别的方法调用 子类实现具体的 template method 和观察者类似 回调函数 钩子函数 都是此模式的应用 观察者一般是多个 方法由被观察者调用 模
  • redhat6.4 下安装oracle 11g详细完整的步骤

    一 虚拟机安装操作系统 Redhat 6 4 注意 linux下安装oracle 11 如果是redhat系统 选择redhat6版本的 因为redhat7版本里面少一个安装数据库用到的包compat libstdc 33 没有这个包 安装
  • flutter自定义广告Banner

    Flutter 1 0 is out Tuesday December 4 2018 Banner是手机应用最常见的需求之一 https pub dartlang org flutter中搜索Banner找到两个开源库 引入项目后 分别存在
  • 项目应用:使用自定义注解完成对controller的aop控制

    在项目进行过程中 需要使用aop对api传入或者传出数据进行日志记录 考虑实际应用 放弃了使用拦截器 转而使用aop的前置通知和返回通知中加入日志记录操作 在spring中 controller会被JDK自动代理 当使用自定义标签时cont
  • R语言的基本数据类型:数值,字符串,逻辑

    R语言的基本数据类型 数值 双整型 储存普通数值型数据 可正可负 可大可小 可含小数可不含 R中输入的任何一个数值都默认以双整型存储 可以使用typeof 函数来查看对象类型 在数据科学里 它更常被称为数值型 整型 只能用来储存正数 在R中
  • 前端面试题之——兼容篇

    兼容性面试题 一 html 部分 1 H5 新标签在 IE9 以下的浏览器识别 html5shiv js 下载地址 2 ul 标签内外边距问题 ul 标签在 IE6 IE7 中 有个默认的外边距 但是在 IE8 以上及其他浏览器中 有个默认
  • websphere6.x 调用集群EJB的Context.PROVIDER_URL写法

    import java util Hashtable import javax naming Context import javax naming InitialContext Hashtable env new Hashtable en
  • caffe源码解析 — net.cpp

    Net类是Solve类的一个成员 在net cpp中定义了对Net的所有操作 其中包括 Init GetLearningRateAndWeightDecay ForwardPrefilled Backward ShareTrainedLay
  • 计算机数值转换教案,计算机数值方法教案.doc

    第O章 绪论 一 教学设计 1 教学内容 数值计算方法这门课程的形成背景及主要研究内容 研究方法 主要特点 算法的有关概念及要求 误差的来源 意义 及其有关概念 数值计算中应注意的一些问题 2 重点难点 算法设计及其表达法 误差的基本概念
  • 数据结构——快速排序

    1 原理及代码实现 快速排序 快速排序也是一种分治的排序算法 快速排序和归并排序是互补的 归并排序将数组分成两个子数组分别排序 并将有序的子数组归并以将整个数组排序 会需要一个额外的数组 而快速排序的排序方式是当两个子数组都有序时 整个数组
  • @Async注解线程池

    在启动类上添加 EnableAsync配合该注解使用 自定义线程池方法 implements AsyncConfigurer extends AsyncSupportConfigurer Bean 注入线程池 public interfac
  • Shell中表示数字跨度的几种方式

    在Shell脚本中 如果要输出数字为0 20中3的倍数 可以使用下面三种方式来完成 方式一 i 0 i lt 20 i 3 for i 0 i lt 20 i 3 do echo i done 方式二 0 20 3 for i in 0 2
  • ElasticSearch 评分排序

    背景 通过脚本改变评分 背景 近期有一个需求 需要对优惠券可用商品列表加个排序 只针对面值类的券不包括折扣券 需求是这样的 假设有一张面值券 50 块钱 可用商品列表 A 100 B 40 C 10 当用户查询当前券可用商品列表的时候优先将
  • RuntimeError: expected dtype Double but got dtype Float 损失求导出错

    求导出现问题 源代码 修改为 把损失修改为loss clone detach requires grad
  • 卫星通信的资源调度 vs 地面无线通信的资源调度

    以OFDM系统为例 考虑多个用户共用多个子载波的场景 一 两类资源 调整时频资源 即基站或卫星决定给各个用户在哪些时隙分配多少子载波 调整发射功率 即基站或卫星决定给每个用户在每个子信道上的功率是多大 用字母 P n k P n k Pn
  • 【TestNG】TestNG使用教程详解

    一 TestNG介绍 TestNG是Java中的一个测试框架 类似于JUnit 和NUnit 功能都差不多 只是功能更加强大 使用也更方便 详细使用说明请参考官方链接 https testng org doc index html 二 Te
  • anaconda 创建虚拟环境、激活及使用的基本方法

    1 查看当前存在的虚拟环境 conda env list 2 创建虚拟环境 环境名 conda create n 环境名 python X X 3 激活指定虚拟环境 activate 环境名 4 删除虚拟环境 conda remove n
  • c++ 鼠标控制

    windows下获得鼠标位置和控制鼠标 include
  • 使用nodejs开发一个markdown文档管理小系统(一)Using Nodejs to quickly develop a markdown management system...

    好多年没碰过前端jquery了 用一两天时间重温一下 刚好写个小工具 不递归取文件夹和文件 只写一层 保持足够简单 验证和参数判断暂不写 毕竟只写了几个小时而已 功能算完备了 添加一个简单的管理员权限管理修改的所有功能即可放出去了 看来还不
  • LaTeX各种算法排版

    1 首先在导言区加入语句 usepackage algorithm usepackage algorithmic 2 例1 begin algorithm caption A label alg A begin algorithmic ST