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.


tcolorboxis not floating by default, but you can add thefloatoption key to make it floating. Second, an alternative to the caption could be using atitlefor thetcolorbox(depends what the result should look like). – Thomas F. Sturm Jul 20 '14 at 13:50