[Tex/LaTex] Is it possible to define a multicolumn environment with a background colour

colormarginsmdframedmulticol

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;
  3. adds a background color;
  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{adjmulticol}

\usepackage{mdframed}
\usepackage{lipsum}

\begin{document}
\lipsum[1]
%\begin{mdframed}[background=yellow]
\begin{adjmulticols}{2}{-1in}{-1in}
\lipsum
\end{adjmulticols}
%\end{mdframed}

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

Best Answer

enter image description here

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
\ifodd\c@page\else\advance\dimen@-2cm\relax\fi
\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