下图是我的目标。你可以看到有一行描述了算法1的参数。我想知道如何添加这样的行。
我还想知道如何在 Algorithm1 下添加以下函数的描述,以使它们像一个主体(没有空白)。我相信他们使用了相同的技术。
如果您需要,我的代码如下。但您只需使用简单的几行代码即可帮助我。如果您能帮助我,我将非常高兴。谢谢!!!
\documentclass{article}
\usepackage[ruled,linesnumbered]{algorithm2e}
\usepackage{algpseudocode}
\begin{document}
An algorithm for test.
\begin{center}
\begin{algorithm}[H]
\KwIn{$p_1=(a_1, b_1), \ldots, p_{10}=(a_{10}, b_{10})$, ten points$p_i(1 \leq i \leq 10)$ on an Euclidean plane.} % Algorithm inputs
\KwOut{$dist$, the distance the closest pair of points.} %Algorithm outputs
\medskip
compute $dist \leftarrow$ MaxValue; \algorithmiccomment{MaxValue is the maximus value of the type of $dist$.}
\For{$i \leftarrow 1$ {\rm to} $10$ }{
\For{$j \leftarrow i$ {\rm to} $10$}{
$tempDist \leftarrow$ GetDistance($p_i, p_j$) \\
\If{$dist < tempDist$}{
$dist \leftarrow tempDist$
}
}
}
\Return{dist}
\caption{\texttt{TheShortestDistancev1}} % Algorithm name
\label{alg:TheShortestDistancev1} % optional label to refer to
\end{algorithm}
\end{center}
\end{document}
答案1
不要混合使用algorithm2e
和algorithmicx
的algpseudocode
。两者都提供自己的功能。相反,使用algorithms
捆的algorithm
包(用于浮动行为/样式)和algpseudocode
代码样式。
下面你会看到类似于你的输出的内容,使用\State
s 表示算法中的每个语句,并\fsmid
设置一个mid
dle 规则f
loat s
tyle(由float
形式为\@fs@mid
)。
\documentclass{article}
\usepackage[margin=1in]{geometry}% Just for this example
\usepackage{amsmath}
\usepackage{algorithm,algpseudocode}
\makeatletter
\newcommand{\fsmid}{\par\@fs@mid}
\makeatother
\newcommand{\var}{\texttt}
\newcommand{\func}{\textit}
\begin{document}
\begin{algorithm}
\caption{TheShortestDistancev1}% Algorithm name
Here are some additional parameters
\fsmid
\begin{algorithmic}[1]
\Require $p_1 = (a_1, b_1), \dots, p_{10} = (a_{10}, b_{10})$, ten points $p_i$ ($1 \leq i \leq 10$) on an Euclidean plane.% Algorithm inputs
\Ensure $\var{dist}$, the distance the closest pair of points.% Algorithm outputs
\medskip
\State compute $\var{dist} \leftarrow \var{MaxValue}$ \Comment{\var{MaxValue} is the maximum value of the type of $\var{dist}$.}
\For{$i \leftarrow 1~\textrm{to}~10$ }
\For{$j \leftarrow i~\textrm{to}~10$}
\State $\var{tempDist} \leftarrow \func{GetDistance}(p_i, p_j)$
\If{$\var{dist} < \var{tempDist}$}
\State $\var{dist} \leftarrow \var{tempDist}$
\EndIf
\EndFor
\EndFor
\State \Return $\var{dist}$
\end{algorithmic}
\medskip
\fsmid
\textbf{function} Some other function
\fsmid
\begin{algorithmic}
\Require $p_1 = (a_1, b_1), \dots, p_{10} = (a_{10}, b_{10})$, ten points $p_i$ ($1 \leq i \leq 10$) on an Euclidean plane.% Algorithm inputs
\Ensure $\var{dist}$, the distance the closest pair of points.% Algorithm outputs
\medskip
\State compute $\var{dist} \leftarrow \var{MaxValue}$ \Comment{\var{MaxValue} is the maximum value of the type of $\var{dist}$.}
\For{$i \leftarrow 1~\textrm{to}~10$ }
\For{$j \leftarrow i~\textrm{to}~10$}
\State $\var{tempDist} \leftarrow \func{GetDistance}(p_i, p_j)$
\If{$\var{dist} < \var{tempDist}$}
\State $\var{dist} \leftarrow \var{tempDist}$
\EndIf
\EndFor
\EndFor
\State \Return $\var{dist}$
\end{algorithmic}
\end{algorithm}
\end{document}