1

I'm trying to write an algorithm using the package algorithm2e.

This is my code:

\usepackage[linesnumbered,ruled]{algorithm2e}
...
{\footnotesize
\IncMargin{1em}
\begin{algorithm}[H]
  \SetAlgoVlined
  \SetAlgoNlRelativeSize{-1}
  \SetNlSkip{1em}

  $ALIGNMENTS \gets NEXT$\;
  \BlankLine
  \While{$(C_{cov} > 0) \wedge (|ALIGNMENTS| > 0)$}{
    $NEXT \gets 0$\;
    \ForEach{$A \in ALIGNMENTS$}{
      $U \gets checkAlignments(\mu,A,C_{cov})$\;
      \uIf{$|U| = 1$}{
        $\mu \gets updateProfile(\mu,U)$\;
      }
      \Else{
        $NEXT = NEXT \cup \{A\}$\;
      }
    }
    \If{$|ALIGNMENTS| = |NEXT|$}{
      $C_{cov} \gets C_{cov} - 1$\;
    }
    $ALIGNMENTS \gets NEXT$\;
  }
  \BlankLine
  \KwRet{$\mu$}\;

\end{algorithm}
}  

And this is the result: enter image description here

The problem is that the line numbers are not properly aligned: for example the 9 is a lot more on the right than the 1.

So, how can I properly align them horizontally?

4

This was quite hard to debug. At first glance the misalignment seems arbitrary, but on closer examination the numbers move the farer to the right the more vertical rules are used on this line. This can be seen best when looking at the 6/8 and 7/9 pairs:

enter image description here

It turns out the amount of extra spacing is actually (the number of vertical rules on that line)x0.4pt, which is the default width for rules in LaTeX. Therefore the effect of this bug is almost not noticeable with standard font sizes and long blocks.

Digging a bit in the algortihm2e code, the vertical rules seem to be created by nesting several \hboxes and \vtops into each other, where each outer \vtop is preceded by a \vrule, which draws the vertical rule for each algorithm block.

(NB: When the package calculates the indentation of blocks, this additional spacing seems to be taken into account. However, it's missing when an \llap box is inserted at the start of the following code to push the line number back to the left by an accumulated amount of spacing. I'm not sure why there are two different dimension registers for this spacing.)

An easy fix for that bug is to "undo" the width of every vertical rule by adding a \kern-0.4pt after it. You can apply that patch quite easily using the \patchcmd from the etoolbox package. Just add the following code to your preamble:

\usepackage{etoolbox}

\makeatletter
\patchcmd\algocf@Vline{\vrule}{\vrule \kern-0.4pt}{}{}
\patchcmd\algocf@Vsline{\vrule}{\vrule \kern-0.4pt}{}{}
\makeatother

The new output gives correctly aligned line numbers:

enter image description here

1
  • 1
    Upvote you for a detailed answer. My best regards.
    – Sebastiano
    Jul 31 '19 at 11:38
1

Welcome to TeX.SE. From the documentation of algorithm2e you have:

enter image description here

\documentclass[12pt]{article}
\usepackage[linesnumbered,lined,boxed,commentsnumbered]{algorithm2e}
\begin{document}
\IncMargin{1em}
\begin{algorithm}
$ALIGNMENTS \gets NEXT$\;
  \BlankLine
  \While{$(C_{cov} > 0) \wedge (|ALIGNMENTS| > 0)$}{
    $NEXT \gets 0$\;
    \ForEach{$A \in ALIGNMENTS$}{
      $U \gets checkAlignments(\mu,A,C_{cov})$\;
      \uIf{$|U| = 1$}{
        $\mu \gets updateProfile(\mu,U)$\;
      }
      \Else{
        $NEXT = NEXT \cup \{A\}$\;
      }
    }
    \If{$|ALIGNMENTS| = |NEXT|$}{
      $C_{cov} \gets C_{cov} - 1$\;
    }
    $ALIGNMENTS \gets NEXT$\;
  }
  \BlankLine
  \KwRet{$\mu$}\;
  \DecMargin{1em}
\end{algorithm}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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