# Bug with framed, listings and xcolor are together

When using a combination of the packages framed, listings, and xcolor, that causes a strange bug, that I cannot manage to fix.

The problem is that, when a box (made with the framed package, and fcolorbox) contains a listing just before a page breaks, the rest of the block appears blank on the next page. Moreover, the block on the firxt page is not framed anymore.

This does not happens if I use fbox instead of fcolorbox.

The minimal example is the following:

    \documentclass[a4paper,12pt]{scrartcl}

\usepackage[english]{babel}
\usepackage{blindtext}

\usepackage{framed}
\usepackage{listings}
\usepackage{xcolor}

\lstnewenvironment{python}{\lstset{language=python}}{}

\renewcommand*{\FrameCommand}{%
\setlength{\fboxrule}{\FrameRule}\setlength{\fboxsep}{\FrameSep}%
\fcolorbox{black}{white}}

\begin{document}
\blindtext
\blindtext
\blindtext

\begin{framed}{Block}
\blindtext

Foo

\blindtext

\begin{python}
def f(x):
foo
\end{python}
THIS NEVER APPEARS
\blindtext
\end{framed}
End of block
\end{document}


What I am looking for is any kind of workaround, not necessarily a full solution.

-

Adding \color{black} after \end{python} solves the problem (but a color stack warning appears). It seems that somehow the "white" background becomes also the text color after the python environment.

## EDIT

Use mdframed. A simple

\begin{mdframed}[skipabove=\topskip,skipbelow=\topskip]
...
\end{mdframed}


produces what you are looking for. The environment is customizable in many ways.

-
Haha, brilliant! This is indeed what is happening: the text after the page break becomes the background colour! Thanks! – Olivier Jul 21 '11 at 14:04
There is still the issue of the framing, though. The frame on the first page disappears... – Olivier Jul 21 '11 at 14:06
Sorry. It has to do with the inner workings of framed that gathers its contents and all the catcode tricks of listings cannot be performed properly. I believe that mdframed can do a better job. – egreg Jul 21 '11 at 14:20
Thanks, this is brilliant. Exactly the right solution. This package mdframed seems much more reliable than framed. I also prefer the result. – Olivier Jul 21 '11 at 17:05