%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%
% This is file 'changelayout.sty', version 0.1.                              %
%                                                                            %
% This package can be used to change page and text layout on the fly,        %
% (ie, for specific pages, and, of course, for the entire document).         %
% It is an extension of Peter Wilson's 'changepage' package.                 %
%                                                                            %
% Copyright (c) 2009 Ahmed Musa (a.musa@rocketmail.com).                     %
%                                                                            %
% This work may be distributed and/or modified for commercial and            %
% non-commercial purposes.                                                   %
%                                                                            %
% The LPPL maintenance status of this software is 'author-maintained'.       %
%                                                                            %
% This software is provided 'as it is', without warranty of any kind,        %
% either expressed or implied, including, but not limited to, the            %
% implied warranties of merchantability and fitness for a particular         %
% purpose.                                                                   %
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%
\ProvidesPackage{changelayout}[2009/06/25 v0.1 Change page layout on the fly]
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{etextools}
\RequirePackage{xkeyval}
\RequirePackage{ltxnew}
\newrobustcmd\cpl@warn[1]{\PackageWarningNoLine{changelayout}{#1}}
\newrobustcmd\cpl@info[1]{\PackageInfo{changelayout}{#1\@gobble}}
\newrobustcmd\cpl@err[1]{\PackageError{changelayout}{#1}{\@ehc}}
\new\let\cpl@xp\expandafter
\@ifclassloaded{memoir}{%
  \cpl@err{This package can't be used with\MessageBreak
    memoir class. I've therefore exited}
  \cpl@xp\endinput%
}{}
\@ifpackageloaded{xcolor}{%
  \cpl@info{You've already loaded 'xcolor'\MessageBreak
    package with options that possibly\MessageBreak
    differ from my options.\MessageBreak
    I've not reloaded 'xcolor'}
}{%
  \RequirePackage[dvipsnames*,svgnames*,x11names*,table,hyperref]{xcolor}
  \cpl@info{'xcolor' package loaded with\MessageBreak
    options 'dvipsnames*', 'svgnames*',\MessageBreak
    'x11names*', 'table', 'hyperref'}
}
\AtEndPreamble{%
  \@ifpackageloaded{changepage}{%
    \cpl@err{This package is a replacement for\MessageBreak
      'changepage' package.\MessageBreak
      It can't be used with 'changepage'}
    \cpl@xp\endinput%
  }{}
}
\newrobustcmd*\cpl@numdef[2]{%
  \ifstrempty{#2}{\cpl@err{You can't assign empty argument\MessageBreak
    to number}}{}
  \ifundef{#1}{\new\count#1\relax #1=\z@\relax}{}%
  #1=\numexpr#2\relax
}
\newrobustcmd*\cpl@csnumdef[1]{\cpl@xp\cpl@numdef\csname#1\endcsname}
\newrobustcmd*\cpl@dimdef[2]{%
  \ifstrempty{#2}{\cpl@err{You can't assign empty argument\MessageBreak
    to dimension}}{}
  \ifundef{#1}{\new\dimen#1\relax #1=\z@\relax}{}%
  #1=\dimexpr#2\relax
}
\newrobustcmd*\cpl@csdimdef[1]{\cpl@xp\cpl@dimdef\csname#1\endcsname}
\newrobustcmd*\cpl@dimadd[2]{%
  \ifstrempty{#2}{\cpl@err{Why do you want to add empty\MessageBreak
    argument to dimension?}}{}
  \ifundef{#1}{\cpl@err{You can't add to undefined dimension}}{}
  #1=\dimexpr#1+#2\relax
}
\newrobustcmd*\cpl@csdimadd[1]{\cpl@xp\cpl@dimadd\csname#1\endcsname}
\newrobustcmd\cpl@changelayout{%
  \@colht=\textheight%
  \@colroom=\textheight%
  \vsize=\textheight%
  \columnwidth=\textwidth%
  \if@twocolumn
    \cpl@csdimdef{dimen@}{(\columnwidth-\columnsep)/2}
    \columnwidth=\dimen@%
    \@firstcolumntrue
  \fi
  \hsize=\columnwidth%
  \linewidth=\hsize%
}
\newrobustcmd\cpl@getdefaults{\begingroup
  \protected@edef\@tempa{\endgroup
    \noexpand\cpl@csdimdef{cpl@orig@paperwidth}{\paperwidth}%
    \noexpand\cpl@csdimdef{cpl@orig@paperheight}{\paperheight}%
    \noexpand\cpl@csdimdef{cpl@orig@textheight}{\textheight}%
    \noexpand\cpl@csdimdef{cpl@orig@textwidth}{\textwidth}%
    \noexpand\cpl@csdimdef{cpl@orig@oddsidemargin}{\oddsidemargin}%
    \noexpand\cpl@csdimdef{cpl@orig@evensidemargin}{\evensidemargin}%
    \noexpand\cpl@csdimdef{cpl@orig@topmargin}{\topmargin}%
    \noexpand\cpl@csdimdef{cpl@orig@headsep}{\headsep}%
    \noexpand\cpl@csdimdef{cpl@orig@headheight}{\headheight}%
    \noexpand\cpl@csdimdef{cpl@orig@footskip}{\footskip}%
    \noexpand\cpl@csdimdef{cpl@orig@footins}{\skip\footins}%
    \noexpand\cpl@csdimdef{cpl@orig@marginparwidth}{\marginparwidth}%
    \noexpand\cpl@csdimdef{cpl@orig@marginparsep}{\marginparsep}%
    \noexpand\cpl@csdimdef{cpl@orig@marginparpush}{\marginparpush}%
    \noexpand\cpl@csdimdef{cpl@orig@columnsep}{\columnsep}%
    \noexpand\cpl@csdimdef{cpl@orig@columnseprule}{\columnseprule}%
    \noexpand\cpl@csdimdef{cpl@orig@linewidth}{\linewidth}%
    \noexpand\cpl@csdimdef{cpl@orig@hoffset}{\hoffset}%
    \noexpand\cpl@csdimdef{cpl@orig@voffset}{\voffset}%
    \noexpand\cpl@csnumdef{cpl@orig@mag}{\mag}%
  }\@tempa%
}
\@onlypreamble\cpl@getdefaults\relax
\AtEndPreamble{\cpl@getdefaults}%
\newcommand\cpl@print@defaultparams{%
  ^^J*******************************************************^^J%
  Original page layout dimensions and booleans^^J%
  (changelayout package)^^J%
  \string\paperwidth\space\space\the\cpl@orig@paperwidth^^J%
  \string\paperheight\space\space\the\cpl@orig@paperheight^^J%
  \string\textwidth\space\space\the\cpl@orig@textwidth^^J%
  \string\textheight\space\space\the\cpl@orig@textheight^^J%
  \string\oddsidemargin\space\space\the\cpl@orig@oddsidemargin^^J%
  \string\evensidemargin\space\space\the\cpl@orig@evensidemargin^^J%
  \string\topmargin\space\space\the\cpl@orig@topmargin^^J%
  \string\headheight\space\space\the\cpl@orig@headheight^^J%
  \string\headsep\space\space\the\cpl@orig@headsep^^J%
  \string\footskip\space\space\the\cpl@orig@footskip^^J%
  \string\marginparwidth\space\space\the\cpl@orig@marginparwidth^^J%
  \string\marginparsep\space\space\the\cpl@orig@marginparsep^^J%
  \string\columnsep\space\space\the\cpl@orig@columnsep^^J%
  \string\columnseprule\space\space\the\cpl@orig@columnseprule^^J%
  \string\skip\string\footins\space\space\the\cpl@orig@footins^^J%
  \string\hoffset\space\space\the\cpl@orig@hoffset^^J%
  \string\voffset\space\space\the\cpl@orig@voffset^^J%
  \string\mag\space\space\the\cpl@orig@mag^^J%
  \if@twocolumn\string\@twocolumntrue\space\fi%
  \if@twoside\string\@twosidetrue\space\fi%
  \if@mparswitch\string\@mparswitchtrue\space\fi%
  \if@reversemargin\string\@reversemargintrue\space\fi^^J%
  (1in=72.27pt, 1cm=28.45pt)^^J%
  *******************************************************^^J%
}%
\@onlypreamble\cpl@print@defaultparams\relax
\newcommand\cpl@print@finalparams{%
  ^^J+++++++++++++++++++++++++++++++++++++++++++++++++++++++^^J%
  Final page layout dimensions and booleans^^J%
  (changelayout package)^^J%
  \string\paperwidth\space\space\the\paperwidth^^J%
  \string\paperheight\space\space\the\paperheight^^J%
  \string\textwidth\space\space\the\textwidth^^J%
  \string\textheight\space\space\the\textheight^^J%
  \string\oddsidemargin\space\space\the\oddsidemargin^^J%
  \string\evensidemargin\space\space\the\evensidemargin^^J%
  \string\topmargin\space\space\the\topmargin^^J%
  \string\headheight\space\space\the\headheight^^J%
  \string\headsep\space\space\the\headsep^^J%
  \string\footskip\space\space\the\footskip^^J%
  \string\marginparwidth\space\space\the\marginparwidth^^J%
  \string\marginparsep\space\space\the\marginparsep^^J%
  \string\columnsep\space\space\the\columnsep^^J%
  \string\columnseprule\space\space\the\columnseprule^^J%
  \string\skip\string\footins\space\space\the\footins^^J%
  \string\hoffset\space\space\the\hoffset^^J%
  \string\voffset\space\space\the\voffset^^J%
  \string\mag\space\space\the\mag^^J%
  \if@twocolumn\string\@twocolumntrue\space\fi%
  \if@twoside\string\@twosidetrue\space\fi%
  \if@mparswitch\string\@mparswitchtrue\space\fi%
  \if@reversemargin\string\@reversemargintrue\space\fi^^J%
  (1in=72.27pt, 1cm=28.45pt)^^J%
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++^^J%
}%
\newrobustcmd\cpl@backtodefaults{\begingroup
  \protected@edef\@tempa{\endgroup
    \noexpand\setlength{\paperwidth}{\cpl@orig@paperwidth}%
    \noexpand\setlength{\paperheight}{\cpl@orig@paperheight}%
    \noexpand\setlength{\textheight}{\cpl@orig@textheight}%
    \noexpand\setlength{\textwidth}{\cpl@orig@textwidth}%
    \noexpand\setlength{\linewidth}{\cpl@orig@linewidth}%
    \noexpand\setlength{\oddsidemargin}{\cpl@orig@oddsidemargin}%
    \noexpand\setlength{\evensidemargin}{\cpl@orig@evensidemargin}%
    \noexpand\setlength{\topmargin}{\cpl@orig@topmargin}%
    \noexpand\setlength{\headsep}{\cpl@orig@headsep}%
    \noexpand\setlength{\headheight}{\cpl@orig@headheight}%
    \noexpand\setlength{\footskip}{\cpl@orig@footskip}%
    \noexpand\setlength{\skip\footins}{\cpl@orig@footins}%
    \noexpand\setlength{\marginparwidth}{\cpl@orig@marginparwidth}%
    \noexpand\setlength{\marginparsep}{\cpl@orig@marginparsep}%
    \noexpand\setlength{\marginparpush}{\cpl@orig@marginparpush}%
    \noexpand\setlength{\columnsep}{\cpl@orig@columnsep}%
    \noexpand\setlength{\columnseprule}{\cpl@orig@columnseprule}%
    \noexpand\setlength{\hoffset}{\cpl@orig@hoffset}%
    \noexpand\setlength{\voffset}{\cpl@orig@voffset}%
    \noexpand\mag=\cpl@orig@mag%
    \noexpand\cpl@changelayout%
    \noexpand\cpl@info{Layout parameters reset to defaults}%
  }\@tempa%
}
\new\def\cpl@content{No content}
\new\def\cpl@paperheight{\paperheight}
\new\def\cpl@paperwidth{\paperwidth}
\new\def\cpl@textheight{\textheight}
\new\def\cpl@textwidth{\textwidth}
\new\def\cpl@evensidemargin{\evensidemargin}
\new\def\cpl@oddsidemargin{\oddsidemargin}
\new\def\cpl@topmargin{\topmargin}
\new\def\cpl@headheight{\headheight}
\new\def\cpl@headsep{\headsep}
\new\def\cpl@footskip{\footskip}
\new\def\cpl@footins{\skip\footins}
\new\def\cpl@topsep{\topsep}
\new\def\cpl@listparindent{\listparindent}
\new\def\cpl@parindent{\parindent}
\new\def\cpl@parsep{\parsep}
\new\def\cpl@parskip{\parskip}
\new\def\cpl@leftmargin{\leftmargin}
\new\def\cpl@rightmargin{\rightmargin}
\new\def\cpl@columnwidth{\columnwidth}
\new\def\cpl@columnsep{\columnsep}
\new\def\cpl@columnseprule{\columnseprule}
\new\def\cpl@marginparwidth{\marginparwidth}
\new\def\cpl@marginparsep{\marginparsep}
\new\def\cpl@marginparpush{\marginparpush}
\new\def\cpl@hoffset{\hoffset}
\new\def\cpl@voffset{\voffset}
\new\def\cpl@textcolor{black}
\define@boolkey+[KV]{changelay}[cpl@]{reset}[true]{%
  \cpl@backtodefaults}{\cpl@err{Erroneous value for 'reset'}}
\define@boolkeys[KV]{changelay}[cpl@]{switchadjust,logdefault,%
  logfinal,strictcheck,oddpage}[true]
\define@cmdkey[KV]{changelay}[cpl@]{content}[]{}
\define@cmdkey[KV]{changelay}[cpl@]{textcolor}[black]{}
\define@cmdkey[KV]{changelay}[cpl@]{paperheight}[\paperheight]{}
\define@cmdkey[KV]{changelay}[cpl@]{paperwidth}[\paperwidth]{}
\define@cmdkey[KV]{changelay}[cpl@]{textheight}[\textheight]{}
\define@cmdkey[KV]{changelay}[cpl@]{textwidth}[\textwidth]{}
\define@cmdkey[KV]{changelay}[cpl@]{evensidemargin}[\evensidemargin]{}
\define@cmdkey[KV]{changelay}[cpl@]{oddsidemargin}[\oddsidemargin]{}
\define@cmdkey[KV]{changelay}[cpl@]{topmargin}[\topmargin]{}
\define@cmdkey[KV]{changelay}[cpl@]{headheight}[\headheight]{}
\define@cmdkey[KV]{changelay}[cpl@]{headsep}[\headsep]{}
\define@cmdkey[KV]{changelay}[cpl@]{footskip}[\footskip]{}
\define@cmdkey[KV]{changelay}[cpl@]{footins}[\skip\footins]{}
\define@cmdkey[KV]{changelay}[cpl@]{topsep}[\topsep]{}
\define@cmdkey[KV]{changelay}[cpl@]{listparindent}[\parindent]{}
\define@cmdkey[KV]{changelay}[cpl@]{parindent}[\parindent]{}
\define@cmdkey[KV]{changelay}[cpl@]{parsep}[\parsep]{}
\define@cmdkey[KV]{changelay}[cpl@]{parskip}[\parskip]{}
\define@cmdkey[KV]{changelay}[cpl@]{leftmargin}[\leftmargin]{}
\define@cmdkey[KV]{changelay}[cpl@]{rightmargin}[\rightmargin]{}
\define@cmdkey[KV]{changelay}[cpl@]{columnwidth}[\columnwidth]{}
\define@cmdkey[KV]{changelay}[cpl@]{columnsep}[\columnsep]{}
\define@cmdkey[KV]{changelay}[cpl@]{columnseprule}[\columnseprule]{}
\define@cmdkey[KV]{changelay}[cpl@]{marginparwidth}[\marginparwidth]{}
\define@cmdkey[KV]{changelay}[cpl@]{marginparsep}[\marginparsep]{}
\define@cmdkey[KV]{changelay}[cpl@]{marginparpush}[\marginparpush]{}
\define@cmdkey[KV]{changelay}[cpl@]{hoffset}[\hoffset]{}
\define@cmdkey[KV]{changelay}[cpl@]{voffset}[\voffset]{}
\DeclareCmdListParser\cpl@csvloop{,}
\new\def\cpl@cmddo#1{\csdef{cpl@x@#1}{\z@}}
\new\def\cpl@list@a{%
  paperheight,paperwidth,textheight,textwidth,evensidemargin,%
  oddsidemargin,topmargin,headheight,headsep,footskip,footins,topsep,%
  listparindent,parindent,parsep,parskip,leftmargin,rightmargin,%
  columnwidth,columnsep,columnseprule,marginparwidth,marginparsep,%
  marginparpush,hoffset,voffset%
}
\new\def\cpl@definecmds{\begingroup%
  \edef\@tempa{\endgroup\cpl@csvloop[\cpl@cmddo]\cpl@list@a}%
  \@tempa%
}
\cpl@definecmds\undef{\cpl@list@a}\undef{\cpl@definecmds}%
\define@cmdkeys[KV@X]{changelay}[cpl@x@]{%
  paperheight,paperwidth,textheight,textwidth,evensidemargin,%
  oddsidemargin,topmargin,headheight,headsep,footskip,footins,%
  topsep,listparindent,parindent,parsep,parskip,leftmargin,%
  rightmargin,columnwidth,columnsep,columnseprule,marginparwidth,%
  marginparsep,marginparpush,hoffset,voffset%
}[\z@]
\DeclareOptionX*{\cpl@warn{Unknown option '\CurrentOption' ignored}}
\ExecuteOptionsX<changelay>{}
\ProcessOptionsX*<changelay>\relax
\cpl@csnumdef{cpl@cnt@a}{\z@}
\cpl@csnumdef{cpl@mem@c@page}{\z@}
\new\def\cpl@xlabel{_x_}
\new\gdef\thecpl@mem@c@page{\the\c@page}
\new\long\def\cpl@afterelsefi#1\else#2\fi{\fi#1}
\newrobustcmd\cpl@protected@write[3]{%
  \begingroup
    \let\thecpl@mem@c@page\relax
    #2%
    \let\protect\@unexpandable@protect
    \csedef{@tempa}{\write#1{#3}}\@tempa%
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi
}
\ifcpl@strictcheck\if@filesw\cpl@afterelsefi
  \new\def\cpl@mem@label#1{%
    \@bsphack
    \cpl@protected@write\@auxout{}%
      {\cpl@new@mem@label{#1}{\thecpl@mem@c@page}}%
    \@esphack
  }
\else
  \cpl@err{nofiles: I need to write in the .aux file}
\fi\fi
\new\def\cpl@new@mem@label#1#2{\csgdef{m@#1}{#2}}
\new\def\cpl@mem@labelref#1{0\csuse{m@#1}}
\newrobustcmd\cpl@checkifoddpage{%
  \cpl@oddpagefalse%
  \ifbool{cpl@strictcheck}{%
    \global\advance\cpl@cnt@a\@ne\relax
    \cpl@mem@label{\cpl@xlabel\the\cpl@cnt@a}%
    \cpl@mem@c@page=\cpl@mem@labelref{\cpl@xlabel\the\cpl@cnt@a}\relax
    \ifodd\cpl@mem@c@page\cpl@oddpagetrue\fi
  }{%
    \ifodd\c@page\cpl@oddpagetrue\fi
  }%
}
\ifbool{cpl@logdefault}{%
  \AtBeginDocument{\immediate\write-1{\cpl@print@defaultparams}}
}{}
\ifbool{cpl@logfinal}{%
  \AtEndDocument{\immediate\write-1{\cpl@print@finalparams}}
}{}
\newrobustcmd\changepagelayout[1]{%
  \setkeys[KV]{changelay}{#1}%
  \cpl@dimdef{\paperheight}{\cpl@paperheight}%
  \cpl@dimdef{\paperwidth}{\cpl@paperwidth}%
  \cpl@dimdef{\textheight}{\cpl@textheight}%
  \cpl@dimdef{\textwidth}{\cpl@textwidth}%
  \cpl@dimdef{\evensidemargin}{\cpl@evensidemargin}%
  \cpl@dimdef{\oddsidemargin}{\cpl@oddsidemargin}%
  \cpl@dimdef{\columnsep}{\cpl@columnsep}%
  \cpl@dimdef{\columnseprule}{\cpl@columnseprule}%
  \cpl@dimdef{\topmargin}{\cpl@topmargin}%
  \cpl@dimdef{\headheight}{\cpl@headheight}%
  \cpl@dimdef{\headsep}{\cpl@headsep}%
  \cpl@dimdef{\footskip}{\cpl@footskip}%
  \cpl@dimdef{\skip\footins}{\cpl@footins}%
  \cpl@dimdef{\marginparwidth}{\cpl@marginparwidth}%
  \cpl@dimdef{\marginparsep}{\cpl@marginparsep}%
  \cpl@dimdef{\marginparpush}{\cpl@x@marginparpush}%
  \cpl@changelayout%
}
\new\global\let\changetextlayout\changepagelayout
\newrobustcmd\adjustpagelayout[1]{%
  \setkeys[KV@X]{changelay}{#1}%
  \cpl@dimadd{\paperheight}{\cpl@x@paperheight}%
  \cpl@dimadd{\paperwidth}{\cpl@x@paperwidth}%
  \cpl@dimadd{\textheight}{\cpl@x@textheight}%
  \cpl@dimadd{\textwidth}{\cpl@x@textwidth}%
  \cpl@dimadd{\evensidemargin}{\cpl@x@evensidemargin}%
  \cpl@dimadd{\oddsidemargin}{\cpl@x@oddsidemargin}%
  \cpl@dimadd{\columnsep}{\cpl@x@columnsep}%
  \cpl@dimadd{\columnseprule}{\cpl@x@columnseprule}%
  \cpl@dimadd{\topmargin}{\cpl@x@topmargin}%
  \cpl@dimadd{\headheight}{\cpl@x@headheight}%
  \cpl@dimadd{\headsep}{\cpl@x@headsep}%
  \cpl@dimadd{\footskip}{\cpl@x@footskip}%
  \cpl@dimadd{\skip\footins}{\cpl@x@footins}%
  \cpl@dimadd{\marginparwidth}{\cpl@x@marginparwidth}%
  \cpl@dimadd{\marginparsep}{\cpl@x@marginparsep}%
  \cpl@dimadd{\marginparpush}{\cpl@x@marginparpush}%
  \cpl@changelayout%
}
\new\global\let\adjusttextlayout\adjustpagelayout
\new\def\cpl@listparams{%
  \topsep=\z@%
  \partopsep=\z@%
  \listparindent=\parindent%
  \parsep=\parskip%
  \itemsep=\z@%
}
\newenvironment{cpl@adjustwidth}[2]{%
  \begin{list}{}{%
    \cpl@listparams%
    \setlength\leftmargin{#1}%
    \setlength\rightmargin{#2}%
  }%
  \item[]\ignorespaces}%
{\end{list}\@ignoretrue}
\newenvironment{cpl@adjustwidth*}[2]{%
  \begin{list}{}{%
    \cpl@listparams%
    \cpl@checkifoddpage%
    \ifbool{cpl@oddpage}{%
      \setlength\leftmargin{#1}%
      \setlength\rightmargin{#2}%
    }{%
      \setlength\leftmargin{#2}%
      \setlength\rightmargin{#1}%
    }%
  }\item[]\ignorespaces}%
{\end{list}\@ignoretrue}
\newrobustcmd\adjusttextwidth[1]{%
  \setkeys[KV]{changelay}{#1}%
  \ifbool{cpl@switchadjust}{%
    \begin{cpl@adjustwidth*}{\cpl@leftmargin}{\cpl@rightmargin}%
      \textcolor{\cpl@textcolor}{\cpl@content}%
    \end{cpl@adjustwidth*}\relax
  }{%
    \begin{cpl@adjustwidth}{\cpl@leftmargin}{\cpl@rightmargin}%
      \textcolor{\cpl@textcolor}{\cpl@content}%
    \end{cpl@adjustwidth}\relax
  }
}
\newrobustcmd*\resetdefault{\setkeys[KV]{changelay}{reset=true}}

\endinput 