基于algorithm2e的LaTeX算法流程使用 (含中英文模板)

基于algorithm2e的LaTeX算法流程使用 [含中英文模板]

使用介绍

采用 LaTeX \LaTeX LATEX写算法流程,可以采用以下三种方式:

  • 采用algpseudocode和algorithm宏包实现
  • 采用algorithmic和algorithm宏包实现
  • 采用algorithm2e宏包实现

根据使用者的使用偏好,可以选择不同的实现方式。本文考虑采用algorithm2e宏包实现 LaTeX \LaTeX LATEX的算法流程。

使用前,需要导入algorithm2e宏包,即

\usepackage[linesnumbered,ruled,noline]{algorithm2e}

其中, [ ] [\quad ] []中的内容为算法的选项说明,linesnumbered表示显示数字行号,ruled表示将算法名放在算法的开头,noline表示不添加连接线。更多选项可以参考algorithm2e的官方文档。

如果需要使用中文,则需要导入ctex宏包,即

\usepackage[UTF8]{ctex}  

并可以通过定义等价命令的方式将算法的关键词改为中文,即

\renewcommand{\algorithmcfname}{算法}  
\SetKwInOut{KwIn}{输入}  
\SetKwInOut{KwOut}{输出}  

算法的基本环境为

\begin{algorithm} 
	% your algorithm here.
\end{algorithm}

算法由标题、输入、输出、主体部分组成。

算法标题的添加方式为

\caption{title of your algorithm}

算法输入的添加方式为

\KwIn{Your algorithm input}

算法输出的添加方式为

\KwOut{Your algorithm output}

在algorithm2e中,用 \; 命令表示换行,默认在行末显示分号

如果不想显示分号,可以在算法环境中添加

\DontPrintSemicolon

下面简要介绍algorithm2e的关键词用法

for循环的实现方式为

\For{condition}{
	for block}

if的实现方式为

\If{condition}{
	then block}

if-else的实现方式为

\eIf{condition}{
	then block 1}
{
	then block 2}

if-elseif-else的实现方式为

\uIf {condition A}{
	then block 1} 
\ElseIf {condition B}{
	then block 2}

while的实现方式为

\While{condition}{
	while block}

中文模板

\documentclass{article}

\usepackage[linesnumbered,ruled,noline]{algorithm2e}
\usepackage[UTF8]{ctex}

\begin{document}
	
	\begin{algorithm}
		\renewcommand{\algorithmcfname}{算法}
		\SetKwInOut{KwIn}{输入}
		\SetKwInOut{KwOut}{输出}
		\DontPrintSemicolon
		\caption{你的算法中文模板}
		\KwIn{你的算法输入}
		\KwOut{你的算法输出}
		give me some description here.\;
		算法内容只是为练习algorithm2e关键词用法而写,无具体意义\;
		\tcp{comment here.}
		\For{i = 1 : $N$}{
			$j = i+1$\;
			\If{i == 1}{
				$j = 0$\;
				Flag = True\;
			}\tcp{judement logic 判断逻辑}
			\eIf{i == 2}
			{
				$j = 1$\;
				Flag = False\;
			}
			{
				$j = 2$\;
				Flag = True\;
			}
		}
		
		\While{j~=0}{
			count = count + 1\;
		\If{count $\ge$ 1000}{
			\uIf {condition 1} {
				code 1\;
			} \ElseIf {condition 2} {
				code 2\;
			}
		}
		}
	\end{algorithm}
	
\end{document}

英文模板

\documentclass{article}

\usepackage[linesnumbered,ruled,noline]{algorithm2e}


\begin{document}
	
	\begin{algorithm}
		\DontPrintSemicolon
		\caption{Your Algorithm Template}
		\KwIn{Your algorithm input}
		\KwOut{Your algorithm output}
		give me some description here.\;
		\tcp{comment here.}
		\For{i = 1 : $N$}{
			$j = i+1$\;
			\If{i == 1}{
				$j = 0$\;
				Flag = True\;
			}\tcp{judement logic}
			\eIf{i == 2}
			{
				$j = 1$\;
				Flag = False\;
			}
			{
				$j = 2$\;
				Flag = True\;
			}
		}
		\While{j~=0}{
			count = count + 1\;
		\If{count $\ge$ 1000}{
			\uIf {condition 1} {
				code 1\;
			} \ElseIf {condition 2} {
				code 2\;
			}
		}
		}
	\end{algorithm}
	
\end{document}

参考文献

[1] https://www.overleaf.com/learn/latex/Algorithms
[2] algorithm2e.pdf (cicku.me)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值