# How to avoid orphan or widow when using tcolorbox?

My MWE below should not be simplified anymore to avoid breaking my real scenario. The settings are tightly coupled.

\documentclass{article}
\usepackage[a6paper,vmargin=15mm,hmargin=5mm]{geometry}
\usepackage{times}
\usepackage{helvet}
\usepackage{courier}
\usepackage[T1]{fontenc}
\usepackage{mathtools}
\allowdisplaybreaks
\usepackage{amssymb}% for \mathbb
\usepackage{tcolorbox}
\usepackage{cancel}
\usepackage{microtype}

\tcbuselibrary{breakable}
\newcounter{exercise}[subsection]
\makeatletter

\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,shrink break goal=18pt]\ignorespaces}
{\end{tcolorbox}\par\smallskip\ignorespacesafterend}

\makeatother

\def\IncludeGraphics{\rule{4cm}{1.5cm}}

\begin{document}
\begin{exercise}
xxxxxxxx xxxxxxxxx xxxxxxx.
\begin{enumerate}
\item $-2x>4$
\item $7x-1<13$
\item $\tfrac{x-1}{2}\leq\tfrac{x+1}{3}$
\item $x^2 -5x +6 \geq 0$
\item $x^2 < 16$
\item $(x-1)^2(x+2)^3 > 0$
\item $\frac{(x-1)^2}{(x+2)^3} \geq 0$
\end{enumerate}
\tcblower
\begin{enumerate}
\item
\! \begin{aligned}[t] -2x &>4\\ x &< \frac{4}{-2} &&\text{\parbox{5cm}{\color{red}\scriptsize xxxx xxxxx xxxx xxxxxx-2. xxxxx xxxxxxxxxxxxxx xxxxx xxxxxxx xxxx xxxxxxx xxxx xxxxxxx xxxxxx xxxxxxxx xxxxxxx}}\\ x &< -2 \end{aligned}
\item
\! \begin{aligned}[t] 7x -1 &< 13\\ 7x &< 13 +1\\ 7x &< 14\\ x &< \frac{14}{7}\\ x &< 2 \end{aligned}
\item
\! \begin{aligned}[t] \frac{x-1}{2} &\leq \frac{x+1}{3}\\ \frac{x-1}{2} \times 2 \times 3 &\leq \frac{x+1}{3} \times 2 \times 3\\ \frac{x-1}{\bcancel{2}} \times \bcancel{2} \times 3 &\leq \frac{x+1}{\bcancel{3}} \times 2 \times \bcancel{3}\\ 3(x-1) &\leq 2(x+1) \\ 3x-3 &\leq 2x +2\\ 3x-2x &\leq 2 +3\\ x &\leq 5 \end{aligned}
\item
xxxxxxxx xxxx xxxxx xxxx xxxxxx xxx xxx xxxxxxxxx xxxxxxxx xx xxxx xxxx.
\begin{align*}
x^2 -5x +6 &\geq 0\\
x^2 -3x -2x +6 &\geq 0\\
x(x-3) -2(x-3) &\geq 0\\
(x-3)(x-2) &\geq 0
\end{align*}
{\color{red}xxxxxx xxxxxxx xxxxxxx xxx!
\begin{align*}
x-3&\geq 0 &&\text{or} & x-2&\geq 0\\
x&\geq 3 && \text{or} & x&\geq 2
\end{align*}\par}
xxxxxxxxxxx xxxxx xxxxxxxx~\ref{enum:xxxxxxx-xxxxxxxxxxxxxx}. xxxxxxx xxx xxxxxx xxxxxxxxxx.
\begin{center}
\IncludeGraphics
\end{center}
xxxxxxx xxxxxxxx xxxxxx $\{\, x\in \mathbb{R} \mid x\leq 2\vee x\geq 3\,\}$ xxxxxx xxxxxx $\vee$ xxxxxx xxxx''.
\item
xxxxxxxx xxxx xxxxx xxxx xxxxxx xxx xxxxxxxx xxxxxxxxx xxxx xxxx.
\begin{align*}
x^2 &< 16\\
x^2 -16 &< 0\\
x^2 -4x +4x -16 &< 0\\
x(x-4) +4(x-4) &< 0\\
(x-4)(x+4) &< 0
\end{align*}
{\color{red}xxxxxx xxxxxxxxx xxxxxxx xxxxxxxxxxx
\begin{align*}
x-4 &<0 &&\text{or} & x+4 &< 0\\
x &<4 &&\text{or} & x &<-4
\end{align*}\par}
xxxxxxxxxxxxx xxxxxxxxx xxxxxxxx~\ref{enum:xxxxxxx-xxxxxxxxxxx}. xxxxxxxxx xxxxxxxx xxxxxx xxxxxxxxxx.
\begin{center}
\IncludeGraphics
\end{center}
xxxxxxx xxxxxx xxxxxxx $\{\, x\in \mathbb{R} \mid x > -4 \wedge x < 4\,\}$ xxxxxxx xxxxxxxx $\wedge$ xxxx xxx''. xxxxxxxxx $\{\, x\in \mathbb{R} \mid x > -4 \wedge x < 4\,\}$ xxxxxx xxxxxxxxxx xxxxxxxxxx $\{\, x\in \mathbb{R} \mid -4 < x < 4\,\}$.
\item
xxxxxxxx xxxxx xxxxxxx xxxxxx xxxxxxxxxx xxx xxx xxxxx xxxxx xxxxxxx xxxxxxx xxxxxxx xxxxx xxxxx xxxx xxxxxxxxxx xx xxxxxxxx.
\begin{align*}
(x-1)^2(x+2)^3 > 0
\end{align*}
xxxxxxxxxxxxx xxxxxxxxx xxxxxxxx~\ref{enum:xxxxxxx-xxxxxxxxxxx}. xxxxxxxxx xxxxxxxx xxxxxx xxxxxxxxxx.
\begin{center}
\IncludeGraphics
\end{center}
xxxxxxxxx xxxxxxx xxxxxxxx $\{\, x\in \mathbb{R} \mid x > -2 \wedge x \ne 1\,\}$ xxxxx xxx $\wedge$ xxxxxxx xxxx''.
%
\item
xxxxxxxxx xxxx xxxxx xxxxx xxxxxx xxx xxx xxxxxxx xxxx xxxxx. xxxxx xx xxxx xxxxx xxxxxxx xxxxx xxx xxxxx xxxxx xxx.
\begin{align*}
\frac{(x-1)^2}{(x+2)^3} \geq 0\\
(x-1)^2(x+2)^3 \geq 0 && \text{xxxx $x\ne -2$}
\end{align*}
xxxxxxxxxxxxx xxxxxxxxx xxxxxxxx~\ref{enum:xxxxxxx-xxxxxxxxxxx}. xxxxxxxxx xxxxxxxx xxxxxx xxxxxxxxxx.
\begin{center}
\IncludeGraphics
\end{center}
xxxxxxx xxxxxx xxxxxx $\{\, x\in \mathbb{R} \mid x > -2 \,\}$.
\end{enumerate}
\end{exercise}
\end{document}


