[Tex/LaTex] Different \marginparsep for odd and even pages

double-sidedmarginparspacing

What is the most robust way of increase the effective \marginparsep on even pages only?

I have been playing with outdented lists, section heads etc. which leads to an issue with margin notes: I need to increase the effective \marginparsep on even pages to allow for the outdents. Since this is a length, I cannot simply include a macro such as KOMA's \ifthispageodd.

If possible, I would like a solution that works with the standard \marginpar as well as with marginnote or similar packages.

Update:

Werner's answer below almost solves the problem, but there is a bug closely related to that fixed by mparhack. This MWE demonstrates the problem:

\documentclass[twoside]{scrbook}
\usepackage{etoolbox}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\usepackage{xifthen}% http://ctan.org/pkg/xifthen
\usepackage{mparhack}

\begin{document}
\EveryShipout{%
  \ifthenelse{\isodd{\value{page}}}%
    {\global\marginparsep=22pt}% Next page is even
    {\global\marginparsep=11pt}% Next page is odd
}
\lipsum[1]
\marginpar[This is a paragraph in the left margin]{This is a paragraph in the right margin}
\lipsum[2]

\begin{itemize}
  \def\do#1{\item This is a line #1 and should have a note with the same
    number:\marginpar{Note #1 and a bit.}}
  \docsvlist{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
\end{itemize}

\lipsum[3-5]
\marginpar[This is a paragraph in the left margin]{This is a paragraph in the right margin}
\lipsum[6-8]
\marginpar[This is a paragraph in the left margin]{This is a paragraph in the right margin}
\lipsum[9-10]
\end{document}

enter image description here

Note 17 at the top of the second page does not receive the proper adjustment. If the mparhack package is not used, then this note also appears on the wrong side of the page, so the full solution probably involves somehow applying a similar patch. Is there a simple way of doing this?

Best Answer

I would think that accessing the page number at shipout would be robust enough. For this, use the everyshi package. Here is a small trial run:

\documentclass[twoside]{scrartcl}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\usepackage{xifthen}% http://ctan.org/pkg/xifthen

\begin{document}
\EveryShipout{%
  \ifthenelse{\isodd{\value{page}}}%
    {\global\marginparsep=22pt}% Next page is even
    {\global\marginparsep=11pt}% Next page is odd
}

\lipsum[1]
\marginpar[This is a paragraph in the left margin]{This is a paragraph in the right margin}
\lipsum[2-5]
\marginpar[This is a paragraph in the left margin]{This is a paragraph in the right margin}
\lipsum[6-8]
\end{document}

Different marginparsep values for odd/even pages

The key is to make the change \global within \EveryShipout, since it is grouped. You could apply similar modification to the \marginparwidth.