0

I have a document with a few tables that have their captions formatted like this

enter image description here

which I achieved using

\usepackage[format=plain, justification=raggedright,singlelinecheck=true]{caption}
\captionsetup[table]{labelsep=newline, textfont=it, singlelinecheck=false, margin=1em}

and I would like to use the same layout for algorithms. I did find out from the package-readme and this page that I need

\usepackage[plainruled]{algorithm2e}

to remove the top rule above the caption and get this:

enter image description here

Next, I understand I need to define my own macro style to be able to use

\SetAlgoCaptionLayout{style}

but I am confused how to create the macro and how to left-align, use normalfont in footnotesize for the algorithm heading, insert the line break, and italicize the caption text.

MWE:

\documentclass{article}
\usepackage[plainruled]{algorithm2e}
\begin{document}

\begin{algorithm}
{\footnotesize
\caption{An example}
some text\;
}
\end{algorithm}
\end{document}
6
  • Hello, could you post a complete compilable example so we can help you on this ?
    – BambOo
    Mar 24, 2020 at 16:44
  • of course — apologies, I had forgotten but added one now!
    – bumblebee
    Mar 24, 2020 at 18:57
  • Maybe this ? \documentclass{article} \usepackage[algoruled]{algorithm2e} \renewcommand{\AlCapNameFnt}{\normalfont\itshape} \renewcommand{\AlCapFnt}{\normalfont} \SetAlgoCaptionSeparator{\\} \begin{document} \begin{algorithm} {\footnotesize \caption{An example} some text\; } \end{algorithm} \end{document}
    – BambOo
    Mar 24, 2020 at 19:19
  • Unfortunately not with plainrule, where \SetAlgoCaptionSeparator{\\} does not work and the caption is below the body and centered. I use plainruled to get rid of the top rule above the caption. Is there perhaps a way of getting this done in algoruled? That would solve things!
    – bumblebee
    Mar 24, 2020 at 20:06
  • @bumblebee: You want the algorithm caption to resemble the table though, right? plainruled puts the caption at the bottom, which is different from the visual of putting the (table) caption at the top.
    – Werner
    Mar 24, 2020 at 20:08

2 Answers 2

2

Got it. You can redefine the function defining the top rule. It is not perfect, but works.

\documentclass{article}
\usepackage[algoruled]{algorithm2e}

% Removes the top rule above caption see l.2531 of algorithm2e.sty
\makeatletter
\def\@algocf@pre@algoruled{}%
\makeatother
% Changes the caption font to italic
\renewcommand{\AlCapNameFnt}{\footnotesize\itshape}
% Removes bold font Algorithm
\renewcommand{\AlCapFnt}{\footnotesize\normalfont}
% Replaces ":" with a linebreak after Algorithm n°xxx
\SetAlgoCaptionSeparator{\\}
\begin{document}
\begin{algorithm}
{\footnotesize
\caption{An example}
some text\;
}
\end{algorithm}
\end{document}

enter image description here

2
  • Fantastic! I will edit your code to insert \footnotesize into the font setups and post new output so it matches the OP. Thanks!
    – bumblebee
    Mar 24, 2020 at 21:10
  • @bumblebee you're welcome
    – BambOo
    Mar 24, 2020 at 21:15
2

The following also adds the \footnotesize font size to your algorithm environment:

enter image description here

\documentclass{article}

\usepackage{fourier}
\usepackage[ruled]{algorithm2e}

\SetAlCapFnt{\normalfont\footnotesize}% Set caption font
\SetAlgoCaptionSeparator{\par\nobreak}% Set caption separator
\SetAlCapNameFnt{\unskip\itshape\footnotesize}% Set caption name font
\makeatletter
\def\@algocf@pre@ruled{}% Remove rule above caption
\let\old@algocf@start\@algocf@start
\renewcommand{\@algocf@start}{\old@algocf@start\footnotesize}
\makeatother

\begin{document}

\begin{algorithm}
  \caption{An example}
  some text\;
\end{algorithm}

\end{document}
5
  • Thanks! I'm assuming the \footnotesize in the \SetAlCapFnt{} and the SetAlCapNameFnt{} are redundant then?
    – bumblebee
    Mar 25, 2020 at 8:05
  • @bumblebee: No, since the default ads \normalfont if I recall, so you have to set it explicitly.
    – Werner
    Mar 25, 2020 at 15:37
  • I am afraid I don’t see the difference between this and the previous answer then.
    – bumblebee
    Mar 25, 2020 at 15:39
  • @bumblebee: The difference is you don't have to use \footnotesize explicitly with every algorithm. It seemed like you wanted everything (the algorithm caption and body) in \footnotesize. Am I wrong?
    – Werner
    Mar 25, 2020 at 15:43
  • Now I understand, thanks very much!
    – bumblebee
    Mar 25, 2020 at 15:44

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.