2

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}
2
  • Could you post a complete, yet minimal, code? – Bernard Mar 6 '18 at 17:18
  • @Bernard added a mwe for the algorithm stuff. – fheck Mar 6 '18 at 17:25
3

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.

2
  • works like a charm except the indentation. Do you know a way to left align the text at the really left side? Like in the listing above. So that the line numbering are outside the box. – fheck Mar 6 '18 at 18:11
  • 3
    Done by \setlength{\algomargin}{-0pt} – fheck Mar 6 '18 at 18:33

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.