[Tex/LaTex] How to switch off “figure” in algorithm2e only in certain cases

adjustboxalgorithm2ealgorithmsfloats

Suppose the following situation.

Algorithm2e should have the global option "figure", i.e. an algorithm is included in Figures.

However we need to switch off this property only in certain cases (to make some operations with pictures) and then continue running the same global option.

In other words, it's necessary to develop a local command, say \SetAlgoNoFig.

There are similar global options and local commands like "vlined" and \SetAlgoNoLine, respectively, for vertical lines e.c.t.

Here is a slightly modified MWE taken from this and this discussions. Unforturantly, if we add "figure" to the package options, there will be an error. It's necessary also to remove "ruled" option to have a desired representation.

\documentclass{article}

\usepackage{lipsum}
\usepackage[ruled,linesnumbered,vlined,scleft]{algorithm2e} %"figure" should be added to options, and "ruled" should be removed

\usepackage{adjustbox}
\newsavebox{\tempbox}

%a local command to switch off the global option ``linesnumbered''
% from https://tex.stackexchange.com/questions/153646/algorithm2e-disabling-line-numbers-for-specific-lines
\makeatletter
\let\oldnl\nl% Store \nl in \oldnl
\newcommand{\nonl}{\renewcommand{\nl}{\let\nl\oldnl}}% Remove line number for one line
\makeatother

\begin{document}

%% Some Figures and algorithms in Fig. environment above %%
%% \SetAlgoNoFig  start%%

\savebox{\tempbox}{% create image
\begin{minipage}[c]{0.45\textwidth}%
\begin{algorithm*}[H]
        \SetKwFunction{funone}{MyFunction}
        \SetKwFunction{funtwo}{OtherFun}
        \SetKwProg{main}{Algorithm}{}{}
        \main{\funone{b}}{
                \KwData{MyData b}
         \While{this is true}{
                 Do X\;
                 Do X\;
            }
         Do X\;
        }
        \SetKwProg{foo}{Procedure}{}{}
        \foo{\funtwo{h, s, d}}{
                \KwData{MyData h, s, d}
         Do X\;
         Do X\;
         \If{Is his true?}{
                 \eIf{Is this true?}{
                         Do X\;
                        }{
                         Do X\;
                            }
                }
         Do X\;
         Do X\;
         Do X\;

        }
\end{algorithm*}%
\end{minipage}}

%% \SetAlgoNoFig  end%%
%% Some Figures and algorithms in Fig. environment below %%

\begin{algorithm}
\nonl %switch off line numbers
\clipbox{0pt {\depth} 0pt {\baselineskip}}{\usebox{\tempbox}}\hfill
\raisebox{\depth}{\clipbox{0pt 1ex 0pt {\height}}{\usebox{\tempbox}}}
\caption{My lovely procedure}
\end{algorithm}

\end{document}

Here is a desired view of the picture:

enter image description here

Best Answer

I find the solution here fairly straight-forward. Instead of using an algorithm environment with switches, just use a figure environment whenever you need it. Potentially (perhaps encouraged), you could hide the floating figure environment inside a differently-named one figalgorithm (say):

enter image description here

\documentclass{article}

\usepackage[linesnumbered,vlined,scleft]{algorithm2e}
\newenvironment{figalgorithm}[1][htbp]
  {\begin{figure}[#1]}
  {\end{figure}}

\newsavebox{\tempbox}

\begin{document}

\listoffigures

\savebox{\tempbox}{% create image
  \begin{minipage}{.5\linewidth}
    \begin{algorithm*}[H]
      \SetKwFunction{funone}{MyFunction}
      \SetKwFunction{funtwo}{OtherFun}
      \SetKwProg{main}{Algorithm}{}{}
      \main{\funone{b}}{
        \KwData{MyData b}
        \While{this is true}{
          Do X\;
          Do X\;
        }
        Do X\;
      }
    \end{algorithm*}%
  \end{minipage}}

\begin{figalgorithm}
  \centering\usebox{\tempbox}
  \caption{My lovely algorithm}
\end{figalgorithm}

\end{document}