You can use underlay
options combined with remember
and frame hidden
.
remember
allows to make reference to other frame pictures or use current page
node in overlay|underlay
additions. frame hidden
avoid spurious lines behind your filled rectangles.
\documentclass{book}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}
\begin{document}
\chapter{Foo bar}
\lipsum[1-2]
\begin{tcolorbox}[oversize,
remember,
sharp corners,
breakable,
enhanced,
frame hidden,
underlay first={\fill[green!30!white] (frame.north-|current page.west) rectangle (current page.south east);},
underlay last={\fill[green!30!white] (current page.north west) rectangle (current page.east|-frame.south);},
colframe=blue,
colupper=black,
bottom=10pt,
top=10pt,
]
\lipsum[2-3]
\end{tcolorbox}
\lipsum[2]
\end{document}
Update: How to preserve header?
I don't know how to solve this problem. To my understanding you want that something which is drawn later (colored box) doesn't cover something which is already printed on your page (header). It's possible that tcolorbox
leaves some information about a broken box which could be used to know that some background must be added to next page before the header is printed. But I don't know how to use it. May be follow up question attracts interest of more smart brains than mine.
The best I can do, which looks horrible, is making a hole for header area with tikzpagenodes
package help.
\documentclass{book}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}
\usepackage{tikzpagenodes}
\begin{document}
\chapter{Foo bar}
\lipsum[1-2]
\begin{tcolorbox}[oversize,
remember,
sharp corners,
breakable,
enhanced,
frame hidden,
interior hidden,
underlay first={\fill[green!30!white] (frame.north-|current page.west)
rectangle (current page.south east);},
underlay last={\begin{scope}[even odd rule, fill=green!30!white]
\fill[clip] (current page.north west) rectangle
(current page.east|-frame.south)
(current page header area.north west) rectangle
(current page header area.south east);
\end{scope}
},
colframe=blue,
colupper=black,
bottom=10pt,
top=10pt,
]
\lipsum[2-3]
\end{tcolorbox}
\lipsum[2]
\end{document}
There is a macro called tcbsegmentstate
which contains 0
, 1
, or 2
. This macro is set for every unbroken box and every broken partial box with the following meaning:
0
: The current (partial) box contains only an upper part.
1
: The current (partial) box contains an upper and a lower part. The segmentation
node can be used for positioning.
2
: The current (partial) box contains only a lower part.
The example code can be adapted as follows:
\documentclass[a5paper]{article}
\usepackage[most]{tcolorbox}
\usepackage{geometry}
\usepackage{lipsum}
\makeatletter
\newtcolorbox{mybox}[1][]{%
enhanced,
breakable,
overlay={%
\ifcase\tcbsegmentstate
% 0 = Box contains only an upper part
\or%
% 1 = Box contains an upper and a lower part
\path[draw=red] (segmentation.west)--(frame.south east);
\else%
% 2 = Box contains only a lower part
\path[draw=red] (frame.north west)--(frame.south east);
\fi%
}
#1
}
\makeatother
\begin{document}
\begin{mybox}
This is the only part
\end{mybox}
\begin{mybox}
This is the upper part
\tcblower
\lipsum[3]
\end{mybox}
\begin{mybox}
\lipsum[1-2]
\tcblower
\lipsum[3-4]
\end{mybox}
\end{document}
This gives:
Best Answer
First, the behaviour is not a bug. If a break occurs at the segmentation between upper and lower part, then the segmentation line and the lower part go to the next page. There, the segmentation is displayed to signal that the lower part starts.
For
bicolor
, the color change also signals the begin of the lower part. So, it may be preferable to have no explicit segmentation line here. I take it as a feature request that the segmentation is optionally not drawn on page breaks :-)For 'faked' segmentations with
\tcbline
, there already is the option to use\tcbline*
instead to get this feature.For 'real' segmentations, this option is currently not available. But, I think we can do it with the following new option:
With it, the segmentation line is not drawn at the begin of the splitted box. I have to investigate, if this causes some unwanted side-effects. Also, a good name for the feature has to be found (suggestions are welcome).
The full example code is: