# algorithm2e and \algomargin with negative value

How can I use a negative length in \algomargin using algorithm2e? I get the unpleasant effect that the vertical lines and the caption are not moved accordingly, see for example:

Note that this image is cut in the left incorrectly by the standalone document class (use article class instead).

The text "Algorithm 0.2" extends into the left margin (which is wanted) but the caption and the vertical lines are not. I am happy with any solution that lets me extend the algorithm environment slightly in the left and right margins, not necessarily using \algomargin.

\documentclass[varwidth]{standalone}
%\documentclass{article}

\usepackage[ruled,vlined,algosection]{algorithm2e}

\begin{document}

\begin{algorithm} normal computation \caption{normal algorithm} \end{algorithm}

\setlength{\algomargin}{-2em}
\begin{algorithm} long computation \caption{long algorithm} \end{algorithm}

\setlength{\algomargin}{2em}
\begin{algorithm} short computation \caption{short algorithm}
\end{algorithm}

\end{document}

-

Here's one possible solution:

\documentclass{article}
\usepackage[ruled,vlined,algosection]{algorithm2e}

\newlength\marincrease

\makeatletter
\newenvironment{Walgo}[2][htbp]
{\renewcommand{\@algocf@start}{%
\setlength\marincrease{#2}
\@algoskip%
\begin{lrbox}{\algocf@algobox}%
\begin{minipage}{\dimexpr\textwidth+2\marincrease\relax}
\setlength{\algowidth}{\hsize}%
\vbox\bgroup% save all the algo in a box
\hbox to\algowidth\bgroup\hbox to \algomargin{\hfill}\vtop\bgroup%
\ifthenelse{\boolean{algocf@slide}}{\parskip 0.5ex\color{black}}{}%
% initialization
\let\@mathsemicolon=\;\def\;{\ifmmode\@mathsemicolon\else\@endalgoln\fi}%
\raggedright\AlFnt{}%
\ifthenelse{\boolean{algocf@slide}}{\IncMargin{\skipalgocfslide}}{}%
\@algoinsideskip%
%   \let\@emathdisplay=\]\def\]{\algocf@endline\@emathdisplay\nl}%
}%
\renewcommand{\@algocf@finish}{%
\@algoinsideskip%
\egroup%end of vtop which contain all the text
\hfill\egroup%end of hbox wich contains [margin][vtop]
\ifthenelse{\boolean{algocf@slide}}{\DecMargin{\skipalgocfslide}}{}%
%
\egroup%end of main vbox
\end{minipage}
\end{lrbox}%
\makebox[\linewidth][c]{\algocf@makethealgo}% print the algo
\@algoskip%
% restore dimension and macros
\setlength{\hsize}{\algowidth}%
\lineskip\normallineskip\setlength{\skiptotal}{\@defaultskiptotal}%
\let\;=\@mathsemicolon%
\let\]=\@emathdisplay%
}%
\begin{algorithm}[#1]}
{\end{algorithm}}
\makeatother

\begin{document}
\section{Test Section}

\begin{Walgo}[ht]{20pt}
\While{not at end of this document}
{
\eIf{understand}
{current section becomes this one\;}
{go back to the beginning of current section and add osme more text to see the change in the margins\;}
}
\caption{How to write algorithms and some more text to see the effect of the change in the margins}
\end{Walgo}

\begin{algorithm}[ht]
\While{not at end of this document}
{
\eIf{understand}
{current section becomes this one\;}
{go back to the beginning of current section and add osme more text to see the change in the margins\;}
}
\caption{How to write algorithms and some more text to see the effect of the change in the margins}
\end{algorithm}

\begin{Walgo}[!ht]{2cm}
\While{not at end of this document}
{
\eIf{understand}
{current section becomes this one\;}
{go back to the beginning of current section and add osme more text to see the change in the margins\;}
}
\caption{How to write algorithms and some more text to see the effect of the change in the margins}
\end{Walgo}

\end{document}


The algorithm2e package stores the algorithm inside the lrbox \algocf@makethealgo and then typesets it; I defined a new environment Walgo that uses a minipage inside this lrbox; the width of the minipage is \textwidth+2\marginincrease; then, the \algocf@makethealgo is placed inside a centered \makebox to typeset it.

The Walgo environment has one optional argument (the placement specifier) and a mandatory one (to give the \marginincrease lenght used to increase the margins).

-
Thanks, this works like a charm. –  H. Brandsmeier Mar 26 '13 at 7:44