[Tex/LaTex] new caption type in captionof

algorithm2ecaptions

\captionof command from caption package allows caption for various floats. However we cannot use it to typeset an algorithm caption like: \captionof{Algorithm}{<Caption text>}. I tried the following without success.

\setcaptiontype{Algorithm}
\captionsetup{options=algorithm}
\captionof{Algorithm}{My caption}

So my question is, can we define a new type to be used in \captionof command? If so how?

Best Answer

The type is different from what is typically used in the naming. In that regard, the algorithm environment typically use Algorithm as its name. algorithm2e is slightly different though; it uses algocf as the type, so you need to use

\captionof{algocf}{<caption of algorithm>}

Make sure the \captionof macro is used inside an environment (or group).

enter image description here

\documentclass{article}
\usepackage{algorithm2e,caption}
\begin{document}

\begin{algorithm}[H]
  \caption{My algorithm}
\end{algorithm}

\noindent
\begin{minipage}{\linewidth}
  \captionof{algocf}{My algorithm}
\end{minipage}

\end{document}