4

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

2 Answers 2

4
+50

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}
1
  • 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. Nov 13, 2014 at 13:38
4

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} 
\usepackage{adjustbox}

\newsavebox{\tempbox}

\makeatletter
\let\oldnl\nl% Store \nl in \oldnl
\newcommand{\nonl}{\renewcommand{\nl}{\let\nl\oldnl}}% Remove line number for one line
\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:

enter image description here

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
}
2
  • thank you. The approach is very close to a desired result. Actually we need reverse problem to be solved. In algorithm2e.sty I found that option figure make \renewcommand{\algocf@list}{lof} and \renewcommand{\algocf@float}{figure}. And later there is a sentence that "loa (is set) by default, lof (is set) if figure option used". So, probably, we need to deal with lof & loa... Nov 14, 2014 at 21:16
  • No idea why but my line breaks disappear when I do this? Any idea what this happens?
    – Kvothe
    Mar 10, 2021 at 18:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .