0

I'm using a solution suggested by C. Fiorio in Want Knuth style line numbering in algorithm2e algorithms. The suggestion was to use the algonl option for algorithm2e to achieve Knuth style algorithm line numbering; i.e. Algorithm #3 should have lines numbered 3.1, 3.2, 3.3, etc. However it seems the line number overflow the bounding box if I use the boxed option.

Can someone suggest a good way to fix this so that the algorithm box does not clash with the line numbering?

Here is what I see rendered. enter image description here

Here is a minimum working example.

\documentclass{article}

\usepackage[noend,boxed,linesnumbered,algonl]{algorithm2e}
\SetKwProg{Fn}{Function}{}{end}

\begin{document}

\begin{algorithm}[H]\label{algo.find.augmenting.path}
  \caption{Implementation of function to find an augmenting path if one exists.}
  \DontPrintSemicolon
  \Fn{find-augmenting-path-or-none$(adj,E,M)$}{
    \SetKwInOut{Input}{Input}\SetKwInOut{Output}{Output}
    \Input{$adj$ adjacency list of simple graph}
    \Input{$E$ set of edges}
    \Input{$M$ a matching}
    \BlankLine
    $free \gets $ generate-free-vertices() \;
    \If{$|free| < 2$}{
      \Return None \;
    }
    \tcp*[l]{Find set of length=2 paths starting at a free vertex}
    $paths \gets \{[u,v] \mid u\in free, \{u,v\} \in E  \}$\label{algo.line.paths.1b}\;
    $k \gets 1$  \tcp*{index of 2nd element of 0-index-based array}
    \While{$paths \neq \emptyset$}{
      \If{odd$(k)$}{
        \For{$p \in paths$}{
          \If{ $p_k \in free$ }{
            \Return p\;
          }
        }
      }
      $paths \gets$ extend-alternating-path$(adj,M,k,free,paths)$ \;
      $k \gets k+1$ \;
    }
    \Return None
  }
\end{algorithm}

\end{document}
1

Setting a larger \algomargin solves the problem. You can

  • directly set \setlength\algomargin{3em} or
  • use \IncMargin{<length>} to add <length> to \algomargin.

See the documentation of algorithm2e, sec. 9.6.

Alternatively, you can

Full example

\documentclass{article}

\usepackage[noend,boxed,linesnumbered,algonl]{algorithm2e}
\SetKwProg{Fn}{Function}{}{end}

\setlength\algomargin{3em}

\begin{document}

\begin{algorithm}[H]\label{algo.find.augmenting.path}
  \caption{Implementation of function to find an augmenting path if one exists.}
  \DontPrintSemicolon
  \Fn{find-augmenting-path-or-none$(adj,E,M)$}{
    \SetKwInOut{Input}{Input}\SetKwInOut{Output}{Output}
    \Input{$adj$ adjacency list of simple graph}
    \Input{$E$ set of edges}
    \Input{$M$ a matching}
    \BlankLine
    $free \gets $ generate-free-vertices() \;
    \If{$|free| < 2$}{
      \Return None \;
    }
    \tcp*[l]{Find set of length=2 paths starting at a free vertex}
    $paths \gets \{[u,v] \mid u\in free, \{u,v\} \in E  \}$\label{algo.line.paths.1b}\;
    $k \gets 1$  \tcp*{index of 2nd element of 0-index-based array}
    \While{$paths \neq \emptyset$}{
      \If{odd$(k)$}{
        \For{$p \in paths$}{
          \If{ $p_k \in free$ }{
            \Return p\;
          }
        }
      }
      $paths \gets$ extend-alternating-path$(adj,M,k,free,paths)$ \;
      $k \gets k+1$ \;
    }
    \Return None
  }
\end{algorithm}

\end{document}

output showing larger \algomargin

3
  • Thanks muzimuzhi Z, that works well. – Jim Newton Mar 25 '20 at 12:37
  • @JimNewton If you like the proposed answer and it was helpful, please consider upvoting (by clicking on the arrows next to the score) and/or marking it as the accepted answer (by clicking on the checkmark ✓). – BambOo Mar 25 '20 at 17:51
  • 1
    @BambOo, thanks for the suggestion. I had upvoted it, but forgot to click the checkmark. Thanks for the kind reminder. – Jim Newton Mar 26 '20 at 7:16

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.