I have two questions linked to the algorithms environment that I am using in LaTeX.
Example
Let me start with an example file to illustrate what I'm looking for. Algorithm 1 in the MWE below does what I want: It spans 95% of the text width, is centered, has rules of that width, and indentation is the way I want it to be, as in the image below.
\documentclass{memoir}
%% Memoir layout setup
%\setulmarginsandblock{3cm}{3cm}{*}
%\setlrmarginsandblock{2.25cm}{2.25cm}{*}
%\checkandfixthelayout
%% packages
\usepackage{etoolbox}
\usepackage{enumitem}
\usepackage{lipsum}
%% algorithm environment
\usepackage[vlined,ruled,algo2e]{algorithm2e}
\SetAlCapHSkip{0pt}
\setlength{\algoheightrule}{1pt}
\setlength{\algotitleheightrule}{0.7pt}
\newcommand{\marginwidth}{0.025\linewidth}
\newcommand{\algoboxwidth}{0.95\linewidth}
\setlength{\algomargin}{0.0em}
\SetCustomAlgoRuledWidth{0.95\linewidth}
\makeatletter
\patchcmd{\@algocf@start}{%
\begin{lrbox}{\algocf@algobox}%
}{%
\rule{\marginwidth}{\z@}%
\begin{lrbox}{\algocf@algobox}%
\begin{minipage}{\algoboxwidth}%
}{}{}
\patchcmd{\@algocf@finish}{%
\end{lrbox}%
}{%
\end{minipage}%
\end{lrbox}%
}{}{}
% extend caption all the way to the right
\renewcommand{\algocf@makecaption@ruled}[2]{%
\global\sbox\algocf@capbox{\hskip\AlCapHSkip%
\setlength{\hsize}{\columnwidth}% restored on exit of sbox
\addtolength{\hsize}{-2\AlCapHSkip}% add equal margin to both sides
\vtop{\algocf@captiontext{#1}{#2}}}% then caption is not centered
}%
\makeatother
\begin{document}
\lipsum[1]
\begin{algorithm2e}[H]
\caption{A simple algorithm}
\SetKwInOut{Input}{Input}
\Input{Give me some input.}
\smallskip
\ForEach{outer iteration}{
\ForEach{inner iteration}{
calculate some cool stuff
}
more cool stuff
}
\Return{got it}
\end{algorithm2e}
\lipsum[1]
\begin{algorithm2e}[H]
\caption{This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption.}
\SetKwInOut{Input}{Input}
\Input{\lipsum[2]}
\smallskip
\begin{enumerate}[itemsep=0.3em, labelwidth=0.9em, leftmargin=!, topsep=0pt]
\item \lipsum[2]
\item \lipsum[2]
\end{enumerate}
\end{algorithm2e}
\lipsum[2]
\end{document}
Issues
Note that the layout setup, i.e., the three lines
\setulmarginsandblock{3cm}{3cm}{*} \setlrmarginsandblock{2.25cm}{2.25cm}{*} \checkandfixthelayout
is commented out in the above document. If that setup is used, the lines bounding the algorithms don't extend to 95% of the line width any longer. I can, of course, fiddle around with the factor in
\SetCustomAlgoRuledWidth{0.95\linewidth}
, but I guess there should be a general solution to this problem that really gives me 95% of the linewidth.Also, I would be happy to understand why the behaviour is not as expected.
The picture below shows the situation.
In Algorithm 2, where we have more text in the caption, input, and algorithm body, there are two points I don't like:
Text width: The caption is corrected manually to take the full algorithm width, how can I do this for the input, as well? The algorithm body seems to have the correct width.
Text alignment: Is there a way to justify both the input and the algorithm body text? I tried the suggestion at algorithm2e fully justified input / output blocks for the Input, but for some reason that I do not understand, this introduces extra spacing above the input in my case.
Again, the picture below shows how things look like.