如何在算法环境中的两个函数之间放置垂直空间?

如何在算法环境中的两个函数之间放置垂直空间?

为了使算法可读,我想在两个函数之间留一些空格。例如:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}[t]
  \begin{algorithmic}[1]
    \Function{Euclid1}{$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
    \State \textbf{return} $b$\Comment{The gcd is b}
    \EndFunction

    \Function{Euclid2}{$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
    \State \textbf{return} $b$\Comment{The gcd is b}
    \EndFunction

  \end{algorithmic}
  \caption{Euclid2’s algorithm}\label{euclid2}
\end{algorithm}
\end{document}

答案1

添加\Statex未编号的行(或\State编号的行):

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}[t]
  \begin{algorithmic}[1]
    \Function{Euclid1}{$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
    \State \textbf{return} $b$\Comment{The gcd is b}
    \EndFunction
    \Statex
    \Function{Euclid2}{$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
    \State \textbf{return} $b$\Comment{The gcd is b}
    \EndFunction

  \end{algorithmic}
  \caption{Euclid2’s algorithm}\label{euclid2}
\end{algorithm}
\end{document}

在此处输入图片描述

相关内容