%
%       S E L E C T P . S T Y   - Select pages for output
%
%  Version 1.0   Sept 25, 1992
%
%  By Donald Arseneau, asnd@triumf.ca, based on macros in
%  TUGBoat, 8:2 (1987), p. 217 written by Don Knuth, and with 
%  suggestions from Hossein Saiedian.
%
%  Copyright 1992,  Donald Arseneau
%  This software is contributed freely to the public domain.
%
%  This style allows selection of individual pages to be output by LaTeX
%  or plain TeX by specifying, for example, 
%
%     \outputonly{1,3, 7-12 16 17}
%
%  The list of numbers should be in the order that TeX generates the pages.
%  The numbers should be separated by commas or spaces, and may include 
%  ranges (7-12).
%
%  More complete instructions follow \endinput


%  First, set catcode of @ in case it isn't a "letter" already
\expandafter\edef\csname SP@catcode\endcsname
  {\catcode`\noexpand\@=\the\catcode`\@
  \let \csname SP@catcode\endcsname \noexpand\UnDefinedButNoAt}
\catcode`\@=11

% Allow plain TeX to use this style:

\ifx\c@page\Und@Fynd 
 \let\c@page \pageno 
 \def\@empty{}% note: \ifx ignores the \global nature of macros for comparison
 \let\nofiles\relax 
 \let\typeout\message
\fi

% Read list of selected pages

\def\outputonly{\begingroup \catcode`\,13 \SP@outonly}

\begingroup \catcode`\,13 
\gdef\SP@outonly#1{\edef,{\string,}\typeout{Only pages #1 will be output.}%
  \def,{ }\xdef\SP@geS{\SP@geS\space #1}% make commas be spaces
  \endgroup 
  \nofiles % prevent writing partial .aux files
  \let\shipout\SP@shipout% replace shipout command
  \ifnum\SP@nextpage=-\maxdimen % if first \outputonly command...
    \SP@getnextpage \fi % get first page number from list
}\endgroup

\gdef\SP@geS{}

% replace \shipout command with filter to select pages for output

\let\plain@shipout=\shipout

\newcount\SP@nextpage \global\SP@nextpage=-\maxdimen % flag as undefined
\newcount\SP@expectpage \global\SP@expectpage=-\maxdimen

\def\SP@getnextpage{\ifx\SP@geS\@empty \global\SP@nextpage\maxdimen
     \else % If more page numbers on list
     \afterassignment\SP@updPS \global\SP@nextpage\SP@geS\relax 
     \fi}

\def\SP@updPS#1\relax{\gdef\SP@geS{#1}}

\def\SP@shipout{\relax %\showthe\c@page%
 \ifnum\SP@expectpage=-\maxdimen \global\SP@expectpage\c@page
 \else \global\advance\SP@expectpage\@ne
 \fi
 \ifnum\SP@expectpage=\c@page\else % page number has jumped,
   \ifnum\SP@nextpage<\z@ %   if in middle of a range --
     \ifnum \AbsVal\c@page >\AbsVal\SP@expectpage\else % and jumping backward --
       \SP@getnextpage % read next number for sure
   \fi\fi 
   \SP@catchup  %  Resynchronize
 \fi \global\SP@expectpage\c@page
 \ifnum\SP@nextpage<\z@ % either a `negative' page, or end of range
   \ifnum\c@page<\z@   % preamble pages in plain only match singly
     \ifnum\c@page=\SP@nextpage\SP@getnextpage \let\SP@dopage\plain@shipout\fi
   \else % look for range match
     \ifnum\c@page>-\SP@nextpage \else \let\SP@dopage\plain@shipout \fi
     \ifnum\c@page<-\SP@nextpage \else \SP@getnextpage \fi
   \fi
 \else % look for single match
   \ifnum\c@page=\SP@nextpage \SP@getnextpage \let\SP@dopage\plain@shipout \fi
 \fi \SP@dopage}

\newbox\SP@garbage 
\def\SP@dopage{\deadcycles\z@ \setbox\SP@garbage=}

% read list of desired pages to get resynchronized with actual page numbers.
% \SP@catchup is called whenever the page number has a discontinuity.
%
\def\SP@catchup{%
 \ifnum \AbsVal\c@page >\AbsVal\SP@nextpage
   \ifx\SP@geS\@empty \def\SP@catchup{\global\SP@nextpage\maxdimen}\fi
   \SP@getnextpage 
   \expandafter \SP@catchup 
 \fi}

% take absolute value of TeX number or dimension
\def\AbsVal#1{\ifnum#1<\z@-\fi#1}

\SP@catcode % restore @ catcode

