Sidenotes and marginfigures in tcolorbox theorems

I am using the tufte-book class to write my notes and I would like to have tcolorbox theorems that can contain marginfigures and sidenotes (provided by the sidenotes package). Following I will post a MWE and the error I have in both situations:

\documentclass[nobib,notoc, a4paper]{tufte-book}

% For the example image
\usepackage{graphicx}

\usepackage[T1]{fontenc}
\usepackage[italian]{babel} % Italian support
\usepackage[utf8]{inputenc} % Accented letters

\usepackage[theorems]{tcolorbox}

\newtcbtheorem{Thm}{Teorema}%
{colback=green!5,colframe=green!35!black,fonttitle=\bfseries}{thm}

\begin{document}

\mainmatter

\begin{Thm}{Titolo}{esempio}
Esempio di teorema.
%\sidenote{Una nota a margine}
\begin{marginfigure}
\includegraphics{example-image-a}
\caption{Figura di esempio}
\label{fig:ex_a}
\end{marginfigure}
\end{Thm}

\end{document}


When using the marginfigure environment I incur in

LaTeX Error: Not in outer par mode.


while when using a side note

l.29 LaTeX Error: Float(s) lost.
l.21 Missing number, treated as zero.


I had this problem not only with tcolorbox but also with ntheorem (that I am currently using but moving from because I would like different styles for theorems, definitions, examples etc.) I am basically forced to use marginfigures only outside of these environments making the positioning very tricky and the code quite messy.

I looked at this answer and this too but none of them actually solved my problem since I cannot put theorems inside marginfigures. From what I understand the problem is that both marginfigures and sidenotes are floats and so are tcolorbox' theorems and I can't create one from the other. I'm not grokking LaTeX internals well enough to develop a solution or to understand better.

Any insight for this would be greatly appreciated!

• – M.Ahmadi Oct 16 '19 at 18:48
• I read those and now I seem to understand what the problem is but still have no idea on how to go around it or to solve it unfortunately – gjkf Oct 16 '19 at 21:38
• @gjkf I don't understand the problem. What do you want? Do you want to put a sidenote or margin figure INSIDE the tcolorbox which contains a theorem? In this case I suggest to use a sidebyside tcolorbox with the theorem and corresponding figure/note. But if you just want to associate them to a theorem but left them float you can declare outside theorem environment. Please, explain it better. – Ignasi Oct 17 '19 at 6:51
• This is an example of what I currently have. There I am using a ntheorem environment and two rules to create the two green lines. The marginfigure environrment is put inside the ntheorem one, like so. (Note that \incfig only loads both an svg and the pdf_tex file generated by Inkscape, it's not relevant). What this method achieves is that the marginfigure is automatically aligned with the theorem. If I instead of ntheorem try to use any kind of floats (even framed theorems or tcolorboxes), the above error shows up. – gjkf Oct 17 '19 at 7:43
• What I am then trying to get is a way to have the automatic alignment of sidenotes and marginfigures with tcolorbox theorems, while also keeping the tufte style of the page. – gjkf Oct 17 '19 at 7:44

1 Answer

Here there are two possible solutions for marginfigures. I think OP wants something like the second, but ...

Sidenotes are another problem not solved here.

First solution uses a sidebyside box where upper part is the theorem contents and lower part a non floating figure which has been captioned with \captionof command.

Second solution uses a tcbraster with two columns. These columns have their own size, left one is the tcolorboxed theorem and the right one a non floating figure with a caption.

Some work on margins is needed but this is just a proof of concept.

By the way, neither sidebyside tcolorboxes nor rows from a tcbraster can be broken between pages. Therefore some pagination problems should be expected.

\documentclass[nobib,notoc, a4paper]{tufte-book}

% For the example image
% \usepackage{graphicx} %<- loaded by tcolorbox

\usepackage[T1]{fontenc}
\usepackage[italian]{babel} % Italian support
\usepackage[utf8]{inputenc} % Accented letters

\usepackage[most]{tcolorbox}

\usepackage{caption}
\usepackage{lipsum}

\tcbset{
fullwidth/.style={
enhanced,
grow to right by=\marginparsep+\marginparwidth,
sidebyside,
righthand width=\marginparwidth,
sidebyside gap=\marginparsep,
segmentation hidden},
}

\newtcbtheorem{Thm}{Teorema}%
{colback=green!5,colframe=green!35!black,fonttitle=\bfseries}{thm}

\begin{document}

\mainmatter

\lipsum[2]

\begin{Thm}[fullwidth]{Titolo}{esempio}
\lipsum[2]
\tcblower
\centering
\includegraphics[width=\linewidth]{example-image-a}
\captionof{figure}{My first figure}
\end{Thm}

\begin{tcbraster}[
raster width=\linewidth+\marginparsep+\marginparwidth,
raster force size=false,
raster column skip=\marginparsep,]
\begin{Thm}[width=\textwidth]{Titolo}{esempio}
Esempio di teorema.
\end{Thm}
\begin{tcolorbox}[blankest, width=\marginparwidth]
\includegraphics[width=\marginparwidth]{example-image-a}
\captionof{figure}{Figura di esempio}
\label{fig:ex_a}
\end{tcolorbox}
\end{tcbraster}

\end{document}


• This is absolutely fantastic, I love both solutions and will try them out tomorrow morning. Thanks a lot! By the way, are sidenotes a "different" kind of floats from figures that they cannot be solved this way? If so, are there any "hacks" or workarounds that can achieve the same or similar result? Thanks again and I will report back when I have tried both solutions tomorrow – gjkf Oct 17 '19 at 22:17
• @gjkf tufte-book class already defines sidenotes and marginnotes and I don't know how to patch them. Loading sidenotes or snotez packages gives error. So, I think it's better to write another question for this particular case. – Ignasi Oct 18 '19 at 8:36
• I will ask a new question specifically for sidenotes then, thanks :) Is there no way around the breaking of the tcolorbox theorems? Since I have many of those I might have a little problem with so many blank pages. – gjkf Oct 18 '19 at 13:53
• Looking at this answer it seems I could use marginnotes that can contain figures, in pretty much the same way. I noticed that without the fullwidth option, the theorems become breakable and I can put figures and notes (without numbering unfortunately) in fixed places. I will experiment some more and try to find the solution that suits me best but this is a solid contender. – gjkf Oct 18 '19 at 14:05