如何使用 algorithm2e 在条件下断行?

如何使用 algorithm2e 在条件下断行?

我正在尝试(部分)重现图 4 中显示的算法NetFlow:信息丢失还是获胜?

我的问题是“IF”中有多行条件。我希望它们缩进,但如果同一行中没有\Indp新行,这似乎不起作用。\\

这是我尝试过的:

\begin{algorithm}[htb]
    \SetAlgoNoLine%
    \SetNoFillComment
    \SetKwIF{If}{ElseIf}{Else}{if}{:}{elif}{else:}{}%

    \If{(SYNs from both hosts and \\
            \Indp SYNs in earliest NFs of both hosts and \\
            \Indp hosts' earliest packets differ)}{
        Host with earliest NF is originator 
    }
    \If{(SYN only from one host and \\
        \Indp SYN is in connection's earliest NF)}{
    Host is originator
    }
    \If{one host uses port 20 (ftp-data)}{
    Host is originator
    }
    \If{only one host uses a well-known port}{
    Host is responder
    }
    \If{start of hosts' first packets differ}{
    Host with earliest NF is originator
    }
    Arbitrarily choose originator
    \caption{Pseudo code for determining connection server side}
    \label{algo:serverside2}
\end{algorithm}

例如,“两个主机的最早 NF 中的 SYN”这一行缩进正确,但“主机的最早数据包不同”这一行缩进不正确。

答案1

一些手动干预提供了预期的结果:

在此处输入图片描述

\documentclass{article}
\usepackage{algorithm2e}% http://ctan.org/pkg/algorithm2e
\begin{document}
\begin{algorithm}[htb]
    \SetAlgoNoLine%
    \SetNoFillComment
    \SetKwIF{If}{ElseIf}{Else}{if}{:}{elif}{else:}{}%

    \If{(SYNs from both hosts and \\
      \mbox{}\phantom{\textbf{if} \itshape(}SYNs in earliest NFs of both hosts and \\
      \mbox{}\phantom{\textbf{if} \itshape(}hosts' earliest packets differ)}{
    \Indp Host with earliest NF is originator 
    }
    \If{(SYN only from one host and \\
      \mbox{}\phantom{\textbf{if} \itshape(}SYN is in connection's earliest NF)}{
    Host is originator
    }
    \If{one host uses port 20 (ftp-data)}{
    Host is originator
    }
    \If{only one host uses a well-known port}{
    Host is responder
    }
    \If{start of hosts' first packets differ}{
    Host with earliest NF is originator
    }
    Arbitrarily choose originator
    \caption{Pseudo code for determining connection server side}
    \label{algo:serverside2}
\end{algorithm}
\end{document}

上述使用\phantom(和适当的字体更改)来复制缩进上方的线条所占据的水平空间。

但是,您也可以使用不同的环境来复制输出。我最常tabbing想到的就是这个。请参阅使用正确的语法打印程序

在此处输入图片描述

\documentclass{article}
\begin{document}
\begin{figure}
  {\ttfamily
  \begin{tabbing}
    If \=SYN\=s from both hosts and \\ \kill
       \>SYNs in earliest NFs of both hosts and \\
       \>start of hosts’ earliest packets differ: \\
       \> \>Host with earliest NF is originator \\
    If SYN only from one host and \\
       \> SYN is in connection’s earliest NF: \\
       \> \>Host is originator \\
    If one host uses port 20 (ftp-data): \\
       \> \>Host is originator \\
    If only one host uses a well-known port: \\
       \> \>Host is responder \\
    If start of hosts’ first packets differ: \\
       \> \>Host with earliest NF is originator \\
    Arbitrarily choose originator
  \end{tabbing}}
  \caption{Pseudo code for determining connection server side}
  \label{algo:serverside2}
\end{figure}
\end{document}

相关内容