latex algpseudocode 不要 end 块 | noend | no end | without end

问题描述

\documentclass{article}
\usepackage{algpseudocode}

\begin{document}
\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{document}

会生成
在这里插入图片描述
有时候,我们不希望有诸如第7行、第9行的end语句/块

解决方法

引入algpseudocode使用[noend]选项,即\usepackage[noend]{algpseudocode}
整体代码如下

\documentclass{article}
\usepackage[noend]{algpseudocode}

\begin{document}
\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{document}

会生成
在这里插入图片描述
就没有end语句/块了

algorithmicx的官方文档中是这样描述的

With noend specified all end . . . lines are omitted. You get a somewhat smaller algorithm, and the ugly feeling, that something is missing. . . The end value is the default, it means, that all end . . . lines are in their right place.

官方文档说没有end会难看。。。见仁见智吧,我觉得不难看。

其它解决方法

上述解决方法是比较正式的解决方法,该方法会取消显示所有的end语句。如果要取消显示某种end语句,比如只想取消显示end while语句
可以使用\algtext*{EndWhile}% Remove "end while" text
整体代码如

\documentclass{article}
\usepackage{algpseudocode}
\algtext*{EndWhile}% Remove "end while" text

\begin{document}
\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{document}

该方法来自https://tex.stackexchange.com/questions/51295/algpseudocode-without-end-block-text

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值