algorithm2e - 覆盖默认值

algorithm2e - 覆盖默认值

我正在使用algorithm2e具有以下设置的包:

\usepackage[ruled,vlined,linesnumbered,noresetcount]{algorithm2e}

但对于同一文档中的某些算法子集,我想使用此设置:

\usepackage[boxed]{algorithm2e}

有没有办法只覆盖子集的默认设置?

我发现了类似的问题: algorithm2e 带有“ruled”,但下方有标题? 这里newenvironment用于覆盖选项的默认设置。但我想覆盖选项本身。

我希望有一种方法可以多次加载一个包,并且每次实例都可以使用不同的选项。

答案1

您必须手动调整设置:

在此处输入图片描述

\documentclass{article}

\usepackage[ruled,vlined,linesnumbered,noresetcount]{algorithm2e}

\makeatletter
\newcommand{\AlgoResetCount}{\renewcommand{\@ResetCounterIfNeeded}{\setcounter{AlgoLine}{0}}}
\newcommand{\AlgoNoResetCount}{\renewcommand{\@ResetCounterIfNeeded}{}}
\newcounter{AlgoSavedLineCount}
\newcommand{\AlgoSaveLineCount}{\setcounter{AlgoSavedLineCount}{\value{AlgoLine}}}
\newcommand{\AlgoRestoreLineCount}{\setcounter{AlgoLine}{\value{AlgoSavedLineCount}}}
\makeatother

\begin{document}

\begin{algorithm}[H]
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
\end{algorithm}

\RestyleAlgo{boxed}% Change from the 'ruled' style to 'boxed'
\SetAlgoNoLine% Removes 'vlined' option (somewhat opposite of \SetAlgoVlined)
\LinesNumberedHidden% Removes 'linesnumbered' option (opposite of \LinesNumbered)
\AlgoSaveLineCount% Stores the algorithm line number (similar to 'resetcount' in the package load option)

\begin{algorithm}[H]
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
\end{algorithm}

\RestyleAlgo{ruled}% Change from the 'boxed' style to 'ruled'
\SetAlgoVlined% Similar to 'vlined' in the package load option
\LinesNumbered% Similar to 'linesnumbered' in the package load option
\AlgoRestoreLineCount% Restores the algorithm line number (similar to 'noresetcount' in the package load option)

\begin{algorithm}[H]
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
\end{algorithm}

\end{document}

答案2

无需太多手动调整!您只需添加 \RestyleAlgo{options} 就在之前您想要覆盖的算法定义。

请注意,选项可以是以下之一:boxedboxruled和。ruledalgoruled

前任:

\usepachage[]{algorithm2e}在序言中 定义之后:

     \RestyleAlgo{ruled}
     \begin{algorithm}
      ....
      \end{algorithm}

就是这样!

相关内容