%% LaTeX2e file `chngpage.sty'
%% generated by the `filecontents' environment
%% from source `changepage' on 2009/10/20.
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PLEASE NOTE:
% THIS PACKAGE IS DEPRECATED IN FAVOUR OF CHANGEPAGE.STY FOR REASONS
% OF COMPATIBILITY WITH THE MEMOIR CLASS. The two are equivalent;
% please do not use this one for new documents or packages.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{chngpage}[2009/10/20 v1.2b change page layout]

%% Note: internal package commands all include the string |cp@|.
%% New \cs{if} for the strict option.
\newif\ifcpstrict
  \cpstrictfalse

%% Declare and process options.
\DeclareOption{strict}{\cpstricttrue}
\ProcessOptions\relax

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% New commands for strict checking of odd/even page.
% Works by writing a label and then checking its pageref.
%
% Rewritten in v1.2 to handle roman, Roman, etc page numbers
% as well as arabic; also eliminates special code to handle
% hyperref. Based on the code in the memoir class.

\newif\ifcpoddpage
\newcounter{cp@cnt}
\newcounter{cp@tempcnt}
\newcommand{\cplabelprefix}{^_}

\gdef\thecp@page{\the\c@page}
\long\def\cp@protected@write#1#2#3{%  % modified \cs{protected@write}
  \begingroup
  \let\thecp@page\relax
  #2%
  \let\protect\@unexpandable@protect
  \edef\reserved@a{\write#1{#3}}%
  \reserved@a
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi}

\DeclareRobustCommand{\checkoddpage}{%
  \cpoddpagefalse
  \stepcounter{cp@cnt}\cplabel{\cplabelprefix\thecp@cnt}%
  \c@cp@tempcnt=\cp@labelref{\cplabelprefix\thecp@cnt}\relax
  \ifodd\c@cp@tempcnt\cpoddpagetrue\fi}

\newcommand{\cplabel}[1]{\@bsphack
  \cp@protected@write\@auxout{}%
    {\string\newcplabel{#1}{\thecp@page}}%
  \@esphack}

\newcommand{\newcplabel}[2]{{\global\@namedef{p@#1}{#2}}}

\newcommand{\cp@labelref}[1]{%
  \expandafter\ifx\csname p@#1\endcsname\relax
    0%  % 0 if there is no label yet in aux file
  \else
    \csname p@#1\endcsname
  \fi}

% End newcommands for strict checking of odd/even page.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Copy of some of the code from the ifmtarg package to save requiring ifmtarg.
\begingroup
\catcode`\Q=3
\long\gdef\@ifmtarg#1{\@xifmtarg#1QQ\@secondoftwo\@firstoftwo\@nil}
\long\gdef\@xifmtarg#1#2Q#3#4#5\@nil{#4}
\endgroup

% Set the page output parameters.
\DeclareRobustCommand{\ch@ngetext}{%
  \setlength{\@colht}{\textheight}\setlength{\@colroom}{\textheight}%
  \setlength{\vsize}{\textheight}\setlength{\columnwidth}{\textwidth}%
  \if@twocolumn%
    \advance\columnwidth-\columnsep \divide\columnwidth\tw@%
    \@firstcolumntrue%
  \fi%
  \setlength{\hsize}{\columnwidth}%
  \setlength{\linewidth}{\hsize}%
}

\DeclareRobustCommand{\changetext}[5]{%
  \@ifmtarg{#1}{}{\addtolength{\textheight}{#1}}%
  \@ifmtarg{#2}{}{\addtolength{\textwidth}{#2}}%
  \@ifmtarg{#3}{}{\addtolength{\evensidemargin}{#3}}%
  \@ifmtarg{#4}{}{\addtolength{\oddsidemargin}{#4}}%
  \@ifmtarg{#5}{}{\addtolength{\columnsep}{#5}}%
  \ch@ngetext%
}

\DeclareRobustCommand{\changepage}[9]{%
  \@ifmtarg{#1}{}{\addtolength{\textheight}{#1}}%
  \@ifmtarg{#2}{}{\addtolength{\textwidth}{#2}}%
  \@ifmtarg{#3}{}{\addtolength{\evensidemargin}{#3}}%
  \@ifmtarg{#4}{}{\addtolength{\oddsidemargin}{#4}}%
  \@ifmtarg{#5}{}{\addtolength{\columnsep}{#5}}%
  \ch@ngetext%
  \@ifmtarg{#6}{}{\addtolength{\topmargin}{#6}}%
  \@ifmtarg{#7}{}{\addtolength{\headheight}{#7}}%
  \@ifmtarg{#8}{}{\addtolength{\headsep}{#8}}%
  \@ifmtarg{#9}{}{\addtolength{\footskip}{#9}}%
}


\newenvironment{adjustwidth}[3][\@empty]{%
  \begin{list}{}{%
    \topsep\z@%
    \listparindent\parindent%
    \parsep\parskip%
    \@ifmtarg{#2}{\setlength{\leftmargin}{\z@}}{\setlength{\leftmargin}{#2}}%
    \@ifmtarg{#3}{\setlength{\rightmargin}{\z@}}{\setlength{\rightmargin}{#3}}%
    \ifx\@empty #1\relax \else
      \cpoddpagefalse
      \ifcpstrict\checkoddpage\else
        \ifodd\c@page\cpoddpagetrue\fi
      \fi
      \ifcpoddpage\else  % even numbered page
        \@ifmtarg{#3}{\setlength{\leftmargin}{\z@}}{\setlength{\leftmargin}{#3}}%
        \@ifmtarg{#2}{\setlength{\rightmargin}{\z@}}{\setlength{\rightmargin}{#2}}%
      \fi
    \fi}
    \item[]}{\end{list}}

\endinput
