7

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.

  • So the boxed algorithm should not have vlined, nor linesnumbered, nor noresetcount? – Werner Nov 23 '15 at 21:40
  • yes. I was reading the documentation on algorithm2e. \RestyleAlgo{boxed} overrides ruled, but doesn't seem to override other things. – arunmoezhi Nov 23 '15 at 21:43
7

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}
| improve this answer | |
  • What would I have to do to remove the option "figure" for a certain algo: \usepackage[figure]{algorithm2e} Is that also possible? ("figure" lets the algo behave like a figure.) – lukas.coenig Jul 19 '16 at 16:18
  • ...or the other way around - include this option for certain algos, but not as a global option. – lukas.coenig Jul 19 '16 at 16:20
3

Not much manual adjustment is needed! You only add \RestyleAlgo{options} right before the algorithm definition you want to be overridden.

Note that options can be one of the followings: boxed, boxruled, ruled and algoruled.

Ex:

After defining \usepachage[]{algorithm2e} in preamble:

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

That's it!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.