Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I use the algorithm2e package to typeset algorithms. Now I would like to put a tcolorbox around an algorithm. My first approach looked like this:

\begin{tcolorbox}
    \begin{algorithm}
        $a \leftarrow 1$
        \caption{My Algorithm}
    \end{algorithm}
\end{tcolorbox}

This yields the following error: LaTeX Error: Not in outer par mode. My understanding is that this happens because both the tcolorbox and the algorithm are floating environments that cannot be nested.

There is already a similar question: How to put \algorithm and \algorithmic environments inside a box? The accepted answer there does not work for me, because it simply describes how to put an arbitrary box around the algorithm. I also tried to use \RestyleAlgo{tcolorbox}, but to no avail. I am looking for a method that specifically uses a tcolorbox.

As a final remark, I do not need to typeset the algorithms particularly with algorithm2e, any other algorithm package will do as well.

share|improve this question
    
To complement both answers: a tcolorbox is not floating by default, but you can add the float option key to make it floating. Second, an alternative to the caption could be using a title for the tcolorbox (depends what the result should look like). –  Thomas F. Sturm Jul 20 at 13:50

2 Answers 2

up vote 3 down vote accepted

The error "Not in outer par mode" is because the algorithm environment wants to float, yet it's contained inside a non-floating tcolorbox. The only way to make algorithm2e not floats its algorithms is to use the [H] float specifier:

enter image description here

\documentclass{article}
\usepackage[paper size={15cm,10cm}]{geometry}
\usepackage[most]{tcolorbox}
\usepackage{algorithm2e}

\begin{document}

\begin{tcolorbox}
  \begin{algorithm}[H]
    $a \leftarrow 1$
    \caption{My Algorithm}
  \end{algorithm}
\end{tcolorbox}
\end{document}
share|improve this answer

This is an alternative. The LaTeX Wikibook section on algorithms has the following statement.

The algorithmicx package provides a number of popular constructs for algorithm designs. Put \usepackage{algpseudocode} in the preamble to use the algorithmic environment to write algorithm pseudocode (\begin{algorithmic}...\end{algorithmic}). You might want to use the algorithm environment (\usepackage{algorithm}) to wrap your algorithmic code in an algorithm environment (\begin{algorithm}...\end{algorithm}) to produce a floating environment with numbered algorithms.

With that said, without algorithm environment means non-floating. Thus, it can be put inside tcolorbox environment, consequently, caption package is required for the \caption for the algorithm. Remember to add the package algpseudocode statement to your document preamble.

enter image description here

The following is an example of code that yields the result shown above.

Code

\documentclass{article}
\usepackage[paper size={15cm,10cm}]{geometry}
\usepackage[most]{tcolorbox}
\usepackage{algpseudocode}
\usepackage{caption}

\DeclareCaptionType{mytype}[Algorithm][List of MyType]
\newenvironment{myenv}{}{}

\begin{document}
\listofmytypes

\begin{tcolorbox}
    \centering
\begin{algorithmic}
\If {$i\geq maxval$}
    \State $i\gets 0$
\Else
    \If {$i+k\leq maxval$}
        \State $i\gets i+k$
    \EndIf
\EndIf
\end{algorithmic}
\captionof{mytype}{My Algorithm}
\end{tcolorbox}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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