Define an environment with appointed color background

colorenvironments

I want to define an environment with appointed color, that is to say the whole environment from the \begin{env} to \end{env} will be drawed by the color. Just like a code block. Roughly, this is waht I expect.

enter image description here

I tried to use \colorbox, but it seemed that the box didn't support for multiple lines of text. For instance, I have a costomized environment myenv, then how can I realize what I expect.

\documentclass{article}

\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{framed}

\newenvironment{myenv}{%
    %\colorbox{gray}
    \medskip
    \begin{quote}
        \centering
}{\end{quote}\medskip}

\begin{document}
    \section{A}
    \lipsum{1}
    \begin{myenv}
        \lipsum{2}
    \end{myenv}
    \lipsum{3}\par
    {\centering
    \colorbox{gray}{AAAAAA}
\end{document}

Please give me an example. Thanks.

Best Answer

If you want to stay with framed.sty package, then try with the below MWE:

\documentclass{article}

\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{framed}
\definecolor{shadecolor}{cmyk}{0,0,0,0.5}%change what ever you like

\newenvironment{myenv}{%
    %\colorbox{gray}
    \medskip
    \begin{shaded*}
        \centering
}{\end{shaded*}\medskip}

\begin{document}
    \section{A}
    \lipsum{1}
    \begin{myenv}
        \lipsum{2}
    \end{myenv}
    \lipsum{3}\par
    {\centering
    \colorbox{gray}{AAAAAA}
\end{document}

Apart from this, tcolorbox.sty and mdframed.sty also having nice features...