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}
Best Answer
Here's one possible solution:
The
algorithm2e
package stores the algorithm inside thelrbox
\algocf@makethealgo
and then typesets it; I defined a new environmentWalgo
that uses aminipage
inside thislrbox
; the width of theminipage
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).