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.

When using line numbers in the algorithm2e package, the line numbers are placed left of the text block, not directly below the text. See page 5 of the algorithm2e manual.

Is it possible to place them below the text ?

Update: Line numbering now:

   Input ...
   Output ...
1: Code ...

Line numbering I want:

Input ...
Output ...
1: Code ...
share|improve this question
add comment

1 Answer

The simplest way to do this is to use the formatting commands \Indm and \Indp to decrease and increase the indentation. Used around the input/output commands makes these lines flushleft, just like the subsequent line numbers. The example below shows this in action in a cut-down version of the second example from the manual.

Sample output

\documentclass{article}

\usepackage[linesnumbered]{algorithm2e}

\begin{document}

\IncMargin{1em}
\begin{algorithm}
  \SetKwData{Left}{left}
  \SetKwData{Up}{up}
  \SetKwFunction{FindCompress}{FindCompress}
  \SetKwInOut{Input}{input}
  \SetKwInOut{Output}{output}

\Indm  
  \Input{A bitmap $Im$ of size $w\times l$}
  \Output{A partition of the bitmap}
\Indp
  \BlankLine
  \For{$i\leftarrow 2$ \KwTo $l$}{ 
    \Left$\leftarrow$ \FindCompress{$Im[i,j-1]$}\;
    \Up$\leftarrow$ \FindCompress{$Im[i-1,]$}\; }
    \lForEach{element $e$ of the line $i$}{\FindCompress{p}}
\end{algorithm}
\DecMargin{1em}

\end{document}

For more general indentation changes there is \Indentp, which is defined in the style class, but not mentioned in the manual. \Indm is equivalent to \Indentp{-1em}, \Indp is \Indentp{1em}. There is also \Indmm and \Indpp which adjust the indentation by 0.5em. You need an extra 0.5em for each extra digit of line number. In particular, if you have two digit line numbers you can use \Indm\Indmm as follows:

Sample two digit output

\documentclass{article}

\usepackage[linesnumbered]{algorithm2e}

\begin{document}

\IncMargin{1.5em}
\begin{algorithm}
  \SetKwData{Left}{left}
  \SetKwData{Up}{up}
  \SetKwFunction{FindCompress}{FindCompress}
  \SetKwInOut{Input}{input}
  \SetKwInOut{Output}{output}

\Indm\Indmm
  \Input{A bitmap $Im$ of size $w\times l$}
  \Output{A partition of the bitmap}
\Indp\Indpp
  \BlankLine
  \For{$i\leftarrow 2$ \KwTo $l$}{ 
    \Left$\leftarrow$ \FindCompress{$Im[i,j-1]$}\;
    \Up$\leftarrow$ \FindCompress{$Im[i-1,]$}\; }
    \lForEach{element $e$ of the line $i$}{\FindCompress{p}}
  \For{$i\leftarrow 2$ \KwTo $l$}{ 
    \Left$\leftarrow$ \FindCompress{$Im[i,j-1]$}\;
    \Up$\leftarrow$ \FindCompress{$Im[i-1,]$}\; }
    \lForEach{element $e$ of the line $i$}{\FindCompress{p}}
  \For{$i\leftarrow 2$ \KwTo $l$}{ 
    \Left$\leftarrow$ \FindCompress{$Im[i,j-1]$}\;
    \Up$\leftarrow$ \FindCompress{$Im[i-1,]$}\; }
    \lForEach{element $e$ of the line $i$}{\FindCompress{p}}
  \For{$i\leftarrow 2$ \KwTo $l$}{ 
    \Left$\leftarrow$ \FindCompress{$Im[i,j-1]$}\;
    \Up$\leftarrow$ \FindCompress{$Im[i-1,]$}\; }
    \lForEach{element $e$ of the line $i$}{\FindCompress{p}}
    \BlankLine
    \Indm\Indmm
  \Input{A bitmap $Im$ of size $w\times l$}
  \Output{A partition of the bitmap}
\Indp\Indpp
\end{algorithm}
\DecMargin{1.5em}

\end{document}

There is no internal variable storing the width of the line numbers. The package uses \llap to them in a box of width zero, with the numbers plus their space from the algorithm sticking out to the right of this box. As the examples in the documentation indicate, it is up to the user to adjust the margins to suit the given algorithm.

share|improve this answer
add comment

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.