如何仅在某些情况下关闭 algorithm2e 中的“图形”?

如何仅在某些情况下关闭 algorithm2e 中的“图形”?

假设以下情况。

Algorithm2e 应该具有全局选项“figure”,即算法包含在Figures中。

但是,我们只需要在某些情况下关闭此属性(对图片进行某些操作),然后继续运行相同的全局选项。

换句话说,有必要制定一个本地命令,比如说\SetAlgoNoFig

还有类似的全局选项和局部命令,如“vlined”和\SetAlgoNoLine,分别用于垂直线等

以下是稍作修改的 MWE,取自这个讨论。不幸的是,如果我们将“figure”添加到包选项中,就会出现错误。还需要删除“ruled”选项才能获得所需的表示。

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

以下是所需的图片视图:

在此处输入图片描述

答案1

我发现这里的解决方案相当简单。不要使用algorithm带有开关的环境,figure只需在需要时使用环境即可。可能(也许鼓励),您可以将浮动figure环境隐藏在不同名称的环境内figalgorithm(例如):

在此处输入图片描述

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

答案2

这里还有另一个选项,为您提供两个命令\SetAlgoFig来切换到figure环境和\ŞetAlgoNoFig切换到algorithm环境(解释在底部):

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

结果:

在此处输入图片描述

这个想法很简单:

首先,我们存储环境的原始定义algorithm

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

现在,\SetAlgoFig简单地algorithm变成figure

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

\SetAlgoNoFig简单地返回algorithm其原始定义:

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

相关内容