首页 > 其他分享 >algorithmicx(use algpseudocode as layout)学习记录

algorithmicx(use algpseudocode as layout)学习记录

时间:2023-03-24 13:48:05浏览次数:43  
标签:use layout algorithm While algpseudocode State algorithmic algorithmicx block

这几天写算法作业,提供的tex文件中使用algorithmicx书写伪代码,虽然也会用algorithm2e,但技多不压身,现在就来学一学。

目录

概述

相比algorithm2e的复杂命令(If,eIf,uIf,...),algorithmicx真是十分的简单。要使用algorithmicx,可以使用algpseudocode作为layout,它会导入algorithmicx[1]

关于algorithm, algorithmic, algorithmicx, algorithm2e, algpseudocode之间的关系,可以看看stackexchange上的回答

\usepackage{algorithm}
\usepackage[noend]{algpseudocode} %end or noend

Example

view code
\begin{algorithm}[H]
			\caption{Quick Sort algorithm}\label{alg:sampling1}
			\begin{algorithmic}[1]
				\Function {QSort} {$l,r$}
				\If{$l \geq r$}
				\State \textbf{exit}
				\EndIf

				\State pick a random element in $A[l], ..., A[r]$ as $pivot$
				\State $i=l, j=r$

				\While{$i\leq j$}
				\While{$A[i] < pivot$} $i=i+1$
				\EndWhile
				\While{$A[j] > pivot$} $j=j-1$
				\EndWhile

				\If{$i \leq j$}
				\State \textsc{SWAP}($A[i], A[j]$)
				\State $i=i+1$
				\State $j=j-1$
				\EndIf
				\EndWhile

				\State \textsc{QSort}($l, j$)
				\State \textsc{QSort}($i, r$)
				\EndFunction
			\end{algorithmic}
		\end{algorithm}

image

Details

行号

每个算法以\begin{algorithmic}[lines]命令开头。lines为0则不标行号;若为1则每行标记一次;若为n则每n行标记一次。

\State开启一个新行。

注释

\Comment命令来添加注释,该命令和algorithmic不兼容。

引用

我们可以用\label来给算法或者算法的某一行来设置标签,并在随后用\ref来引用它。在用\ref引用某一行时,只会显示行号,有时我们想精确地表示它是具体哪个算法的哪一行,可以使用\algref,方法如下。

view code

\begin{algorithm}
	\caption{Euclid’s algorithm}\label{euclid}
	\begin{algorithmic}[1]
		\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
		\State $r\gets a\bmod b$
		\While{$r\not=0$}\Comment{We have the answer if r is 0}
			\State $a\gets b$
			\State $b\gets r$
			\State $r\gets a\bmod b$
			\EndWhile\label{euclidendwhile}
		\State \textbf{return} $b$\Comment{The gcd is b}
	\EndProcedure
	\end{algorithmic}
\end{algorithm}
The \textbf{while} in algorithm \ref{euclid} ends in line \ref{euclidendwhile}, so \algref{euclid}{euclidendwhile} is the line we seek.

image

Commands

if block

\If, \Else, \ElseIf, \EndIf

for block

\For, \ForAll, \EndFor

while block

\While, \EndWhile

repeat block

\Repeat, \Until

procedure block

\Procedure, \EndProcedure

function block

\Function, \EndFunction


  1. algorithmicx documentation page5 ↩︎

标签:use,layout,algorithm,While,algpseudocode,State,algorithmic,algorithmicx,block
From: https://www.cnblogs.com/coco02/p/17250688.html

相关文章

  • when should we use struct in C++?
    InC++,structisakeywordusedtodefineadatastructurethatgroupsmultiplevariablesofdifferentdatatypesintoasingleunit.Herearesomesituations......
  • DrawerLayout(官方侧滑菜单)的简单使用
    本节给大家带来基础UI控件部分的最后一个控件:DrawerLayout,官方给我们提供的一个侧滑菜单控件,和上一节的ViewPager一样,3.0以后引入,低版本使用它,需要v4兼容包,说到侧滑,相信很多......
  • 220-You are user number 3 of 50 allowed.
    FileTransferProtocol(FTP)ftp220----------WelcometoPure-FTPd[privsep][TLS]----------220-Youareusernumber3of50allowed.220-Localtimeisnow22:12......
  • openSUSE下修改python3为默认python
    前言在大多数基于Ubuntu和Debian的Linux发行版中,python命令通常指向Python2解释器,而python3命令则指向Python3解释器。这可能会导致使用Python3的脚本和......
  • ArrayList的contains方法(转) list.contains(user)时实际上比较的是user.equals(obje
    ArrayList的contains方法(转)https://www.shuzhiduo.com/A/x9J216pez6/今天在用ArrayList类的caontains方法是遇到了问题,我写了一个存放User类的ArrayList 但在调用lis......
  • openSUSE 系统, 安装 "snapd" 软件包
    简介Snaps是一种应用程序打包格式,可以在各种Linux发行版上运行,从而使软件安装变得更加简单和可靠。安装在openSUSE中启用Snaps支持需要安装“snapd”软件包。您......
  • react中的useRef和useContext
    1.useRef和useState类似,都是用来更新数据,但是useRef更新数据是同步的useRef返回一个对象,初始化数据保存在current字段下import{useRef}from'react';constdata=......
  • react项目中state和useState
    1.在类组件中,数据保存在state中,更新数据使用setStatesetState有两种用法函数式state={count:0}...setState(state=>(count:state.count+1))对象式state={......
  • Stack Exchange & Stack Overflow User Reputation Leagues All In One
    StackExchange&StackOverflowUserReputationLeaguesAllInOne......
  • 基于 ByteHouse 构建实时数仓实践
    更多技术交流、求职机会,欢迎关注字节跳动数据平台微信公众号,回复【1】进入官方交流群随着数据的应用场景越来越丰富,企业对数据价值反馈到业务中的时效性要求也越来越高,......