%% -*-TeX-*-
 % ###################################################################
 % 
 %  FILE: "pdfbooklet.sty"
 %                                    created: 2002-03-11 14:18:51 
 %                                last update: 2006-09-05 16:32:21 
 %  Author: Fr\'ed\'eric Boulanger
 %  E-mail: Frederic.Boulanger@supelec.fr
 %    mail: Sup\'elec -- Service Informatique
 %          Plateau de Moulon, 3 rue Joliot-Curie, 91192 Gif-sur-Yvette cedex
 %     www: http://wwwsi.supelec.fr/fb/
 %  
 %  Description: 
 %  
 %  A small package to build 4-up, duplex-printing A6 booklets from a pdf 
 %  file. Intended to replace the following PostScript specific stuff:
 %    psbook
 %    pstops 8:0,1,4,5,2,3,6,7
 %    psnup -p a4 -4
 %  
 %  Once printed in duplex mode, the pages should be cut in two A5 
 %  sheets. Thoses sheets should then be folded, stacked and stappled
 %  in there middle to build a A6 booklet.
 %  
 %  In the following diagram, n is the smallest multiple of 8 which is 
 %  greater or equal to the number of pages in the original document.
 %  
 %           +----------+----------+
 %       1   |          |          |   n
 %           |          |          |  
 %       |   |    2     |   n-1    |   |
 %       +-> |          |          | <-+
 %           |          |          |
 %           |          |          |
 %           +----------+----------+ <-- cut here, stack the bottom part 
 %           |          |          |     on top of the top part, repeat 
 %           |          |          |     with the following pages.
 %       +-> |    4     |   n-3    | <-+
 %       |   |          |          |   |
 %           |          |          |   
 %       3   |          |          |   n-2
 %           +----------+----------+
 %  
 %                      ^
 %                      |
 %                      +------ fold here
 %  
 %  Usage:
 %  
 %  Write the number of page of your source.pdf document into a
 %  source.pages text file. This can be done automatically with the
 %  following code in the preamble of the LaTeX source of the 
 %  document :
 %  
 %  \AtEndDocument{%
 %    \newwrite{\numberofpages}
 %    \immediate\openout\numberofpages\jobname.pages%
 %    \immediate\write\numberofpages{\thepage}%
 %    \immediate\closeout\numberofpages
 %  }
 %  
 %  Write a booklet.tex LaTeX source file as follows:
 %  
 %  \documentclass[a4paper,twoside]{article}
 %  \usepackage{pdfbooklet}
 % 
 %  \begin{document}
 %    \makepdfbooklet{source}%
 %  \end{document}
 %
 %  Then compile booklet.tex with pdflatex. If the source.pages
 %  file does not exist, pdfbooklet will ask you the number of pages
 %  of the source.pdf document.
 %  
 %  
 %  History
 % 
 %  modified   by  rev reason
 %  ---------- --- --- -----------
 %  2002-03-11 FBO 1.0 original
 % ###################################################################
 %%
\RequirePackage{ifthen}
\RequirePackage{pdfpages}

