[Tex/LaTex] Maintaining text colour change in a breakable tcolorbox

boxescolortcolorbox

I try to maintain the current text colour in breakable boxes from tcolorbox, but the manual clearly states (see manual of the current version, section 15.2, page 292, version 3.34)

• If your text content contains some text color changing commands,
your color will not survive the break to the next box.

So, this has to fail (right now). However, colour changes are maintained in explicit
TeX boxes, being split using \vsplit.

Is there a way (i.e. hack/patch) to force tcolorbox to maintain the current text colour from one part of the broken box to the next one?

A grouping with { \color{....}...} or \begingroup \color{...}...\endgroup or \textcolor{...}{...} does not work neither.

\documentclass{article}

\usepackage{blindtext}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}

\begin{document}

\begin{tcolorbox}[breakable]
  \blindtext[3]
  \color{blue}  % To 'force' color change 
  \blindtext[3]
  \color{red}
  \blindtext
  \color{brown}
  \blindtext
\end{tcolorbox}


\end{document}

The colour should be blue on the upper half of the second box if it would work 😉

enter image description here

Best Answer

With xelatex or lualatex you could color the font instead of using the color commands. This would survive a box break:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Latin Modern Roman}
\usepackage{blindtext}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}

\begin{document}

\begin{tcolorbox}[breakable]
  \blindtext[3]
  \addfontfeatures{Color=blue}
  \blindtext[3]
  \addfontfeatures{Color=red}
  \blindtext
  \addfontfeatures{Color=brown}
  \blindtext
\end{tcolorbox}


\end{document}

enter image description here

Related Question