5

I have a problem with the framed environment. I am writing a document (article), and several parts of it have a gray background. Unfortunately, when a new page comes, such a shaded environment is cut in half :(

How can I avoid this? (with no use of \newpage...)

Example: In the document generated by this code, I would like the second shaded environment to be placed in the second page:

\documentclass{article}
\usepackage{framed,color}
\definecolor{shadecolor}{gray}{.75}
\begin{document}
\begin{shaded}
Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text
\end{shaded}
\begin{shaded}
Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text
\end{shaded}
\end{document}
5

Although it sort of defeats the purpose of the framed package, you may enclose every shaded environment within a minipage (of width \linewidth) to prohibit page breaks. In my example I use the etoolbox package to patch the defintion of the shaded environment.

\documentclass{article}
\usepackage{framed,color}
\definecolor{shadecolor}{gray}{.75}
\usepackage{etoolbox}
\BeforeBeginEnvironment{shaded}{\begin{minipage}{\linewidth}}
\AfterEndEnvironment{shaded}{\end{minipage}\par}
\begin{document}
\begin{shaded}
Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text
\end{shaded}
\begin{shaded}
Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text\\Text
\end{shaded}
\end{document}

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.