% Those are missing in LaTeX
\newcommand{\dividecounter}[2]{%
  \global\divide\csname c@#1\endcsname by #2%
}
\newcommand{\multiplycounter}[2]{%
  \global\multiply\csname c@#1\endcsname by #2%
}
%%%
% Page counters
%%%
% Get a new page counter
\newcommand{\pdfbooklet@newpagenumber}[1]{%
  \newcounter{pdfbooklet@page\romannumeral#1}%
}
% Set a page counter
\newcommand{\pdfbooklet@setpagenumber}[2]{%
  \setcounter{pdfbooklet@page\romannumeral#1}{#2}%
}
% Add to a page counter
\newcommand{\pdfbooklet@addpagenumber}[2]{%
  \addtocounter{pdfbooklet@page\romannumeral#1}{#2}%
}
% Get the value of a page counter
\newcommand{\pdfbooklet@pagevalue}[1]{%
  \value{pdfbooklet@page\romannumeral#1}%
}
% Get the representation of a page counter
\newcommand{\thepdfbooklet@page}[1]{%
  \expandafter\csname thepdfbooklet@page\romannumeral#1\endcsname
}
%
%%
% Page before the initial page in the page set
\newcounter{pdfbooklet@startpage}
\setcounter{pdfbooklet@startpage}{0}
%
% Number of pages in the output document (multiple of 8)
\newcounter{pdfbooklet@lastpage}
%
% Create the 8 page counters (4 pages per sheet with duplex printing)
\newcounter{pdfbooklet@tmpcount}
\setcounter{pdfbooklet@tmpcount}{1}
\whiledo{\value{pdfbooklet@tmpcount}<9}{%
  \pdfbooklet@newpagenumber{\thepdfbooklet@tmpcount}%
  \addtocounter{pdfbooklet@tmpcount}{1}%
}

%%
% The main command which includes the PDF pages in the right order
\newcommand{\pdfbooklet@includepages}[1]{%
  % First, initialize the 4 first page numbers to the start number
  % These page numbers start from the beginning of the document
  \setcounter{pdfbooklet@tmpcount}{1}%
  \whiledo{\value{pdfbooklet@tmpcount}<5}{%
    \pdfbooklet@setpagenumber{\thepdfbooklet@tmpcount}{\value{pdfbooklet@startpage}}%
    \addtocounter{pdfbooklet@tmpcount}{1}%
  }
  % Then, initialize the 4 last page numbers to the last page number
  % These page numbers start from the end of the document (n-x)
  \setcounter{pdfbooklet@tmpcount}{5}%
  \whiledo{\value{pdfbooklet@tmpcount}<9}{%
    \pdfbooklet@setpagenumber{\thepdfbooklet@tmpcount}{\value{pdfbooklet@lastpage}}%
    \addtocounter{pdfbooklet@tmpcount}{1}%
  }
  %
  % Then, compute the number of pages 1, 2, 3 and 4 in the set
  \setcounter{pdfbooklet@tmpcount}{1}%
  \whiledo{\value{pdfbooklet@tmpcount}<5}{%
    \pdfbooklet@addpagenumber{\thepdfbooklet@tmpcount}{\value{pdfbooklet@tmpcount}}%
    \addtocounter{pdfbooklet@tmpcount}{1}%
  }
  % And the number of pages n, n-1, n-2, n-3 in the set
  \setcounter{pdfbooklet@tmpcount}{5}%
  \whiledo{\value{pdfbooklet@tmpcount}<9}{%
    \pdfbooklet@addpagenumber{\thepdfbooklet@tmpcount}{5}%
    \pdfbooklet@addpagenumber{\thepdfbooklet@tmpcount}{-\value{pdfbooklet@tmpcount}}%
    \addtocounter{pdfbooklet@tmpcount}{1}%
  }
  %
  % If pages are beyond the total page number of the original document, 
  % insert blank pages.
  \setcounter{pdfbooklet@tmpcount}{1}%
  \whiledo{\value{pdfbooklet@tmpcount}<9}{%
    \ifthenelse{\pdfbooklet@pagevalue{\thepdfbooklet@tmpcount}>\pdfbooklet@totalpages}{%
      \expandafter\def\csname pdfbooklet@\romannumeral\thepdfbooklet@tmpcount\endcsname{{}}%
    }{%
      \expandafter\edef\csname pdfbooklet@\romannumeral\thepdfbooklet@tmpcount\endcsname{%
        \thepdfbooklet@page{\thepdfbooklet@tmpcount}%
      }%
    }%
    \addtocounter{pdfbooklet@tmpcount}{1}%
  }%
  %
  % Include the pages 2, n-1, 4, n-3 on one side, n, 1, n-2, 3 on the other 
  % side so that page 1 is on the back of page 2, page n on the back 
  % of page n-1, page 3 on the back of page 4 and page n-2 on the back of 
  % page n-3.
%   \typeout{^^J\includepdf[%
%     nup=4,
%     pages={%
%       \pdfbooklet@ii,\pdfbooklet@vi,\pdfbooklet@iv,\pdfbooklet@viii,%
%       \pdfbooklet@v,\pdfbooklet@i,\pdfbooklet@vii,\pdfbooklet@iii
%     }
%   ]{#1}^^J}%
  \includepdf[%
    nup=4,
    pages={%
      \pdfbooklet@ii,\pdfbooklet@vi,\pdfbooklet@iv,\pdfbooklet@viii,%
      \pdfbooklet@v,\pdfbooklet@i,\pdfbooklet@vii,\pdfbooklet@iii
    }
  ]{#1}%
  % Go to the next set of 8 pages.
  \addtocounter{pdfbooklet@startpage}{4}%
  \addtocounter{pdfbooklet@lastpage}{-4}%
}

% The command to make a 4-up duplex-printing booklet from a PDF document.
% The argument is the base name of the file.
% The number of pages of the original document is read from a file 
% with the same base name as the PDF document, but with a .pages 
% extension. If no such file exists, the user is asked for the number 
% of pages.
% 
% A .pages file is easy to create. Just add the following to the 
% preamble of your document:
% \AtEndDocument{%
%   \newwrite{\numberofpages}
%   \immediate\openout\numberofpages\jobname.pages%
%   \immediate\write\numberofpages{\thepage}%
%   \immediate\closeout\numberofpages
% }
% 
\newcommand{\makepdfbooklet}[1]{%
  \newread\pdfbooklet@numberofpages
  % Try to read from the .pages file
  \immediate\openin\pdfbooklet@numberofpages#1.pages
  \ifeof\pdfbooklet@numberofpages
    % No .pages file -> ask the user
    \immediate\closein\pdfbooklet@numberofpages
    \message{^^J^^J  No file #1.pages^^J}%
    \message{  Enter the number of pages of #1.pdf: }%
    \immediate\read-1 to \pdfbooklet@totalpages
  \else
    % Read the number of pages from the .pages file
    \immediate\read\pdfbooklet@numberofpages to \pdfbooklet@totalpages
    \immediate\closein\pdfbooklet@numberofpages
  \fi
  % Compute the number of pages to output (smallest multiple of 8 
  % which is greater or equal to the number of pages in the  
  % original document)
  \setcounter{pdfbooklet@lastpage}{\pdfbooklet@totalpages}%
  \dividecounter{pdfbooklet@lastpage}{8}%
  \multiplycounter{pdfbooklet@lastpage}{8}%
  \ifthenelse{\value{pdfbooklet@lastpage}<\pdfbooklet@totalpages}{%
    \addtocounter{pdfbooklet@lastpage}{8}%
  }{}%
  % Include pages while there are pages to include...
  \whiledo{\value{pdfbooklet@startpage} < \value{pdfbooklet@lastpage}}{%
    \pdfbooklet@includepages{#1.pdf}%
  }%
}
