# Erratic behavior of mdframed environment right after headings

First of all, I have seen and read this question it appears to be a different albeit potentially related issue.

I'm using mdframed 1.9b from 2013 with LuaLaTeX.

Now the problem I encounter can be reproduced with the following MWE.

\documentclass[a4paper,fontsize=12pt,twoside=false,onecolumn,final,openany,parskip=full,chapterprefix=false]{scrreprt}
\usepackage[framemethod=tikz]{mdframed}
\mdfsetup{nobreak=true}
\def\a{Here goes some really boring text which we use to fill the paragraph, but which is shorter than the standard lipsum and blindtext paragraph. }

\begin{document}

\chapter{chapter title}

\begin{mdframed}
some text box
\end{mdframed}

\a

\section{section title}

\begin{mdframed}
some text box
\end{mdframed}

\a

\subsection{subsection title}

\begin{mdframed}
some text box
\end{mdframed}

\a

\chapter{chapter title}

Now with some text prior to the \texttt{mdframed} environment. To show the effect of that on vertical spacing.

\begin{mdframed}
some text box
\end{mdframed}

\a

\section{section title}

Now with some text prior to the \texttt{mdframed} environment. To show the effect of that on vertical spacing.

\begin{mdframed}
some text box
\end{mdframed}

\a

\subsection{subsection title}

Now with some text prior to the \texttt{mdframed} environment. To show the effect of that on vertical spacing.

\begin{mdframed}
some text box
\end{mdframed}

\a

\end{document}


Let me show in screenshots. I have a document in which I use the mdframed environment to refer to documentation right after the heading for a section. Now contrast the vertical position of the mdframed boxes in the following two examples:

Now I'm not sure whether this is a matter of taste on my part, since I know LaTeX puts a lot of effort into spacing all over the place. But I find it very odd that in the first example the mdframed box is essentially glued right to the heading, followed by the vertical skip that seems to belong to the heading; whereas in the second example the vertical spacing looks more sane.

How can I get mdframed to yield something similar to the vertical spacing in the second example, even though the mdframed environment comes right after the section heading? Or is there perhaps an alternative that I could use instead of mdframed?

Edit: incidentally \begin{mdframed}[skipabove=\baselineskip] (even 1em or 1ex instead of \baselineskip) also has no effect right below section headings.

• mdframed doesn't start horizontal mode and so your parskip is lost. You could try startcode=\leavevmode, but on the whole I would advise to use tcolorbox. – Ulrike Fischer Jun 22 '15 at 11:39
• @UlrikeFischer: thank you. I noticed, however, that tcolorbox introduces "badness" as every single instance introduces Overfull \hbox (10.0pt too wide) in paragraph. So I got the feeling tcolorbox may not be the appropriate workaround. As for the setup, I only use \tcbset{sharp corners, colback=white, colframe=black, boxrule=0.4pt} (derived from the answer below). – 0xC0000022L Jun 22 '15 at 12:03
• tcolorbox is much better and much more reliable than mdframed. The overfull box is the \parfillskip. Use \parskip=full-. Or check tex.stackexchange.com/questions/187339/… (for boxes after a sectioning command one must set the key before nobreak in newer tcolorbox versions.) – Ulrike Fischer Jun 22 '15 at 12:16

May be you can change to tcolorbox. The results for your example look better:

\documentclass[a4paper, fontsize=12pt, twoside=false, onecolumn, final, openany, parskip=full, chapterprefix=false]{scrreprt}
\usepackage{tcolorbox}

\def\a{Here goes some really boring text which we use to fill the paragraph, but which is shorter than the standard lipsum and blindtext paragraph. }

\tcbset{sharp corners, colback=white, colframe=black, boxrule=1pt}

\begin{document}

\chapter{chapter title}

\begin{tcolorbox}
some text box
\end{tcolorbox}

\a

\section{section title}

\begin{tcolorbox}
some text box
\end{tcolorbox}

\a

\subsection{subsection title}

\begin{tcolorbox}
some text box
\end{tcolorbox}

\a

\chapter{chapter title}

Now with some text prior to the \texttt{mdframed} environment. To show the effect of that on vertical spacing.

\begin{tcolorbox}
some text box
\end{tcolorbox}

\a

\section{section title}

Now with some text prior to the \texttt{mdframed} environment. To show the effect of that on vertical spacing.

\begin{tcolorbox}
some text box
\end{tcolorbox}

\a

\subsection{subsection title}

Now with some text prior to the \texttt{mdframed} environment. To show the effect of that on vertical spacing.

\begin{tcolorbox}
some text box
\end{tcolorbox}

\a

\end{document}

• thanks, is there a way to emulate what \surroundwithmdframed[]{lstlisting} does with tcolorbox? Also, does tcolorbox ensure that it doesn't break the environment? I have been using mdframed with \mdfsetup{nobreak=true} – 0xC0000022L Jun 22 '15 at 9:38
• @0xC0000022L No, tcolorbox doesn't have this command, but it has an special environment called tcblisting to format color boxes for listings. It works with listings and minted. I think non breakable boxes is tcolorbox default. – Ignasi Jun 22 '15 at 9:49
• thanks a lot. That helps me right now. I'll still hold out for other answers, since I reckon this might be a defect in the mdframed package. I saw its authors participate on this site, so it's possible they'll also weigh in. – 0xC0000022L Jun 22 '15 at 9:56