0

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

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

  1. What is the best solution for this problem? Can I switch [A] and [B] without problems?
  2. Is this a bug in mdframed that should be reported?

Notes

  • The original definition for {myenv} is more complex.
  • I’m aware of \newmdenv and \newmdtheoremenv but I can’t use them for this.
  • Shifting line [A] before \list doesn’t make a difference.
  • If I switch A and B, I get a warning about the box not being splittable, caused by the \nopagebreak command. – egreg Feb 19 '16 at 22:07
  • @egreg: Thanks! I overlooked that warning … – Tobi Feb 20 '16 at 12:09
1

The same would arise with \color{green} in place of \addxcontentsline { toc } { myenv } [ X ] { }. Try to issue first \leavevmode after \item\relax.

  • Thanks! But adding it causes the same warning @egreg mentioned above. The box actually is splittable (try \lipsum*[2-10]). Is there a proper way to get rid of the warning? – Tobi Feb 20 '16 at 12:12
  • The warning comes from the \nopagebreak in your environment. If you remove it, there will be no warning, but the box will split. What is you objective ? it seems \nopagebreak achieves exactly what you want, but mdframed is polite enough to tell you it wishes it could have split the box. If you put the box earlier in the page (try \lipsum[1] \vspace{95mm} \lipsum[2]) the box will be split and there will be no warning of mdframed, because \nopagebreak did not contradict what it esteemed to be the best. – user4686 Feb 20 '16 at 12:47
  • The \nopagebreak should prevent the title from being separated from the fist line of the text, therefor I wouldn’t omit it. So the warning doesn’t have anything to do with the \leavevmode and just aries because with added \leavevmode the \nopagebreak is evaluated? – Tobi Feb 20 '16 at 13:03
  • The warning has nothing to do with \leavevmode. You have inserted a \nopagebreak at a location where mdframed wants to split the box in your mwe. It complains about it that's all. Shift the box to some other location and you get no complaints. (a warning is just a warning: your \nopagebreak forces mdframed to leave the previous page with some unfilled area at bottom). – user4686 Feb 20 '16 at 14:47
  • Ok, thanks again! I accept your answer but I’d appreciate if you could add some explanation about why the \leavevmode solves the problem. – Tobi Feb 20 '16 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.