# Is it possible to define a multicolumn environment with a background colour

EDIT: I should have given a MWE based on memoir (which I am using) where the inner and outer margins are initially different. I would like the margins to be the same in that new environment. (Inner margin is the left margin on odd number pages and the right margin on even number pages ... or the other way around.)

I would like to be able to define an environment that

1. changes the width of the text;
2. changes the number of columns;
4. works across page boundaries.

So far, the best I can get is three out of four; items 2 and 3 seem to be non-compatible choices.

Here's a MWE which shows the intent. (Unfortunately, like the documentation states, adjusmulticols and mdframed are not compatible.)

\documentclass{article}

\usepackage{mdframed}
\usepackage{lipsum}

\begin{document}
\lipsum[1]
%\begin{mdframed}[background=yellow]
\lipsum
%\end{mdframed}

\begin{mdframed}[backgroundcolor=yellow,leftmargin=-1,rightmargin=-1]
\lipsum
\end{mdframed}
\end{document}

-

the standard multicol can cope with the page margins being locally changed, so you just need to drop in a coloured rule at the point it is assembling the columns, something like this:

\documentclass{article}
\usepackage{multicol}

\usepackage{color}

\makeatletter

\newenvironment{wider}
{\list{}{\leftmargin-1cm\relax\rightmargin\leftmargin}%
\item\relax}
{\endlist}

\def\foo#1\full@width#2#3\foo{%
\def\page@sofar{%
#1%
\full@width{%
{\color{yellow}\vrule \@width \full@width}%
\kern-\full@width
#2}%
#3}}
\expandafter\foo\page@sofar\foo

\usepackage{lipsum}

\begin{document}\color{black}
\lipsum[1]

\begin{wider}
\begin{multicols}{2}
\lipsum
\end{multicols}
\end{wider}

\begin{multicols}{3}
\lipsum
\end{multicols}

\end{document}


UPDATE

Or a minor variation, as requested in the comments, just using memoir in the example, and just making the environment wider on one side, and shifting the rule depending on the page

\documentclass{memoir}
\usepackage{multicol}

\usepackage{color}

\makeatletter

\newenvironment{wider}
{\list{}{\leftmargin0pt\relax
\rightmargin-2cm\relax}%
\item\relax}
{\endlist}

\def\foo#1\moveright#2\full@width#3#4\foo{%
\def\page@sofar{%
#1%
\dimen@\multicol@leftmargin
\moveright\dimen@
\hbox to \full@width{%
{\color{yellow}\vrule \@width \full@width}%
\kern-\full@width
#3}%
#4}}
\expandafter\foo\page@sofar\foo


.... rest as before

-
Since I plan to use memoir instead of article (I should have done that in my MWE), how do I modify this to have the same left and right margins regardless if the page is even or odd. I tried to look up \list in my (arguably very old) copies of the TeXbook and the LaTeX user guide but did not find it. Google is not helping either... –  André Apr 4 '12 at 17:23
You absolutely need to use adjmulticols for that, but the relevant code by David is in the \page@sofar hack, so this should hopefully also work with adjmulticols. –  Stephan Lehmke Apr 4 '12 at 17:27
I;m not sure I understand the comment about margins, in that wider environment (which is just the code for the standard latex quote env but with negative rather than positive offset) I just took a cm off each margin, so whatever relationship between the margins and odd and even pages held before should still hold, apart from being reduced? –  David Carlisle Apr 4 '12 at 18:39
With memoir and the twoside option, I have a larger outer margin than inner one (say by 2in), basically to leave room for notes in the margin. With [adjmargincol] I can do \begin{adjmulticols}{3}{0in}{-2in} and the multicolumn text will now take the full width of the page, either intruding on the left or right margin depending if the page is odd or even (or the other way?), and effectively have both margins the same size. –  André Apr 4 '12 at 18:56
added memoir example to my answer –  David Carlisle Apr 4 '12 at 19:28

A good package to have a look is the tcolorbox, although sadly also does not break across pages.

\documentclass{article}
\usepackage{multicol,lipsum}
\usepackage{tcolorbox}
\begin{document}
\begin{tcolorbox}[width=15cm]
\begin{multicols}{3}
\lipsum[1-3]
\end{multicols}
\end{tcolorbox}
\end{document}

-
I may be forced to use this (plus manual editing...) but I really would like to have it break across pages. –  André Apr 4 '12 at 17:03
@André Personally I dislike boxes breaking across pages. Normally you box something to make it stand-out, so you do not want to split it. –  Yiannis Lazarides Apr 4 '12 at 17:13
I would normally agree... What I want to use this for is to shade out worked-out examples in physics and math notes, reproducing the look often found in introductory textbooks. The shading here is to de-emphasize the examples, enabling the student to quickly jump over if they so desire. –  André Apr 4 '12 at 17:26
@André Have a look at Steward's Calculus to see a better scheme. –  Yiannis Lazarides Apr 4 '12 at 17:51
Thanks for the pointer. –  André Apr 4 '12 at 20:32

Apparently, another solution is boites with some pstrick magic as demonstrated here. Unfortunately, I have not been able to reproduce it as I get a Non-PDF special ignored! error message (using pdflatex).

-