algorithm2e 中的嵌套 If 和 For

algorithm2e 中的嵌套 If 和 For

我在 algorithm2e 中编写了以下代码,其中包含 boxruled、vlined 和 linesnumbered 选项。pdflatex 在 ifs 中的两个 for 循环末尾都给出了一个空行。有人能建议一种删除多余行的方法吗?谢谢。

\If {$\lfloor c\ (\log (n+1) - \log n) \rfloor > 1$}
{
    $u \leftarrow {\tt SampleControl}$\;
    $U \leftarrow \{ u \} \cup U_m$\;
}
${\tt ConnectState}(z, S_{n+1}, U, P_n, Q_n, \delta_n)$\;
\eIf{$\min_{u \in U} \Delta t_{n+1} (z_{n+1}, u) \leq \delta_n$}
{   $\delta_{n+1} = \delta_n/2$\;
    \For{$z \in S_{n+1}$}{
        ${\tt ConnectState}(z, S_{n+1}, U, P_{n+1}, Q_{n+1}, \delta_{n+1})$\;
}}{ $\delta_{n+1} \leftarrow \delta_n$\;
    \For{$z' \in Z_\mathrm{near}(z_{n+1})$}{
        ${\tt ConnectState}(z', S_{n+1}, U, P_{n+1}, Q_{n+1}, \delta_{n+1})$\;
}}

答案1

if也许您指的是和块末尾的额外垂直空间for。它可以通过以下方式配置\SetVlineSkip

\documentclass{article}
\usepackage[boxruled,vlined,linesnumbered]{algorithm2e}

\SetVlineSkip{0pt}      

\begin{document}

\begin{algorithm}
\caption{Caption of algorithm}
\If {$\lfloor c\ (\log (n+1) - \log n) \rfloor > 1$}
{
    $u \leftarrow {\tt SampleControl}$\;
    $U \leftarrow \{ u \} \cup U_m$\;
}
${\tt ConnectState}(z, S_{n+1}, U, P_n, Q_n, \delta_n)$\;
\eIf{$\min_{u \in U} \Delta t_{n+1} (z_{n+1}, u) \leq \delta_n$}
{   $\delta_{n+1} = \delta_n/2$\;
    \For{$z \in S_{n+1}$}{
        ${\tt ConnectState}(z, S_{n+1}, U, P_{n+1}, Q_{n+1},
\delta_{n+1})$\;
}}{ $\delta_{n+1} \leftarrow \delta_n$\;
    \For{$z' \in Z_\mathrm{near}(z_{n+1})$}{
        ${\tt ConnectState}(z', S_{n+1}, U, P_{n+1}, Q_{n+1},
\delta_{n+1})$\;
}}
\end{algorithm}

\end{document}

结果

顺便提一句,\SetVlineSkip定义为:

\newcommand{\SetVlineSkip}[1]{\skiphlne=#1}%

的更好定义\SetVlineSkip是(终止分配,支持包calc):

\renewcommand*{\SetVlineSkip}{\setlength{\skiphlne}}

相关内容