1

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.

enter image description here

\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

  1. 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.

    enter image description here

  2. 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.

    enter image description here

1
  • I am still looking for a solution. Any ideas/suggestions (also partial ones are appreciated!) anyone?
    – user279611
    Nov 16, 2021 at 16:10

1 Answer 1

0

Some analysis for you: using the enumerate environment inside the algorithm2e (floating) environment seems to destroy normal alignment, see e.g. in this wikibook.

Compare:

  • normal enumerate environment
  • enumerate environment inside an algorithm2e environment

2 enumerate environments

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

\begin{document}
    \begin{enumerate}
     \item \blindtext
    \end{enumerate}

    \begin{algorithm2e}[H]
        \caption{some enum in algo}
        \begin{enumerate}
          \item \blindtext
        \end{enumerate}
    \end{algorithm2e}
\end{document}

Which increases my suspicion of using enumerations inside an algorithm: what shall that mean? What kind of action is the algorithm performing there?

Suggestion: use some crisp action-statements INSIDE the algo (like plotting(thingies);), AND explain in more detail later (plotting(thingies) is a complicated process, which involves this and that ...) , if it's that what you want.

P.S.: If you insist on enum inside, try a \parbox, e.g. like this, where I don't know if it may have side-effects:

enum with parbox

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

\begin{document}
    \begin{enumerate}
     \item \blindtext
    \end{enumerate}

    \begin{algorithm2e}[H]
        \caption{some enum in algo}
        \begin{enumerate}
          \item \blindtext
          \item \parbox[t]{8cm}{\blindtext}
        \end{enumerate}
    \end{algorithm2e}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.