[Tex/LaTex] algorithm2e – override defaults

algorithm2epackage-options

I'm using using algorithm2e package with the following settings:

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

But for some subset of algorithms in the same document, I want to use this setting:

\usepackage[boxed]{algorithm2e}

Is there a way to override the default setting just for a subset?

I found a similar question:
algorithm2e with 'ruled' but with caption underneath?
Here newenvironment is used to override the default setting of an option. But I want to override the option itself.

I wish there was a way to load a package several times with different options for each instance.

Best Answer

You have to adjust the settings manually:

enter image description here

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