7

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

  • 2
    It would be nice to have a good answer to this question, but, obviously, I do not have one. \vsplit is used internally, but coloring is not really supported for this command, see http://tex.stackexchange.com/questions/150780/color-bleeding-when-using-vsplit-for-a-box-with-colored-text and David Carlisles answer to this. Changing color setting for the current tcolorbox implementation is very touchy since I put in a great deal of time to get it like it is now. It is not perfect and has same drawbacks, but it circumvents a lot of problems. – Thomas F. Sturm Dec 31 '14 at 10:14
  • 1
    Nevertheless, if there are new insights in how to enhance the current implementation (for all use cases, for pdflatex, for xelatex, ... ) I'm willing to give it a new try :-) – Thomas F. Sturm Dec 31 '14 at 10:16
  • @ThomasF.Sturm: Thanks for your comment(s) ... Isn't it possible to store the color and restore it in the next (continued) box? –  Dec 31 '14 at 10:19
  • 1
    xelatex also has a special color bug which I cannot test on my system, but is circumvented currently, see http://tex.stackexchange.com/questions/186139/last-xelatex-colors-text-in-white-in-some-circumstances/201611#201611 – Thomas F. Sturm Dec 31 '14 at 10:19
  • 1
    @ThomasF.Sturm: I've read your question from your link above and this is, what I feared ;-) Interestingly I need color bleeding where you want to prevent it ... The double grouping is new to me. I will ask David if he has some clue anyway. –  Dec 31 '14 at 10:36
  • 1
    I don't have much hope in the moment, but you never know ;-) – Thomas F. Sturm Dec 31 '14 at 10:42
  • @ThomasF.Sturm: Otherwise, \vsplit boxes keep the colour, as I have realized (still new to 'TeX' programming), I would really like to use your breakable boxes, although I my particular case I don't need the other features of your marvellous package –  Dec 31 '14 at 10:48
  • Basically it's not possible to get this right in TeX (@ThomasF.Sturm) – David Carlisle Dec 31 '14 at 11:49
  • @ThomasF.Sturm: Unrelated: A question for you? http://tex.stackexchange.com/questions/220153/marginalia-inside-tcolorbox –  Jan 01 '15 at 23:05
  • @ChristianHupfer Thanks. I tried to answer this other question now. – Thomas F. Sturm Jan 02 '15 at 10:48

1 Answers1

12

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

Ulrike Fischer
  • 327,261
  • Thanks, this is a new idea... I am currently not using xelatex nor lualatex... I'll give it a try! –  Dec 31 '14 at 11:45
  • 1
    Be aware that the font color always wins. You can't use something like \textcolor{yellow}{yellow} with it. – Ulrike Fischer Dec 31 '14 at 11:51
  • 2
    @UlrikeFischer do you think it would make sense to have an option to color package so it used the font attribute as well as (or instead of?) the color special, so that \textcolor{yellow}{...} would combine with this? – David Carlisle Dec 31 '14 at 12:24
  • 2
    @DavidCarlisle It would certainly worth consideration. But as I wrote such font colors wins but they don't affect all fonts - only the one loaded with \setmainfont etc (and so no rules and without unicode-math no math). And the combination with color commands can lead to curious effects. E.g. after adding colors to the fonts the math will be red with \textcolor{red}{$a=b$}, but \textcolor{red}{blbla $a=b$} it will be black (with luatex, with xetex is is red again). So imho some careful investigations are needed. – Ulrike Fischer Jan 02 '15 at 09:36