spacing=true + ragged2e produces bad minipages

I understand the spacing=true option in microtype is considered "experimental". However, for my document it does improve the appearance of the document -- with the dramatic exception of certain minipages when ragged2e is also used.

\documentclass{article}
\usepackage[document]{ragged2e}
\usepackage{mdframed}

\usepackage[spacing=true]{microtype}

\newenvironment{mybox}{
\begin{minipage}[\textheight]{\linewidth}
\begin{mdframed}
}{
\end{mdframed}
\end{minipage}
}

\usepackage{lipsum}

\begin{document}

\begin{mybox}
\lipsum[1-3]
\end{mybox}

\end{document}


What is the best solution (aside from the default spacing=false)?

• I get no issue if I add the \RaggedRight declaration at the start of the mdframed environment. – egreg Oct 12 '16 at 22:32
• That's true for this example; however, in general that didn't seem to solve the problem. I'll try to produce an example. – Hugh Oct 12 '16 at 22:36
• Well, microtype warns you that with ragged2e something bad can happen. – egreg Oct 12 '16 at 22:45

The microtype package issues a warning:

Package microtype Warning: You are using the ragged2e' package.
(microtype)                undesired results when used with ragged2e'.
(microtype)                In this case, disable the spacing' option.


However, there's no wrong output if I restate \RaggedRight at the beginning of the mdframed environment.

\documentclass{article}
\usepackage[document]{ragged2e}
\usepackage{mdframed}

\usepackage[spacing=true]{microtype}

\newenvironment{mybox}
{\begin{minipage}[\textheight]{\linewidth}
\begin{mdframed}\RaggedRight}
{\end{mdframed}\end{minipage}}

\usepackage{lipsum}

\begin{document}

\noindent
\begin{mybox}
\lipsum[1-3]
\end{mybox}

\end{document}


It's not clear what's the role of the minipage wrapper, though.

If you want that the framed box is not split across pages, just add the nobreak option.

\newenvironment{mybox}
{\begin{mdframed}[nobreak]\RaggedRight}
{\end{mdframed}}


This also avoids the need of \noindent`.