How to avoid orphan or widow when using tcolorbox?
With the version 2.02 you need the following modification. I can think about a new implementation step to reduce the overlapping.
\makeatletter
\def\tcb@split@lower@box{%
\vfuzz=\maxdimen\vbadness=10000%
\setbox\tcb@lowerbox=\vsplit\tcb@totallowerbox to\tcb@split@dim%
\setbox\tcb@lowerbox=\vbox{\unvbox\tcb@lowerbox}%
\vbadness=\tcb@thevbadness\vfuzz=\tcb@thevfuzz%
\tcbdimto\tcb@h@lower{\ht\tcb@lowerbox+\dp\tcb@lowerbox}%
\ifdim\tcb@h@lower>\tcb@split@dim\relax%
\setbox\tcb@lowerbox=\vbox to\tcb@split@dim{\unvbox\tcb@lowerbox}%
\fi%
}
\def\tcb@split@L{%
\stepcounter{tcbbreakpart}%
\tcb@following@title%
\tcb@comp@h@page%
\let\tcb@h@upper=\tcb@zpt%
\tcbdimto\tcb@h@lower{\ht\tcb@totallowerbox+\dp\tcb@totallowerbox}%
\tcbdimto\tcb@h@total{\tcb@h@after+\tcb@h@padding@last+\tcb@h@padtitle+\tcb@h@lower}%
%
\ifdim\tcb@h@total<\tcb@h@page\relax% no break
\tcb@split@setstate@last%
\setbox\tcb@lowerbox=\vbox{\begingroup\color{\kvtcb@collower}\unvbox\tcb@totallowerbox\endgroup}%
\tcb@sdraw@L%
\let\tcb@split@next=\relax%
\else
\tcb@split@setstate@middle%
\tcbdimto\tcb@split@dim{\tcb@h@page-\tcb@h@padding@middle-\tcb@h@padtitle}%
\tcb@split@lower@box%
\tcb@sdraw@L%
\setbox\tcb@totallowerbox=\vbox{\unvbox\tcb@totallowerbox}
\ifdim\wd\tcb@totallowerbox=0pt
\let\tcb@split@next=\relax%
\else%
\let\tcb@split@next=\tcb@split@L%
\fi%
\fi%
\tcb@split@next%
}
\makeatother
OLD ANSWER
The splitting algorithm of tcolorbox
has some lacks. E.g. there is no test if any box is empty, the vbox
doesn't end without removing unnecessary space. I didn't analyse
the complete algorithm.
A quick fix would be:
\documentclass{article}
\usepackage[a6paper,vmargin=15mm,hmargin=5mm]{geometry}
\usepackage{times}
\usepackage{helvet}
\usepackage{courier}
\usepackage[T1]{fontenc}
\usepackage{mathtools}
\allowdisplaybreaks
\usepackage{tcolorbox}
\usepackage{microtype}
\tcbuselibrary{breakable}
\newcounter{exercise}[subsection]
\@addtoreset{exercise}{section}
\renewcommand{\theexercise}{%
\ifnum\value{subsection}>0\relax
\thesubsection
\else
\thesection
\fi
.\arabic{exercise}%
}
\newenvironment{exercise}
{\par\smallskip\refstepcounter{exercise}
\begin{tcolorbox}[breakable,title=Exercise \theexercise,left=0mm,lefttitle=2mm]\ignorespaces}
{\end{tcolorbox}\par\smallskip\ignorespacesafterend}
\makeatletter
\def\endtcb@vbox{%
\unskip
\par\unskip%
\vskip\lastskip%
\par\strut\par\unskip\unskip
\setbox0=\lastbox
\vskip-.3\baselineskip
\endgroup}
\def\tcb@split@first{%
\setcounter{tcbbreakpart}{1}%
\ifx\kvtcb@float\@empty%
\def\tcb@before@first{\nointerlineskip\kvtcb@beforebox}%
\def\tcb@before@middle{\nointerlineskip\noindent}%
\let\tcb@before@last=\tcb@before@middle%
\long\def\tcb@after@first{\parskip\z@\par\pagebreak\nointerlineskip{\topskip\z@\null}}%
\let\tcb@after@middle=\tcb@after@first%
\let\tcb@after@last=\kvtcb@afterbox%
\else%
\edef\float@start{\noexpand\@float{tcbfloat}[\kvtcb@float]}%
\let\tcb@before@first=\float@start%
\let\tcb@before@middle=\float@start%
\let\tcb@before@last=\float@start%
\let\tcb@after@first=\end@float%
\let\tcb@after@middle=\end@float%
\let\tcb@after@last=\end@float%
\fi%
%
\edef\kvtcb@skin@@first{\kvtcb@skin@first}%
\edef\kvtcb@skin@@middle{\kvtcb@skin@middle}%
\edef\kvtcb@skin@@last{\kvtcb@skin@last}%
%
\let\tcb@h@breaksep@first=\kvtcb@bottomsep%
\tcbdimto\tcb@h@breaksep@middle{\kvtcb@bottomsep+\kvtcb@topsep}%
\let\tcb@h@breaksep@last=\kvtcb@topsep%
\tcbdimto\tcb@h@padding@first{\kvtcb@bbtop@stand+\kvtcb@bbbottom@break+\kvtcb@top@rule@stand+\kvtcb@bottom@rule@break+\tcb@h@breaksep@first+\kvtcb@boxsep*2+\kvtcb@top+\kvtcb@bottom}%
\tcbdimto\tcb@h@padding@middle{\kvtcb@bbtop@break+\kvtcb@bbbottom@break+\kvtcb@top@rule@break+\kvtcb@bottom@rule@break+\tcb@h@breaksep@middle+\kvtcb@boxsep*2+\kvtcb@top+\kvtcb@bottom}%
\tcbdimto\tcb@h@padding@last{\kvtcb@bbtop@break+\kvtcb@bbbottom@stand+\kvtcb@top@rule@break+\kvtcb@bottom@rule@stand+\tcb@h@breaksep@last+\kvtcb@boxsep*2+\kvtcb@top+\kvtcb@bottom}%
%
\tcb@split@setstate@first%
%
\splittopskip=0pt%
\splitmaxdepth=0pt%
\edef\tcb@thevfuzz{\the\vfuzz}%
\edef\tcb@thevbadness{\the\vbadness}%
%
\setbox\tcb@totalupperbox=\vbox{\unvbox\tcb@upperbox}%
\setbox\tcb@totallowerbox=\vbox{\unvbox\tcb@lowerbox}%
%
\tcbdimto\tcb@split@dim{\tcb@h@page-\tcb@h@before-\tcb@h@padding@first-\tcb@h@padtitle}%
\ifdim\tcb@split@dim<\tcb@h@upper\relax%
\tcb@split@upper@box%
\tcb@sdraw@U%
\let\tcb@split@next=\tcb@split@USL%
\else%
\setbox\tcb@upperbox=\vbox{\unvbox\tcb@totalupperbox}%
\tcbdimto\tcb@split@dim{\tcb@split@dim-\tcb@h@upper-\tcb@h@segment}
\ifdim\tcb@split@dim<\kvtcb@breakminlines\baselineskip\relax%
\tcb@sdraw@U%
\let\tcb@split@next=\tcb@split@SL%
\else
\tcb@split@lower@box%
\tcb@sdraw@USL%
\ifvoid\tcb@totallowerbox
\let\tcb@split@next=\relax%
\else
\let\tcb@split@next=\tcb@split@L%
\fi%
\fi%
\fi%
\tcb@split@next%
}
\makeatother
\begin{document}
\section{Function}
The notation $f(x)$ can be interpreted as
\begin{itemize}
\item an algebraic expression in a variable\footnote{variable is \ldots} $x$ or
\item the value of function $f$ when its variable equals to $x$.
\end{itemize}
\begin{exercise}
Explain what each of the following means
\begin{enumerate}
\item $h(x)$
\item $h(x+h)$
\item $g(a)$
\item $g(a+b-c)$
\end{enumerate}
\tcblower
\begin{enumerate}
\item $h(x)$ \ldots
\item $h(x+h)$ \ldots
\item $g(a)$ \ldots
\item $g(a+b-c)$ \ldots
\end{enumerate}
\end{exercise}
Note that $f(x)$ does not mean $f$ multiplied by $x$.
\end{document}
I am the author of tcolorbox
and I have just uploaded version 2.22 to CTAN.
This new version removed (some/most) of the addressed problems.
"There are unwanted blank pages"
I found the cause of the problem and (hopefully) eliminated it. Since the breaking of the upper part / lower part features of
tcolorbox
is sometimes a little bit tricky, I may have overlooked something. Let me know, if blank pages occur despite of the corrections."The inconsistent color scope occurs"
I found and (hopefully) corrected a color problem which caused one inconsistent color scope effect (the \item thing). Nevertheless, a color change inside the text content does not survive the breaking. This is a known drawback for
tcolorbox
. Im am sorry to say that I have no real solution in mind for this. But as far as I know it is a common problem when splitting\vboxes
."There is an unwanted orphan/widow effect"
Marco Daniel provided a quick solution (thank you for that!) which hides one of the void boxes. Version 2.22 tries to circumvent all situations where an empty box appears at the end of a break sequence. I've done a lot of tests with the new version and all worked fine. So, hopefully, this effect should not happen again - let me know, if it does ...