4

I've run into a problem in a bigger documents where pictures get in between the breaks of mdframed boxes. So the scenario is that the mdframed box should break at pageend if it's to large and not fitting and images should be positioned automatically.

But the image should not be in between the two parts of the mdframed box. So the mdframed should either be before or after the image (i.e. a page is allowed to break a mdframed, but an image should not be allowed).

Is there any way to change either the pagebreaking-and-positioning of mdframed or the positioning of images accordingly? So something like giving the mdframed float "priority" over the image float. (Of course I could always make an exception for a single special "wrongly" positioned image and position it e.g. "[b]", but I'm looking for an automatic option).

MWE and screenshot below.

Kind regards and thanks for your time,

NextThursday

\documentclass{scrbook}
\usepackage{lipsum}
\usepackage{mdframed}
\usepackage[demo]{graphicx}

\begin{document}\mainmatter\chapter{MWE}
\lipsum[1-3]
\begin{figure}\centering{\includegraphics[width=8cm,height=5cm]{exampleimg}\caption{exampleimg}}\end{figure}
\begin{mdframed}
\lipsum[4-6]
\end{mdframed}%

\end{document}

MWE screenshot

UPDATE

As there doesn't seem to be much solutions this update should document (IMHO inferior) possible fixes, in case somebody stumbles over this question and the hot fixes solve their problem:

  • mdframed has an option like \mdfsetup{nobreak=true} to prevent the boxes from spanning multiple pages
  • figure positioning is discussed a lot already and has good answers

One possible idea would be to use a \FloatBarrier (pkg:placeins) before each mdframed-box, although that is not as intelligent as it should be. It might break more than it fixes: a very small mdframed box which might fit into the remainder of the page is forbidden and the bigger image which doesn't fit results in unused remainder of the first page. The MWE for this would be:

\documentclass{scrbook}
\usepackage{lipsum}\usepackage{mdframed}\usepackage[demo]{graphicx}

\usepackage{placeins}

\newenvironment{mymdframed}{\FloatBarrier\begin{mdframed}}{\end{mdframed}}

\begin{document}\mainmatter\chapter{MWE}
\lipsum[1-3]
\begin{figure}\centering{\includegraphics[width=8cm,height=5cm{exampleimg}\caption{exampleimg}}\end{figure}
\begin{mymdframed}\lipsum[4-6]\end{mymdframed}%
\lipsum[4-16]
\end{document}
2

With a bit of care you can set the float placement parameters to prevent floats within the region, here I set totalnumber to 0. It is a global setting so you need to explicitly set it back at the end, you can not use a group to restore things. Also because of its global nature and the timing of the page breaking sometimes a bit of care needs to be taken into exactly where it is set. But in this case the "obvious" places of immediately surrounding the environment works:

enter image description here

\documentclass{scrbook}
\usepackage{lipsum}
\usepackage{mdframed}
\usepackage[demo]{graphicx}

\begin{document}\mainmatter\chapter{MWE}
\lipsum[1-3]
\begin{figure}\centering{\includegraphics[width=8cm,height=5cm]{exampleimg}
\caption{exampleimg}}\end{figure}

\setcounter{totalnumber}{0}
\begin{mdframed}
\lipsum[4-6]
\end{mdframed}%
\setcounter{totalnumber}{3}

\end{document}
|improve this answer|||||

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.