I think I've stumbled upon a tcolorbox bug: whenever there are two consecutive breakable boxes, neither can fit unbroken in the current page, and the content of the first is unbreakable (e.g. an image) while the content of the second is breakable (e.g. text), then the first is moved to the next page as expected, but for some reason, the second one is not broken and end-up overflowing from the page. Making the first box unbreakable (which should change basically nothing since it can not be broken) fixes the problem, which makes me think that this is due to tcolorbox somehow wrongly applying the decision to not break the first box to the second box.
The actual use case is a PhD thesis where every figure, theorem (and fact, proposition, remark, etc.) and proof is in a breakable tcolorbox. This problem happens at unexpected places because the figures float while the theorems do not, which makes manually making some boxes unbreakable unmanageable.
I've looked at the code for the beakable, tried changing the values of booleans \tcb@break@allowedtrue and \tcb@ignorenobreaktrue and placing the box in a group (to prevent value changes from leaking to the other box), but this had no effect.
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{afterpage}
\usepackage{lipsum}
\tcbset{enhanced}
\newcommand{\breakablecontent}{\lipsum[1-12]}
\newcommand{\nonbreakablecontent}{\rule{\columnwidth}{\columnwidth}}
\newcommand{\mytest}[1]{
\lipsum[1-3]
\afterpage{
\begin{tcolorbox}[breakable=true]
\breakablecontent
\end{tcolorbox}
}
\begin{tcolorbox}[breakable=#1]
\nonbreakablecontent
\end{tcolorbox}
}
% Naive Attempt to fix this:
%\makeatletter
%\renewcommand{\mytest}[1]{
% \lipsum[1-3]
% \afterpage{
% \tcb@break@allowedtrue\tcb@ignorenobreaktrue
% \begingroup
% \begin{tcolorbox}[breakable=true]
% \breakablecontent
% \end{tcolorbox}
% \endgroup
% \tcb@break@allowedtrue\tcb@ignorenobreaktrue
% }
% \tcb@break@allowedtrue\tcb@ignorenobreaktrue
% \begin{tcolorbox}[breakable=#1]
% \nonbreakablecontent
% \end{tcolorbox}
%}
%\makeatother
\begin{document}
\section{Expected behavior}
\mytest{false}
\clearpage
\section{Unexpected behavior}
\mytest{true}
\end{document}
\afterpagewhere you place the firsttcolorboxin. Without this, the breakpoints work as expected. What is the reason you use this? Would\clearpagebe a workaround for you, maybe? – Jasper Habicht May 26 '23 at 11:01\afterpage{\begin{landscape}\begin{figure}[H]. Maybe I should just use the landscape option of tcolorbox to rotate only the figure, and then store lists of pages I want to rotate back and use something similar to https://tex.stackexchange.com/questions/451418/floating-landscape-environment – xavierm02 May 26 '23 at 12:03\saveboxand\useboxfor the second box, which prevents any interleaving with the first box and hence fixes the problem for this example. But I have use cases where the afterpage figure takes several pages so this would not always work (as can be seen if we replace the content of the first box with\nonbreakablecontent\\\nonbreakablecontent\\\nonbreakablecontent\\\nonbreakablecontent). – xavierm02 May 26 '23 at 12:20\captionof{figure}{...}(caption and capt-of packages). – John Kormylo May 26 '23 at 12:49