\endinput
%
% SELECTP.STY  -- Select pages for output
%
% INSTRUCTIONS
%
% This style file defines the command \outputonly which selects specific
% pages for output, much as \includeonly selects certain files for input.
% To use selectp.sty in LaTeX, specify "selectp" as one of the document
% style options and give the command \outputonly{_list_of_page_numbers_}
% before \begin{document}.  Only pages given in the list will be output
% to the DVI file.  For example
%
%     \documentstyle[12pt,selectp,subeqn]{article}
%     \outputonly{1,3, 7-12 16 17}
%
% which will allow only pages 1,3,7,8,9,10,11,12,16,17 to be output.
% For plain TeX, use \input{selectp.sty}.
% 
% The number list should consist of numbers and number ranges (7-12, e.g.)
% separated by commas or spaces. The order is not random but MUST BE THE
% ORDER THAT THE PAGES ARE PRODUCED; normally this means the numbers must
% always increase.  If an invalid page number appears on the list, no pages
% will be output until the list is re-synchronized or the document ends.
% 
% Any time the document's page number does not increment normally,
% selectp.sty tries to re-synchronize the page number with the \outputonly
% list of numbers.  This is necessary when \includeonly is used. 
% 
% Pages that are labeled with roman numerals or letters should still be
% listed with a normal arabic number; page xi should be referred to as 6.
% 
% In LaTeX, unfortunately, pages i and 1 are both number 1, so it is
% tricky to skip over all the roman-numbered pages and then print
% page 1.  If there are 3 roman-numbered pages,
%   1-3      will print pages i,ii,iii; not pages 1,2,3
%   1-3,1-5  will print pages i,ii,iii,1,2,3,4,5
%   1-3,8-12 will print pages i,ii,iii,8,9,10,11,19
%   1,1-5    will print i,1,2,3,4,5
%   1-6      prints pages i,ii,iii only, not pages 4,5,6  !!!
%   0,2-6    prints pages 2,3,4,5,6, since there is no page 0 and the
%            outputonly page list is resynchronized when the actual page
%            number goes back to 1)
% 
% The last example is illuminating.  LaTeX looks for page 0, but can't find
% it. It skips pages until the page number goes from 3(iii) to 1, at which
% point it resynchronizes by reading "2" from the list, waiting for page 2
% to be produced, and then writing page 2. Selectp then reads "-6", and
% CONTINUES outputting pages until it has done page 6.  As long as the
% numbers of the intervening pages are less than 6 (the end of the range),
% those pages will be printed. 
% 
% If there are no roman numeral pages, the number list is simple, because
% the numbers must increase monotonically.
%
% LaTeX will not write auxilliary files while selecting output pages, so
% the cross references and citations must be correct on the run BEFORE 
% using \outputonly.  Unfortunately, this means you must produce the 
% full-size DVI file at least once.
% 
% If multuple \outputonly commands are given, the lists are concatenated.
% 
% In plain TeX, specify roman numeral pages as negative numbers, but do not
% use ranges until getting to the ordinary (arabic) page numbers (e.g.,
% \outputonly{-1,-2,-3,-4, 1-8}).  In plain TeX specifying page 1 will NOT
% print page i. 
% 
% If a page number in the list is not found, generally no more pages will be
% printed. For example, suppose a document has 50 pages, the list 3 5 910 13
% (which perhaps should read "9 10") causes LaTeX to process silently
% through the whole document looking for page 910.  Only pages 3 and 5 will
% be printed. 
% 
% EXCEPT if the number terminating a RANGE is not found {3 5-910 13},
% LaTeX continues outputting pages until the end.
% 
% EXCEPT if the actual page number changes discontinuously, then the number
% list MAY be scanned for the next number greater than the new page number.
% (Jumps are usually due to the numbering being changed from roman to arabic
% with the page reset to 1, or because an included file was skipped due to
% an \includeonly command.) The list is scanned if the page number jumps
% backward, or if it jumps forward PAST the current target page. ("Target
% page" = the end of the current range or the next number to be printed out,
% as appropriate.) 
% 
% Numbers must be integers: 4.3 is illegal, even if some other style file
% is generating page numbers in that format.
% 
% Of course non-numbers are illegal.  Some plausible mistakes:
%      page, xvii, IV, 12--16, 49ff, 49-, 5_7.  
% 5_7 could be a typo for the range 5-7.
% To specify page 49 and all following ("49ff" or "49-"), use a range 
% ending with a non-existent page:  49-99999.  
% The typo 12--16 prints pages 12 and 16, not the range 12 through 16 as
% intended. 
% 
% EXAMPLES OF USE
% 
% Suppose a document has pages i,ii,iii,iv,v,vi,1,2,3,...,33, but pages
% 22...28 are absent due to \includeonly; here are some examples of valid
% uses of \outputonly. 
% 
% \outputonly{15-20}        -- prints only the 6 pages 15...20
% \outputonly{1-3}          -- prints pages i,ii,iii (in LaTeX)
%                           -- plain TeX prints pages 1,2,3
% \outputonly{1,1,2}        -- prints pages i,1,2
% \outputonly{0,1,2}        -- prints pages 1,2
% \outputonly{1,3-6}        -- prints pages i,iii,iv,v,vi
% \outputonly{4,3-6}        -- prints pages iv,3,4,5,6
% \outputonly{5,2}          -- prints pages v,2
% \outputonly{5-2}          -- prints only page v    only!
% \outputonly{5-7}          -- prints pages v,vi     only!
% \outputonly{1-33}         -- prints pages i,ii,iii,iv,v,vi  only!
% \outputonly{29-99}        -- prints pages 29,30,31,32,33
% \outputonly{19-99}        -- prints pages 19,20,21,29,30,31,32,33
% \outputonly{24-32}        -- prints pages 29,30,31,32
% \outputonly{15-25,32}     -- prints pages 15,16,17,18,19,20,21,32
% \outputonly{0,4,23-27,31-99}  -- prints pages 4,31,32,33
%
% Version 0.9 (test), Sept 25, 1992: 
%
% Send problem reports to asnd@triumf.ca
%
% test integrity:
% brackets: round, square, curly, angle:   () [] {} <>
% backslash, slash, vertical, hat, tilde:   \ / | ^ ~