[Tex/LaTex] overlay for lowerpart of a breakable tcolorbox

tcolorbox

I would like to define an special design for the lower part of a breakable tcolorbox. And for lowerpart I mean all contents defined after \tcblower.
I would like to define some overlay or borders or whatever for the lower part independently of how many pages it covers or when it starts in a broken box.

Let's suppose something like

enter image description here

A crossing line covers the lowerpart of the box. When the tcolorbox is not breakable there is no problem, because segmentation node can be used as top reference for lowerpart.

When this design is applied to a broken box I would like that this crossing line appears in all fragments which contain parts of the lowerpart. If some fragment contains part of upper part and part of lower, the line will cover only the lower part of the fragment (like in previous figure) but will cover the whole fragment if they only contain lower part.

As an example, following figure shows a "correct" left page but a incorrect "right" because the red line doesn't starts at top left corner.

enter image description here

Another example, on left page there is an unbroken box with a "correct" red line and the first fragment of a broken one with an "incorrect" red line. It shouldn't be here because this fragment doesn't contains any lowerpart text.

enter image description here

From previous examples it seems that segmentation is remembered from previous uses or definitions and is not deleted in fragments where it should not exist.

Previous examples have been obtained with variations of:

\documentclass[a5paper]{article}
\usepackage[most]{tcolorbox}
\usepackage{geometry}
\usepackage{lipsum}

\newtcolorbox{mybox}[1][]{%
    enhanced, 
    breakable, 
    overlay unbroken={%
        \path[draw=red] (segmentation.west)--(frame.south east);},
    overlay broken={%
        \path[draw=red] (segmentation.west)--(frame.south east);},
    #1
}

\begin{document}

\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}

As an alternative I tried with a bicolor skin but segmentation style options is not enough flexible and segmentation code doesn't respect the frame format.

Could you suggest an alternative solution?

Best Answer

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:

enter image description here