2

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}

mdframed problem with page breaks and center

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

2
  • 1
    Use tcolorbox instead of mdframed. Much more reliable. – Ulrike Fischer 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. – badroit Sep 17 '18 at 16:55
3
+100

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:

enter image description here

  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.

enter image description here

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}

enter image description here

ADDENDUM

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}
4
  • 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.) – badroit Sep 17 '18 at 18:25
  • 1
    @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. – Steven B. Segletes Sep 17 '18 at 18:49
  • 1
    @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. – Steven B. Segletes Sep 17 '18 at 18:56
  • @badroit I have provided the above code in an ADDENDUM to my answer. – Steven B. Segletes Sep 18 '18 at 11:17
2

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}

enter image description here

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.