LaTeX伪代码写法总结

1. 伪代码所用包

一般会接触到的包有algorithm、algorithmic、algorithmicx、algorithm2e这四种包。

algorithm用于给伪代码提供一个浮动体环境,防止其换页或其他因素导致的内容中断,从而跨页显示。

algorithmic用于编辑伪代码的内容,一些for、while、if等语句通过该包中的命令进行编写。

algorithmicx可以看作algorithmic的升级版,提供了一些自定义命令

algorithm2e则是独立于algorithmic和algorithmicx的另一套伪代码环境,两套环境语法、排版上均不相同,本篇博文聚焦于algorithmicx环境中所用的一些简单命令,旨在简单快速上手LaTeX伪代码。

阅读此博文前,请先熟悉LaTeX基本语法,可参考本人博文:https://blog.csdn.net/Zerg_Wang/article/details/104120617

 

2. 环境构筑

algorithmic环境

\begin{algorithm}后面跟着的“H”是指定伪代码浮动体的位置,语法规则与普通浮动体一致。

\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}

\begin{document}
\begin{algorithm}[H]
    \caption{title}%标题
    \label{alg1}%标签
    \begin{algorithmic}
      %这里是伪代码内容
    \end{algorithmic}
\end{algorithm}
\end{document}

algorithmicx环境

若要使用algorithmicx的语法环境,调用包的时候将algorithmic包替换为一个叫algpseudocode的包即可(该包会自动调用algorithmicx包),其他地方与algorithmic的环境一致。

 

3. 语法规则

这里介绍较为常用的algorithmicx的语法命令(algorithmic命令与algorithmicx类似,区别是:前者的命令全为大写,后者仅首字母大写)

\State <text>

用于一般内容显示,当然不用该命令直接打内容也可以,但\State会为内容自动且正确地缩进,无论外面嵌套了多少个if或是循环。

\Return <text>

返回语句,一般配合\State使用:\State \Return <text>

\Comment {<text>}

注释内容,一般跟在\State命令正常内容之后,注释的内容在排版时是右对齐的。

条件判断语句

\If{<condition>} <text> \EndIf

\If{<condition>} <text> \Else <text> \EndIf

\If{<condition>} <text> \ElsIf{<condition>}  <text> \Else <text> \EndIf

其中\ElsIf语句可以多重嵌套

效果展现:

\If{$a>b$} 
  \State $max = a$
\ElsIf{$a<b$}
  \State $max = b$
\Else 
  \State same
\EndIf

循环语句

\For{<condition>} <text> \EndFor

\ForAll{<condition>} <text> \EndFor

\While{<condition>} <text> \EndWhile

\Repeat <text> \Until{<condition>}

\Loop <text> \EndLoop

输入输出语句

\Require <text>

\Ensure <text>

当然,如果想改成“Input”“Output”形式,或者“输入”“输出”这种形式,可以在导言区重新定义该命令:

\renewcommand{\algorithmicrequire}{\textbf{自定义输入}}
\renewcommand{\algorithmicensure}{\textbf{自定义输出}}

函数体

\Function{<name>}{<params>} <text> \EndFunction

特殊字符

以下字符仅用于algorithmic环境中

\TRUE \FALSE \AND \OR \XOR \NOT

 

4. 其他

显示行号

\begin{algorithmic}[1]

“1”表示每行均显示行号,如果填“2”,意思是每2行显示一次行号。

标题与标号更改

如果不作额外更改,标题一行会显示为:

如果有多个伪代码,标号为2或者其他数字,则可以在编辑标题内容的语句(环境区)前加:

\setcounter{algorithm}{1}

命令中的数字为1,显示的数字则为1+1=2,以次类推。

如果连标题中的“Algorithm”也要更改为其他内容,可在导言区自定义标签:

\floatname{algorithm}{自定义名}

 

5. 参考资料

https://zhuanlan.zhihu.com/p/145195565

http://hustsxh.is-programmer.com/posts/38801.html

 

  • 70
    点赞
  • 233
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
LaTeX中,有几个包可以用来伪代码,如algorithmicx、algpseudocode和algorithm2e。 如果你选择使用algorithmicx包,你可以使用其中的一些命令来编伪代码,例如for循环、while循环和if语句等。这些命令可以让你按照算法的逻辑结构来描述你的伪代码。 如果你使用algpseudocode包,它的使用方法与algorithmicx包中的algpseudocode相同,也可以用来编伪代码。 另外,algorithm2e是另一套独立于algorithmicx的伪代码环境,它与algorithmicx在语法和排版上也有所不同。你可以通过algorithm2e包来编LaTeX伪代码。 总的来说,你可以根据自己的偏好和需求选择适合你的包来编LaTeX伪代码。无论你选择哪一个包,都可以使用相应的命令来描述算法的逻辑结构和流程。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [LaTeX伪代码](https://blog.csdn.net/qq_44947550/article/details/127251802)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [LaTeX伪代码写法总结](https://blog.csdn.net/Zerg_Wang/article/details/113667980)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值