## Problems:

1. There are unwanted blank pages.
2. The inconsistent color scope occurs. The continued parts in different broken page loses the color scope of the previous pages.
3. There is an unwanted orphan/widow effect.
4. The last \item cannot be removed without affecting the color scoping.

How to fix these issues?

## Edit:

The problem fixed! I am trying to find another bug. :-)

-
thanks for the images. widow and clubpenalty have no effect on that content irrespective of tcolorbox as every (tex primitve) paragraph is a single line so those penalties are not inserted as there is no first or last line that can be separated from the rest of the paragraph. –  David Carlisle May 9 '13 at 21:14
@MarcoDaniel: Because I thought it is a best practice to always invoke \ignorespaces at end. –  cyanide-based food May 9 '13 at 22:18
@MarcoDaniel: I forgot to invoke \makeatletter...\makaatother, the code snippet above was extracted from my own package. How does he fix it? Will you inform him about this issue? –  cyanide-based food May 9 '13 at 22:22
@MarcoDaniel: OK. Thank you for your help. –  cyanide-based food May 9 '13 at 22:29
Your example works fine with the new version 2.22 of tcolorbox :-) Still, some color issues remain and will remain (see my answer). –  Thomas F. Sturm May 15 '13 at 17:24

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{%
\setbox\tcb@lowerbox=\vsplit\tcb@totallowerbox to\tcb@split@dim%
\setbox\tcb@lowerbox=\vbox{\unvbox\tcb@lowerbox}%
\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}%
%
\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%
\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


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]

\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%
%
\tcb@split@setstate@first%
%
\splittopskip=0pt%
\splitmaxdepth=0pt%
\edef\tcb@thevfuzz{\the\vfuzz}%
%
\setbox\tcb@totalupperbox=\vbox{\unvbox\tcb@upperbox}%
\setbox\tcb@totallowerbox=\vbox{\unvbox\tcb@lowerbox}%
%
\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}

-
This solution is not reliable. I still get the same issue. –  cyanide-based food May 12 '13 at 19:03
@Bugbusters: Did you test the mwe? Do you use the newest version of tcolorbox? –  Marco Daniel May 12 '13 at 19:31
Yes. But for other contents, it no longer works. I used Manual for version 2.02 (2013/03/13). –  cyanide-based food May 12 '13 at 19:34
@Bugbusters: I am using \ProvidesPackage{tcolorbox}[2013/04/17 version 2.20 text color boxes] –  Marco Daniel May 12 '13 at 19:53
I used [2013/03/13 version 2.02 text color boxes]. –  cyanide-based food May 12 '13 at 19:56

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.

1. "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.

2. "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.

3. "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 ...

-
Welcome to TeX.sx! –  cgnieder May 13 '13 at 11:18