# How to switch off “figure” in algorithm2e only in certain cases?

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

\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
\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:

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):

\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}

• you are right. This is the shortest way to get a result. However it has one problem. If there are a lot of algorithms you should wrap them all in Figs. Let wait for another answers. If there are no good general solutions, I'll accept your variant soon. – Vladimir Parkhomenko Nov 13 '14 at 13:38

Here's another option giving you two commands \SetAlgoFig to switch to a figure environment and \ŞetAlgoNoFig to switch to the algorithm environment (explanations at the bottom):

\documentclass{article}
\usepackage{lipsum}
\usepackage[ruled,linesnumbered,vlined,scleft]{algorithm2e}

\newsavebox{\tempbox}

\makeatletter
\let\oldnl\nl% Store \nl in \oldnl
\makeatother

\let\oldalgorithm\algorithm
\let\oldendalgorithm\endalgorithm

\newcommand\SetAlgoFig{
\let\algorithm\figure
\let\endalgorithm\endfigure
}

\newcommand\SetAlgoNoFig{
\let\algorithm\oldalgorithm
\let\endalgorithm\oldendalgorithm
}

\begin{document}

\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}}

\SetAlgoFig % use figure

\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}

\SetAlgoNoFig % use the standard algorithm

\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}


The result:

The idea is simple:

First, we store the original definitions for the algorithm environment:

\let\oldalgorithm\algorithm
\let\oldendalgorithm\endalgorithm


Now, \SetAlgoFig simply turns algorithm into figure:

\newcommand\SetAlgoFig{
\let\algorithm\figure
\let\endalgorithm\endfigure
}


and\SetAlgoNoFig simply returns algorithm to its original definition:

\newcommand\SetAlgoNoFig{
\let\algorithm\oldalgorithm
\let\endalgorithm\oldendalgorithm
}

• No idea why but my line breaks disappear when I do this? Any idea what this happens? – Kvothe Mar 10 at 18:11