[Tex/LaTex] Remove top rule of `ruled` algorithm2e

algorithm2erules

I'd like to use similar styles for pseudocode/algorithms and listings using the algorithm2e package. Therefore I need a line above and below the algorithm but not above the caption. Haven't found any solution so far.

Example

Adjusted several configurations like the style of the line numbers and the caption, but yeah.. cannot getting rid off the line above:

\setlength{\algoheightrule}{0.4pt}
\renewcommand\AlCapFnt{\normalfont}
\SetAlgoCaptionLayout{centerline}
\SetNlSty{ttfamily}{}{}
\SetAlgoNlRelativeSize{-3}

Thanks a lot!


MWE:

\documentclass[a4paper, 11pt]{book}

\usepackage[ruled]{algorithm2e}

\begin{document} 
  \begin{algorithm}
    \KwData{this text}
    \KwResult{how to write algorithm with \LaTeX}
    \caption{How to write algorithms}
  \end{algorithm}
\end{document}

Best Answer

You can set the algorithms using the plainruled style - two single rules, one above, one below the algorithm - and adjust the way the algorithm caption is set; default is under, so we change that to above (not top):

enter image description here

\documentclass{article}

\usepackage[
  plainruled,
  linesnumbered,
  noline
]{algorithm2e}

\setlength{\algoheightrule}{0.4pt}
\renewcommand\AlCapFnt{\normalfont}
\SetAlgoCaptionLayout{centerline}
\DontPrintSemicolon

\makeatletter
% Add adjustments to plainruled format
\def\@algocf@capt@plainruled{above}
\renewcommand{\algocf@caption@plainruled}{%
  \vskip\AlCapSkip%
  \box\algocf@capbox%
  \vskip 5\algoheightrule}%
\makeatother

\begin{document}

\begin{algorithm}
  \KwData{This text}
  \KwResult{How to write algorithm with \LaTeX}
  \caption{How to write algorithms}
\end{algorithm}

\end{document}

A minor adjustment is included to add a space between the caption and the algorithm's top rule.