# Breakable nested tcolorbox

I am currently using tcolorbox to create a rounded corner grey box within a rounded corner bordered box:

\documentclass{article}

\usepackage[breakable,skins]{tcolorbox}
\usepackage{lipsum}

\newtcolorbox{OuterBox}[1][]{%
breakable,
enhanced,
colback=white,
colframe=blue,
#1}%

\newtcolorbox{InnerBox}[1][]{%
enforce breakable,
enhanced,
colback=gray,
colframe=white,
#1}%

\begin{document}
\begin{OuterBox}
Just a line of text
\begin{InnerBox}
\lipsum[1]
\lipsum[1]
\lipsum[1]
\lipsum[1]
\end{InnerBox}
\end{OuterBox}
\end{document}


In the above example the nested tcolorbox is started on the next page instead of continuing straight after the contents of the outer box which is rather messy. In more complex documents it becomes very messy:

Preventing the inner box from being breakable fixes those graphical glitches and is the reason why the documentation cautions against its use:

In the unlikely case you really want to have the nested box to be breakable, use /tcb/enforce breakable for the nested box. But, a breakable box inside a breakable box will usually give a mess.

This is also a limitation of mdframed as it's documentation states:

A nested mdframed environment can’t be splitted.

Is there an alternative package that does support this use?

• mdframed perhaps. Please note that pagebreaking and nesting is a very difficult issue.
– user31729
May 17, 2016 at 16:11
• Did you try the option to see if you get the usual mess or what you want?
– cfr
May 17, 2016 at 22:51
• Note that the option does work (messily) if the inner box exceeds the available height within the box for a page. It just prefers breaking before the inner box to breaking within it. So if you put some of the stuff in one inner box and everything else in another, it does break. Messy, a bit, but it does kind of work.
– cfr
May 17, 2016 at 23:05
• @ChristianHupfer Unfortunately mdframed doesn't support nesting. It must be a very difficult issue if its not fully supported by mature packages like mdframed or tcolorbox May 17, 2016 at 23:59
• @ChristianHupfer Sorry I meant to say nested mdframed environments can't be broken/split over a page. I have updated my question to show the documentation lists that as known limitation. May 18, 2016 at 0:28

I propose a partial solution. The main shortcoming is its dependence on later changes made in the .tex file.

The idea is to introduce a command that ends the current inner box and starts a new one. If a breaking is needed, the command is inserted in the text and the desired split is achieved. Without forcing the breaking of the inner box (not needed). In the code below the command is \splitbox. Moreover, starting with the default values of the inner box, TikZ is used to construct sharp corners at the bottom of the current page and at the top of the next one for the split box.

\documentclass{article}
\usepackage{tikz}
\usepackage[breakable, skins]{tcolorbox}
\usepackage{lipsum}

\newtcolorbox{OuterBox}[1][]{%
breakable,
enhanced,
colback=white,
colframe=blue,
#1
}

\newtcolorbox{InnerBox}[1][]{%
enforce breakable,
enhanced,
colback=yellow,
colframe=yellow,
#1
}

\newcommand{\splitbox}{%
\hfill
\begin{tikzpicture}[remember picture, overlay]
\draw[yellow, line width=1.5ex] (1.55em, -2.2ex) -- +(-1\textwidth-3.1em, 0);
\end{tikzpicture}
\end{InnerBox}
\begin{InnerBox}
\begin{tikzpicture}[remember picture, overlay]
\draw[yellow, line width=1.5ex] (-1.55em, 3.2ex) -- +(1\textwidth+3.1em, 0);
\end{tikzpicture}\hspace{-.7ex}
}

\title{Breaking inner boxes. Trial and error}
\date{\vspace{-8ex}}

\begin{document}
\maketitle

\section{The forced breaking is needed}
\lipsum[7]
\begin{OuterBox}
Just a line of text.
\lipsum[5]
\begin{InnerBox}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut pu-
rus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Cur-
abitur dictum gravida mauris. Nam arcu libero, nonummy eget,
consectetuer id, vulputate a, magna. Donec vehicula augue eu
neque. Pellentesque habitant morbi tristique senectus et netus et
malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultri-
ces. Phasellus eu tellus sit amet tortor gravida placerat. Integer
sapien est, iaculis in, pretium quis, viverra ac, nunc. Praesent
eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi do-
lor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur
auctor semper nulla. Donec varius orci eget risus. Duis nibh mi,
congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci
sit amet orci dignissim rutrum.
\splitbox
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi.
Morbi auctor lorem non justo. Nam lacus libero, pretium at,
lobortis vitae, ultricies et, tellus. Donec aliquet, tortor sed ac-
cumsan bibendum, erat ligula aliquet magna, vitae ornare odio
metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse
ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis na-
toque penatibus et magnis dis parturient montes, nascetur ridicu-
lus mus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum
turpis. Pellentesque cursus luctus mauris.
\end{InnerBox}
\lipsum[4]
\end{OuterBox}
\lipsum[5-6]

\end{document}


Note that due to \lipsum[n] behavior with respect to the neighboring paragraphs, I inserted normal text in the inner box (as in a normal text file).

• Thanks that was helpful, how does your solution compare to my current workaround tex.stackexchange.com/a/355194/43952 Sep 21, 2020 at 9:52
• The way I understand the problem is the following: having an inner box that must split on two pages, how can we handle it with minimal modification of the .tex file. My point of view is to have the splitting dealt with in the background through one command. This implies, in case some graphical properties are needed (colored frames and/or rounded boxes), to modify the graphical behavior by hand outside the boxes declaration. If not, the call of the inner box has to be modified each time when splitting occurs---the choice is among normal box, split bottom box, and spit upper box. Sep 22, 2020 at 7:41