2

This is follow-up question of my previous question tcolorbox: It is possible to put breakable boxes behind each other (overlap)?.

My question: how can I simulate various defined breakable boxes inside a breakable box by overlapping, so they seem to be inside each other? The defined boxes are for example

\newtcolorbox{}

Example would be enter image description here

1 Answers1

3

Following with the idea proposed in tcolorbox: It is possible to put breakable boxes behind each other (overlap)? this code proposes to declare three kind of boxes: topbox, middlebox and bottombox.

topbox must the first in the desired series, it contains and draws the outer title. After a topbox you can place as much as middlebox-es as you want and the series ends with a bottombox which will close the false outer tcolorbox.

middlebox draws outer lines and enlarges them to join previous topbox and following bottombox. It shows a wrong behaviour when the middle box is not broken and it's placed to the bottom of a page. I don't know how to solve it.

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

\newtcolorbox{topbox}[2][]{
    title=#2,
    enhanced, breakable, 
    enlarge top initially by=1cm,
%    enlarge bottom finally by=5mm,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken and first={
        \draw[green!70!black, line width=.5mm, rounded corners]
            ([xshift=-5mm]frame.south west)|-([yshift=1cm]frame.north)-|
            ([xshift=5mm]frame.south east);
        \node[fill=green!70!black, minimum height=5mm, 
            minimum width=\linewidth, anchor=north] at ([yshift=1cm]frame.north) 
            (outertitle) {};
        \node[text=white, anchor=west] at ([xshift=3mm]outertitle.west) {Outer title};
        },
    overlay middle and last={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
    #1
}

\newtcolorbox{middlebox}[2][]{
    title=#2,
    enhanced, breakable, 
%    enlarge top initially by=1cm,
%    enlarge bottom finally by=5mm,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken={
        \draw[green!70!black, line width=.5mm]
             ([shift={(-5mm,5mm)}]frame.north west)--([shift={(-5mm,-5mm)}]frame.south west)
             ([shift={(5mm,5mm)}]frame.north east)--([shift={(5mm,-5mm)}]frame.south east);
        },
    overlay first={
        \draw[green!70!black, line width=.5mm]
             ([shift={(-5mm,5mm)}]frame.north west)--([xshift=-5mm]frame.south west)
             ([shift={(5mm,5mm)}]frame.north east)--([xshift=5mm]frame.south east);
        },
    overlay middle={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
    overlay last={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([shift={(-5mm,-5mm)}]frame.south west)
             ([xshift=5mm]frame.north east)--([shift={(5mm,-5mm)}]frame.south east);
    },
    #1
}

\newtcolorbox{bottombox}[2][]{
    title=#2,
    enhanced, breakable, 
%    enlarge top initially by=1cm,
    enlarge bottom finally by=5mm,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken and last={
        \draw[green!70!black, line width=.5mm, rounded corners]
             ([xshift=-5mm]frame.north west)|-([yshift=-5mm]frame.south)
             -|([xshift=5mm]frame.north east);
        },
    overlay first={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
        },
    overlay middle={
        \draw[green!70!black, line width=.5mm, rounded corners]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
    #1
}

\begin{document}
\lipsum[1]
\begin{topbox}{this is the title}
\lipsum[1-2]
\end{topbox}
\begin{middlebox}[colframe=red, colback=pink!10]{this is the title}
\lipsum[1-5]
\end{middlebox}
\begin{middlebox}[colframe=brown!70!black, colback=brown!10]{this is the title}
\lipsum[1-2]
\end{middlebox}
\begin{bottombox}[colframe=blue, colback=cyan!10]{this is the title}
\lipsum[1-3]
\end{bottombox}
\lipsum[1]
\end{document}

enter image description here

Ignasi
  • 136,588
  • wow thanks!! :D how do you know this stuff haha? – Faceb Faceb Apr 18 '18 at 08:38
  • but I already predefined the inner boxes (the black, red, brown, blue boxes in your answer) in a separate \newtcolorbox where I defined frame color, background color etc, so I would like to 'instantiate' them only and without redefining them. Is it possible too? – Faceb Faceb Apr 18 '18 at 09:19
  • @FacebFaceb You have to adapt this solution to your taste. If you read the code (I hope you do it) you'll see that outer box is fixed for all of them but all other parmeters can be adapted. In any case you have to decide which box if the first and declare it as topbox, which are in the middle and declare as middlebox and which one is the last one and declare as bottombox. – Ignasi Apr 18 '18 at 09:25
  • @FacebFaceb As I've already explained you, what you want (breakable neste d boxes) is NOT possible, at least with this version of tcolorbox. Therefore these codes are partial solutions that can I think solve your question. Now it's your time to decide if you want to use or not and adapt to the real situation. – Ignasi Apr 18 '18 at 09:30
  • I will try, thanks anyway! Is it possible to reuse parts of your code by \newcommand, if some parts are similar to each other? – Faceb Faceb Apr 18 '18 at 14:42
  • @FacebFaceb I think it's better to use tcbstyles, if what you want to reuse is part of a tcolorbox definition. – Ignasi Apr 18 '18 at 15:03
  • it is possible to customize the green title bar to something like page 162 of the manual https://mirror.hmc.edu/ctan/macros/latex/contrib/tcolorbox/tcolorbox.pdf – Faceb Faceb Apr 18 '18 at 18:32
  • @FacebFaceb Yes. Read the manual, try it and if you don't succeed, make a new question. – Ignasi Apr 18 '18 at 18:37