I’m using mdframed to surround an own environment called {myenv} (for this example). And this environment is setup as a trio list and uses \addxcontentsline to write a list of these environments (I’m using doc only for the MWE).
Problem
The box is broken across pages leaving an empty top part on the first page.
Code/MWE
\documentclass{scrbook}
\usepackage{xparse}
\usepackage[framemethod = TikZ]{mdframed}
\ExplSyntaxOn\makeatletter
% define a style
\mdfdefinestyle { mystyle } {
backgroundcolor = yellow,
innertopmargin = 0.55\baselineskip,
skipabove = 1\baselineskip \@plus 1ex,
}
% define the environment
\NewDocumentEnvironment { myenv } { } {
\list { } { }
\item \relax
\addxcontentsline { toc } { myenv } [ X ] { }% <---------------- [A]
{ \bfseries My~Env~No.~X }% <----------------------------------- [B]
\par \nopagebreak
} {
\endlist
}
% souround it with an md frame
\surroundwithmdframed[ style = mystyle ] { myenv }
%% for testing
%\long\def\protected@write#1#2#3{%
% \begingroup
% \let\uthepage\relax
% #2%
% \let\protect\@unexpandable@protect
% \edef\reserved@a{\write#1{#3}}% <------+---------------------- [C]
% \reserved@a% <-------------------------'
% \endgroup
% \if@nobreak\ifvmode\nobreak\fi\fi
%}
\ExplSyntaxOff\makeatother
\usepackage{lipsum}
\begin{document}
\lipsum[1] \vspace{110mm} \lipsum[2]
\begin{myenv}
\lipsum*[2]
\end{myenv}
\end{document}
It dosen’t happen when I
- remove (or comment out) line marked with
[A]or - remove (or comment out) line marked with
[B](not sure about this one, though) or - switch order of lines
[A]and[B]
Furthermore I traced the problem back to \protected@write: When deleting (commenting out) the two lines marked with [C] the problem disappears too.
Questions
- What is the best solution for this problem? Can I switch
[A]and[B]without problems? - Is this a bug in
mdframedthat should be reported?
Notes
- The original definition for
{myenv}is more complex. - I’m aware of
\newmdenvand\newmdtheoremenvbut I can’t use them for this. - Shifting line
[A]before\listdoesn’t make a difference.

\nopagebreakcommand. – egreg Feb 19 '16 at 22:07