# Algorithm2e with tufte layout: how to get full-width and margin floats?

I'm using the tufte-book layout, which provides marginfigure, figure and figure* environments to create floating figures which occupy either the margin only, the main text column only, or uses the full page width. The same are defined for tables, but I need to typeset algorithms and I'm wondering how to achieve the same effect. I use algorithm2e (I would rather not change that), which defines an environment algorithm, which by default will be floating, but to which option [H] can be given.

My problem is that if I "cheat" and use a figure* to fit a \begin{algorithm}[H] inside, I get a figure label, whereas I would like a separate numeration for algorithms as algorithm2e does by default. Any idea how to do that (e.g. for full-width algorithm)? I guess it's possible to create a new environment by copying what is used for figures in tufte-book layout's code, but I'm not sure how to re-use algorithm2e numbering.

[EDIT] Here is a MWE. I tried: standard floating algorithm (nice, but would be good to have a legend on the side), static algorithm inside a fullwidth environment (does not align properly), and static algorithm inside a figure* environment (good width, but I would like to replace the legend to use “Algorithm …” as a label). Also, the figure* with an alorithm inside always moves to a separate page (even when it could fit on the previous page), which is not why I want, but I couldn't find how to prevent that, any ideas?

[EDIT2] I removed the fullwidth block, as it does not compile properly.

\documentclass[notoc,nobib,a4paper,justified]{tufte-book}
\usepackage{amssymb,amsmath,amsthm}
\usepackage{fontspec,lipsum}
\usepackage{polyglossia}
\setmainlanguage{english}
\usepackage[ruled,algosection,vlined,linesnumbered]{algorithm2e}
\usepackage{framed}

\begin{document}

\begin{marginfigure}
\begin{framed}
This is a \texttt{marginfigure}.
\end{framed}
\caption{Caption of \texttt{marginfigure}.}
\end{marginfigure}

Some line of text, to avoid collisions.

\begin{figure}
\begin{framed}
This is a \texttt{figure}.
\end{framed}
\caption{Caption of \texttt{figure}.}
\end{figure}

\begin{figure*}
\begin{framed}
This is a \texttt{figure*}.
\end{framed}
\caption{Caption of \texttt{figure*}.}
\end{figure*}

Here is a floating algorithm. Nice, but would be better with a legend on the side, like for \texttt{figure}.

\begin{algorithm}
\KwIn{Some inputs}
\For{some condition}{
$x ←$ some operations \;
}
\Return $x$\;
\caption{This is a floating algorithm}
\end{algorithm}

Below is a floating figure with a fixed algorithm inside. It does wraps on the full width, which is nice, but the caption below is now "Figure 4", and I want it to be "Algorithm …".

\begin{figure*}
\begin{framed}
\begin{algorithm}[H]
\KwIn{Some inputs}
\For{some condition}{
$x ←$ some operations \;
}
\Return $x$\;
\caption{This is a fixed (H) algorithm.}
\end{algorithm}
\end{framed}
\caption{Caption of \texttt{figure*}.}
\end{figure*}
\end{document}

• Hello, please provide a minimal working example with your setup, so that we have something to start from – BambOo Jun 1 at 19:27
• @BambOo I edited and added a MWE. Does it help? – Zooky Jun 2 at 6:09
• I tried to compile your example with lualatex but I get an error complaining about a missing item. Do you concurr ? – BambOo Jun 2 at 6:24
• Sorry for that! It did produce a document on my side… but indeed ignoring an error. Looks like an algorithm cannot be placed in a fullwidth environment. I removed this part from the MWE, now it compiles perfectly for me. – Zooky Jun 2 at 6:35