The following minimal example defines
\nosemic
: remove printing of the end-of-line (EOL) character (typically ;
);
\dosemic
: reinstate printing of the EOL character;
\pushline
: indents line by 1em
(the typical indent); provided by algorithm2e
as \Indp
;
\popline
: remove indent of \pushline
(undent?); provided by algorithm2e
as \Indm
\documentclass{article}
\usepackage{algorithm2e}% http://ctan.org/pkg/algorithm2e
\makeatletter
\newcommand{\nosemic}{\renewcommand{\@endalgocfline}{\relax}}% Drop semi-colon ;
\newcommand{\dosemic}{\renewcommand{\@endalgocfline}{\algocf@endline}}% Reinstate semi-colon ;
\newcommand{\pushline}{\Indp}% Indent
\newcommand{\popline}{\Indm\dosemic}% Undent
\makeatother
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
\nosemic this is a very long statement that has to be\;
\pushline\dosemic wrapped over two lines\;
\popline current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}
\end{document}
Since I am unfamiliar with your algorithm2e
setup, I'm not sure how you would like to combine the four commands. As such, I've kept them separate for maximum (yet manual) usage.
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.
\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:
\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.
Best Answer
Somehow
algorithm2e
has\KwTo
but not\KwBy
. It is easy to define it, however: