Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
Welcome to TeX.sx! –  Kurt Mar 5 '13 at 16:45

1 Answer 1

up vote 2 down vote accepted

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}
share|improve this answer
    
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 6 '13 at 9:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.