# Problem with odd/even margins when environment crosses page boundaries

In this question, I described a problem I have with marginpars appearing on the wrong side of the page when they occur near a page break. I ran into a different, but perhaps related problem described below. I have a non-LaTeX related solution (also described below) that I include as a workaround in case others may find it useful, but I would like very much to have a pure LaTeX solution.

I want to print my course notes as a book, with double-sided paper. [I am using the memoir class.] I also want to have text or figures in the "outer" (i.e. wide) margin - and that margin should change side depending if the page is odd or even.

I also want to be able to change from single column (with a wide margin) to double column (with narrow margins) to include either worked out examples and/or problems at the end of each chapter. The problem I encountered is that the margin adjustment done (correctly) at the beginning of the double-column environment is totally wrong on the subsequent page if the environment crosses a page boundary.

Here is a MWE - actually, three MWE in one. Depending on which options get commented out, one can have the normal two-sided memoir output, or a one-sided output with the wide margin either on the left-hand side or the right hand side. Note that there are two places where commented out options occur: right at the beginning and also in the page layout "section" of the preamble.

%%%%% two-sided option with wide margin alternating sides
%   \documentclass[openany,twoside]{memoir}

%%%%  one sided - wide margin (for marginpar) on the right
%   \documentclass[openany,oneside]{memoir}

%%% one sided - wide margin (for marginpar) on the left
\documentclass[openany,oneside]{memoir}
\marginparmargin{left}

%%% page layout dimensions for memoir
\setstocksize{11in}{8.5in}
\settrimmedsize{\stockheight}{\stockwidth}{*}
\settypeblocksize{9.0in}{5in}{*}
\setulmargins{1.0in}{*}{*}
\setmarginnotes{0.2in}{1.8in}{1cm}
%%% marginpar on right OR twosided
% \setlrmargins{0.75in}{*}{*}
%%% marginpar on left
\setlrmargins{2.75in}{*}{*}
\checkandfixthelayout
%

\usepackage{multicol}
\usepackage{lipsum}

\newcommand{\sampletext}{This is a short sample text
longer than one line but shorter than a typical paragraph
from the lipsum package.}
\newenvironment{TwoCol}{\begin{widematter}\begin{multicols}{2}}{\end{multicols}\end{widematter}}

\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{First Chapter}
\section{First section}
\lipsum[1]\marginpar{\sampletext}
\lipsum[2]
\begin{TwoCol}
\lipsum[5]
\end{TwoCol}
\section{Second section}
\lipsum[3-4]\marginpar{\sampletext}

\chapter{Second Chapter}
\section{First section}
\lipsum[1]\marginpar{\sampletext}
\lipsum[2-6]\marginpar{\sampletext}
\lipsum[7]
\begin{TwoCol}
\lipsum[1-10]
\end{TwoCol}
\lipsum[4-8]\marginpar{\sampletext}

\end{document}


The non-LaTex solution I have found is to have two one-sided "base documents" (for lack of a better term), one that has the preamble for the wide margin on the left, the other for the wide margin on the right, and keeping most of the content in separate files that get inputted. I then process both the "left" document and the "right" document, and combine them with this Python script:

from pyPdf import PdfFileWriter, PdfFileReader

output = PdfFileWriter()

total_nb_pages = left.getNumPages()
assert left.getNumPages() == right.getNumPages()

for page in range(0, total_nb_pages, 2):
if page + 1 < total_nb_pages:

outputStream = open("combined-test.pdf", "wb")
output.write(outputStream)
outputStream.close()


So far, based on limited tests, it does work ... but a pure LaTeX solution would be much appreciated.

-

AFAICS, the only problem with the two-sided version is that the adjustwidth* environment doesn't work over page breaks.

In fact, the implementation of adjustwidth* is not intended to accomodate page breaks, so you need something different.

\usepackage{adjmulticol}


I could define

\newenvironment{TwoCol}{\small\begin{adjmulticols}{2}{0in}{-2in}}{\end{adjmulticols}}


(widematter is no longer needed).

This seems to give an acceptable result (using the twoside version):

-
Sorry. Changed wording. –  Stephan Lehmke Apr 3 '12 at 6:08
Thanks Stephan (+1) –  morbusg Apr 3 '12 at 6:11