问题描述
\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