# mdframed: splittopskip ignored for center environment

I have a lot of example environments using mdframed in my document that break across pages. In these enviroments, I have some tables of results that I currently place in a center environment. Unfortunately though, when these tables appear directly after a page-break, the skip set in skiptopskip is ignored.

I'm looking for a general solution to this problem, or perhaps even an alternative to the center environment that looks the same, but does not encounter this problem.

MWE:

\documentclass{article}
\usepackage[paperwidth=12cm, paperheight=6cm]{geometry}
\usepackage{mdframed}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{blindtext}

\begin{document}
\begin{mdframed}[splittopskip=20pt,backgroundcolor=black!5]
\blindtext

\begin{center}
\begin{tabular}{ll}
\toprule
A & 1 \\
B & 2 \\
\bottomrule
\end{tabular}
\end{center}
\end{mdframed}

\end{document}


Note that the second page respects the skiptopsep but the third page does not.

• Use tcolorbox instead of mdframed. Much more reliable. Sep 2 '18 at 18:12
• Unfortunately I have made a custom environment with mdframed used in hundreds of examples. I would prefer a solution for mdframed if at all possible. Sep 17 '18 at 16:55

There are two issues, neither of which is related to center. One can verify this by commenting out the center environment...the tabular moves to the left, but vertical space is unchanged.

So, what are the issues?

1. The tabular is vertically center-aligned. Try instead \begin{tabular}[t].... That restores top alignment, but with that alone, the space then becomes excessive:

1. The excessive spacing issue appears to be related to the spacing above \toprule. Here is what you get if do this X\begin{tabular}[t]{ll}\toprule...\end{tabular}. The rule is aligned with the baseline, rather than the top of the letter-space, thus creating extra vertical space.

My fix is to use stackengine to tell it how to place the complete tabular relative to the baseline.

\documentclass{article}
\usepackage[paperwidth=12cm, paperheight=6cm]{geometry}
\usepackage{mdframed}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{blindtext}
\usepackage{stackengine}
\begin{document}
\begin{mdframed}[splittopskip=20pt,backgroundcolor=black!5]
\blindtext

\begin{center}
\belowbaseline[-\ht\strutbox]{\begin{tabular}[t]{ll}
\toprule
A & 1 \\
B & 2 \\
\bottomrule
\end{tabular}}
\end{center}
\end{mdframed}

\end{document}


In an effort to make it easier for the OP to back-modify an existing document, I wrapped the above approach into a new environment, qtabular. Thus, to fix instances of the problem, the environment merely needs to be changed from tabular to qtabular.

\documentclass{article}
\usepackage[paperwidth=12cm, paperheight=6cm]{geometry}
\usepackage{mdframed}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{blindtext}
\usepackage{environ,stackengine}
\NewEnviron{qtabular}[1]{%
\def\tmp{\begin{tabular}[t]{#1}}%
\belowbaseline[-\ht\strutbox]{\expandafter\tmp\BODY\end{tabular}}%
}
\begin{document}
\begin{mdframed}[splittopskip=20pt,backgroundcolor=black!5]
\blindtext

\begin{center}
\begin{qtabular}{ll}
\toprule
A & 1 \\
B & 2 \\
\bottomrule
\end{qtabular}
\end{center}
\end{mdframed}

\end{document}

• This works well, thanks! But any suggestion on how I might implement something in the document head that would not require the explicit \belowbaseline command before each tabular? (There are hundreds in the document.) Sep 17 '18 at 18:25
• @badroit No quick fix jumps to mind. Remember, you are going to have to add the [t] qualifier to all affected tabulars anyway...so I don't think you can avoid some level of individual editing. Sep 17 '18 at 18:49
• @badroit You could change affected tabulars into qtabulars with this preamble code: \usepackage{environ,stackengine} \NewEnviron{qtabular}[1]{% \def\tmp{\begin{tabular}[t]{#1}}% \belowbaseline[-\ht\strutbox]{\expandafter\tmp\BODY\end{tabular}}% }. Invocation is the same, except tabular becomes qtabular. Sep 17 '18 at 18:56
• @badroit I have provided the above code in an ADDENDUM to my answer. Sep 18 '18 at 11:17

Imho, whatever environment you have defined with mdframed you can create it with tcolorbox too - minus the problems you have with mdframed:

\documentclass{article}
\usepackage[paperwidth=12cm, paperheight=6cm]{geometry}
\usepackage[most]{tcolorbox}
\newtcolorbox{mdframed}{enhanced,breakable,sharp corners,colback=black!5,topsep at break=20pt,boxrule=0.4pt}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{blindtext}
\usepackage{stackengine}
\begin{document}
\begin{mdframed}
abc\\abc\\\blindtext

\begin{center}
\begin{tabular}[t]{ll}
\toprule
A & 1 \\
B & 2 \\
\bottomrule
\end{tabular}
\end{center}
\end{mdframed}

\end{document}