%%
%% This is file `facingpages.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% facingpages.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright (C) 2008 by Richard Zach <rzach@ucalgary.ca>
%% 
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.2 of this license
%% or (at your option) any later version.  The latest version of this
%% license is in:
%% 
%%    http://www.latex-project.org/lppl.txt
%% 
%% and version 1.2 or later is part of all distributions of LaTeX version
%% 1999/12/01 or later.
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{facingpages}
    [2008/06/06 0.93 Facing recto and verso pages]
\RequirePackage{ifthen}
\@ifundefined{thesheetsequence}{\PackageError{facingpages}{%
    \protect\thesheetsequence\space is undefined. Probably you're not
    using memoir.cls?}{}}
\@ifundefined{pdfsavepos}{\PackageWarning{facingpages}{%
    \protect\pdfsavepos\space is undefined. Sync points won't work.}}
\newboolean{RegEmpty}
\def\SaveReg#1#2#3{%
\expandafter\xdef\csname REG@#1@#2\endcsname{#3}}
\def\cSaveReg#1#2#3{\SaveReg{#1}{\number\value{#2}}{#3}}
\def\IfRegEmpty#1#2#3#4{%
  \@ifundefined{REG@#1@#2}{#3}{#4}}
\def\RegValue#1#2{\expandafter\csname REG@#1@#2\endcsname}
\def\cRegValue#1#2{\RegValue{#1}{\number\value{#2}}}
\def\RestoreReg#1#2{%
\@ifundefined{REG@#1@#2}{%
  \setboolean{RegEmpty}{true}}{%
  \setboolean{RegEmpty}{false}}
\xdef\RegContent{\csname REG@#1@#2\endcsname}}
\def\cRestoreReg#1#2{\RestoreReg{#1}{\number\value{#2}}}
\newboolean{FP@On}
\setboolean{FP@On}{false}
\newboolean{FP@Left}
\setboolean{FP@Left}{false}
\newboolean{FP@ChangedWarn}
\newcounter{FP@Seg}
\newcounter{FP@current@LStart}
\newcounter{FP@current@RStart}
\newcounter{FP@current@LEnd}
\newcounter{FP@current@REnd}
\newcounter{FP@current@REndSaved}
\newcounter{FP@current@LLength}
\newcounter{FP@current@RLength}
\newcounter{FP@current@LPageCount}
\newcounter{FP@current@LPageStart}
\setcounter{FP@Seg}{1}
\setcounter{FP@current@LStart}{1}
\newcommand{\FP@Comma}{=}
\let\mytempoutputpage\@outputpage
\def\incrementpagebytwo{\incrementpagebyone\g@addto@macro\@outputpage{\stepcounter{page}}}
\def\incrementpagebyone{\let\@outputpage\mytempoutputpage}
\def\FP@RestoreLength#1#2{\SaveReg{FP@RLengthSaved}{#1}{#2}}
\newcommand{\FP@ProcessSeg}{%
  \ifthenelse{\boolean{FP@On}}{%
  \whiledo{\not \value{FP@current@LEnd} < \value{FP@current@LStart}}{%
    \immediate\write\@psselect{\arabic{FP@current@LStart},\arabic{FP@current@RStart}\FP@Comma}
    \stepcounter{FP@current@LStart}
    \stepcounter{FP@current@RStart}}}{%
  \ifthenelse{\value{FP@current@LStart} = \value{FP@current@REnd}}{%
    \immediate\write\@psselect{\arabic{FP@current@LStart}\FP@Comma}}{%
    \immediate\write\@psselect{\arabic{FP@current@LStart}-\arabic{FP@current@REnd}\FP@Comma}}}}
\def\FP@FinishUpRight{%
\setcounter{FP@current@RLength}{\value{sheetsequence}}
\addtocounter{FP@current@RLength}{-\value{FP@current@RStart}}
\cRestoreReg{FP@RLengthSaved}{FP@Seg}
\ifthenelse{\boolean{RegEmpty}}{%
  \setboolean{FP@ChangedWarn}{true}
  \typeout{Length of segment \arabic{FP@Seg} not yet saved.}
}{%
\typeout{Segment \arabic{FP@Seg} length was: \RegContent\space
  current: \arabic{FP@current@RLength}.}
  \ifthenelse{\equal{\RegContent}{\theFP@current@RLength}}{}{%
    \setboolean{FP@ChangedWarn}{true}
    \typeout{Length of segment \arabic{FP@Seg} has changed!}}}
\immediate\write\@auxout{%
\string\FP@RestoreLength{\theFP@Seg}{\theFP@current@RLength}
}
\whiledo{\value{FP@current@RLength} < \value{FP@current@LLength}}{%
  \message{Padding right segment with one page.}
  \newpage
  \phantom{*}
  \stepcounter{FP@current@RLength}}}
\newcommand{\BeginFacingPagesLeft}{%
  \message{\string\BeginFacingPagesLeft\space called on sheet \thesheetsequence, page \thepage.}
  \ifthenelse{\boolean{FP@On}}{%
    \ifthenelse{\boolean{FP@Left}}{%
      \PackageError{facingpages}{Called \protect \BeginFacingPagesLeft, but
        already in verso mode}{}}{%
      \FP@FinishUpRight}}{%
\ifthenelse{\isodd{\thepage}}{}{%
  \newpage \phantom{*}%
  \message{Moved one page, now on sheet \thesheetsequence, page \thepage}}}
  \setcounter{FP@current@REnd}{\value{sheetsequence}}
  \FP@ProcessSeg
  \incrementpagebyone
  \newpage
  \incrementpagebytwo
  \stepcounter{FP@Seg}
  \setcounter{FP@current@LStart}{\value{sheetsequence}}
  \setcounter{FP@current@LPageStart}{\value{page}}
  \setcounter{SP@count}{\theFP@Seg}
  \multiply\c@SP@count by \c@SP@perseg
  \setboolean{FP@On}{true}
  \setboolean{FP@Left}{true}}
\newcommand{\BeginFacingPagesRight}{%
  \message{\string\BeginFacingPagesRight\space called on sheet \thesheetsequence, page \thepage.}
\ifthenelse{\boolean{FP@Left}}{}{%
  \PackageError{facingpages}{Called \protect \BeginFacingPagesRight,
    but not in verso mode}{}}
  \setcounter{FP@current@LLength}{\value{sheetsequence}}
  \addtocounter{FP@current@LLength}{-\value{FP@current@LStart}}
  \cRestoreReg{FP@RLengthSaved}{FP@Seg}
  \ifthenelse{\boolean{RegEmpty}}{}{%
    \whiledo{\value{FP@current@LLength} < \RegContent}{%
    \message{Padding left segment with one page.}
    \newpage
    \phantom{*}
    \stepcounter{FP@current@LLength}}}
  \setcounter{FP@current@LEnd}{\value{sheetsequence}}
  \newpage
  \setcounter{FP@current@RStart}{\value{sheetsequence}}
  \setcounter{page}{\value{FP@current@LPageStart}}
  \addtocounter{page}{\@ne}
  \setcounter{SP@count}{\value{FP@Seg}}
  \multiply\c@SP@count by \c@SP@perseg
  \setboolean{FP@On}{true}
  \setboolean{FP@Left}{false}}

\newcommand{\EndFacingPages}{%
  \ifthenelse{\boolean{FP@On}}{%
    \ifthenelse{\boolean{FP@Left}}{%
      \PackageError{facingpages}{Called \protect\EndFacingPages, but not
        in recto mode}{}}{}}{%
    \PackageError{facingpages}{Called \protect\EndFacingPages, but not in
      facing pages mode}{}}
  \setcounter{FP@current@REnd}{\value{sheetsequence}}
  \FP@FinishUpRight
  \FP@ProcessSeg
  \setboolean{FP@On}{false}
  \setboolean{FP@Left}{false}
  \incrementpagebyone
  \newpage
  \addtocounter{FP@Seg}{\@ne}
  \setcounter{FP@current@LStart}{\value{sheetsequence}}}
\AtBeginDocument{%
  \newwrite\@psselect
  \immediate\openout\@psselect\jobname.pss}
\AtEndDocument{%
  \ifthenelse{\boolean{FP@On}}{%
    \ifthenelse{\boolean{FP@Left}}{%
      \PackageError{facingpages}{Document ended in verso mode}{}}{}
    \FP@FinishUpRight}{}
  \setcounter{FP@current@REnd}{\value{sheetsequence}}
  \renewcommand{\FP@Comma}{} \FP@ProcessSeg
  \ifthenelse{\boolean{FP@ChangedWarn}}{%
    \PackageWarning{facingpages}{Facing pages segments have changed.
      Rerun to get facing pages right}}{}
  \immediate\closeout\@psselect}
\newcounter{SP@count}
\newcounter{SP@perseg}
\setcounter{SP@perseg}{100}
\setcounter{SP@count}{\c@SP@perseg}
\newcounter{SP@Pos}
\newcounter{SP@i}
\newboolean{SPadjust}
\setboolean{SPadjust}{true}
\def\FP@RestoreSyncPointL#1#2{%
  \setcounter{SP@Pos}{#2}
  \divide\c@SP@Pos by 65536
  \SaveReg{SP@PositionL}{#1}{\theSP@Pos}}
\def\FP@RestoreSyncPointR#1#2{%
  \setcounter{SP@Pos}{#2}
  \divide\c@SP@Pos by 65536
  \SaveReg{SP@PositionR}{#1}{\theSP@Pos}}
\newcommand{\SP}[1][]{%
  \stepcounter{SP@count}
  \ifthenelse{\boolean{FP@Left}}{%
    \FP@SyncPointLeft{#1}}{%
    \FP@SyncPointRight{#1}}{}
  \ignorespaces}
\def\FP@SyncPointLeft#1{%
 \ifthenelse{\equal{#1}{}}{}{\vspace*{#1 pt}}
  \pdfsavepos
  \setcounter{SP@i}{\value{sheetsequence}}
  \addtocounter{SP@i}{-\theFP@current@LStart}
 \expandafter\write\@auxout\expandafter{\expandafter\noexpand\expandafter\FP@RestoreSyncPointL\expandafter{\number\c@SP@count}{\number\pdflastypos}}
  \immediate\write\@auxout{\noexpand\SaveReg{SP@SheetL}{\number\c@SP@count}{\number\c@SP@i}}
  \cRestoreReg{SP@PositionL}{SP@count}
  \ifthenelse{\boolean{RegEmpty}}{}{%
    \setcounter{SP@Pos}{\RegContent}
  \cRestoreReg{SP@PositionR}{SP@count}
  \ifthenelse{\boolean{RegEmpty}}{}{%
    \addtocounter{SP@Pos}{-\RegContent}
  \cRestoreReg{SP@SheetL}{SP@count}
  \ifthenelse{\boolean{RegEmpty}}{}{%
     \setcounter{SP@i}{\RegContent}
  \cRestoreReg{SP@SheetR}{SP@count}
  \ifthenelse{\boolean{RegEmpty}}{}{%
    \addtocounter{SP@i}{-\RegContent}
  \SP@Adjust{#1}}}}}}
\def\FP@SyncPointRight#1{%
 \ifthenelse{\equal{#1}{}}{}{\vspace*{#1 pt}}
  \pdfsavepos
  \setcounter{SP@i}{\value{sheetsequence}}
  \addtocounter{SP@i}{-\theFP@current@RStart}
\expandafter\write\@auxout\expandafter{\expandafter\noexpand\expandafter\FP@RestoreSyncPointR\expandafter{\number\c@SP@count}{\number\pdflastypos}}
\immediate\write\@auxout{\noexpand\SaveReg{SP@SheetR}{\number\c@SP@count}{\number\c@SP@i}}
  \cRestoreReg{SP@PositionR}{SP@count}
\ifthenelse{\boolean{RegEmpty}}{}{%
  \setcounter{SP@Pos}{\RegContent}
  \cRestoreReg{SP@PositionL}{SP@count}
  \ifthenelse{\boolean{RegEmpty}}{}{%
    \addtocounter{SP@Pos}{-\RegContent}
  \cRestoreReg{SP@SheetR}{SP@count}
  \ifthenelse{\boolean{RegEmpty}}{}{%
    \setcounter{SP@i}{\RegContent}
  \cRestoreReg{SP@SheetL}{SP@count}
\ifthenelse{\boolean{RegEmpty}}{}{%
  \addtocounter{SP@i}{-\RegContent}
  \SP@Adjust{#1}
}}}}}
\def\SP@Show#1{\marginpar[\hfil\scriptsize #1]{\scriptsize #1}}
\def\SP@Adjust#1{%
  \ifthenelse{\equal{\theSP@i}{0}}{%
    \SP@Show{\fbox{\theSP@count} \theSP@Pos}%
    \ifthenelse{\boolean{SPadjust} \AND \equal{#1}{} \AND \theSP@Pos > 0}{%
      \vspace{\number\c@SP@Pos pt}}{}}{%
    \SP@Show{\fbox{\theSP@count} \theSP@i p}}}
\def\cutpageline{\vadjust{\newpage}}
\def\cutpagehere{{\parfillskip0pt\relax\par}\newpage\noindent}
\newcounter{max@reset}
\setcounter{max@reset}{0}
\newcommand*\MakeFPCounter[1]{%
  \RequirePackage{perpage}%
  \AtEndDocument{\SaveResets{#1}}%
  \pp@makeperpage{#1}\c@FPchk@{\@ne}}
\@onlypreamble\MakeFPCounter
\def\c@FPchk@#1{\z@=\z@
  \begingroup \pp@fetchctr{#1}\let\next\theperpage
  \addtocounter{pp@a@#1}\@ne \pp@fetchctr{#1}%
  \ifthenelse{\boolean{FP@On} \AND \NOT \equal{\next}{\theperpage}}{%
    \ResetValue{#1}{\theperpage}%
    \addtocounter{#1}\@ne}{}%
  \ifthenelse{\equal{\theperpage}{\@empty}}{}{%
    \SaveReg{FP@cReset@#1}{\theperpage}{\number\value{#1}}%
    \ifthenelse{\theperpage > \value{max@reset}}{%
      \setcounter{max@reset}{\theperpage}}{}%
    \SaveReg{FP@cResetFresh@#1}{\theperpage}{true}}%
  \pp@writectr\pp@pagectr{#1}{\number\value{#1}}}
\def\FPsetcounter#1#2{%
  \setcounter{SP@i}{\thepage}%
  \addtocounter{SP@i}\m@ne
  \ifthenelse{\value{SP@i} > \value{max@reset}}{%
    \setcounter{max@reset}{\thepage}{}}%
  \cSaveReg{FP@cReset@#1}{SP@i}{#2}%
  \cSaveReg{FP@cResetFresh@#1}{SP@i}{true}}
\def\ResetValue#1#2{%
  \setcounter{SP@i}{#2}%
  \setcounter{#1}{0}%
  \whiledo{\value{SP@i}>0}{%
    \addtocounter{SP@i}\m@ne%
    \IfRegEmpty{FP@cReset@#1}{\number\value{SP@i}}{}{%
      \setcounter{#1}{\cRegValue{FP@cReset@#1}{SP@i}}%
      \setcounter{SP@i}{0}}}}
\def\SaveResets#1{%
  \setcounter{SP@i}{0}
  \whiledo{\value{SP@i} < \value{max@reset}}{%
    \addtocounter{SP@i}\@ne
      \IfRegEmpty{FP@cResetFresh@#1}{\number\value{SP@i}}{}{%
        \immediate\write\@auxout{\noexpand\SaveReg{FP@cReset@#1}{\number\value{SP@i}}{\cRegValue{FP@cReset@#1}{SP@i}}}}{}}}
\endinput
%%
%% End of file `facingpages.sty'.
