3

I am trying to (partially) reproduce the algorithm displayed in Figure 4 from NetFlow: Information Loss or Win?

My problem is with the multi-line conditions present in the "IFs". I want them to be indented, but \Indp does not seem to take effect if a new line \\ is not present in the same line.

This is what I tried:

\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}

For instance, the line "SYNs in earliest NFs of both hosts and" is indented correctly, but "hosts' earliest packets differ" is not.

David Carlisle
  • 757,742
Rafael Barbosa
  • 245
  • 3
  • 7

1 Answers1

4

Some manual intervention provides the desired result:

enter image description here

\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}

The above uses \phantom (and appropriate font changes) to duplicate the horizontal space occupied by the lines above the indentation.

However, you could also just use a different environment for duplicating the output. Most certainly tabbing comes to mind. See Print programs with its proper syntax:

enter image description here

\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}
Moriambar
  • 11,466
Werner
  • 603,163
  • What \mbox does in the first solution? I understand it avoids a linebreak, but why? 'tabbing' looks a simple solution. I will look into that. – Rafael Barbosa Mar 06 '13 at 09:57