% \iffalse meta-comment
%
% pagesel.dtx
%
% This file is part of the package `pagesel' for use with LaTeX2e.
%
% Function: Selecting shipout output pages.
%
% Copyright (C) 1999, 2003 Heiko Oberdiek.
%
% This program 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.
%
% Please send error reports and suggestions for improvements to
%   Heiko Oberdiek <oberdiek@uni-freiburg.de>.
%
%<*dtx>
          \ProvidesFile{pagesel.dtx}
%</dtx>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{pagesel}
%<driver>\ProvidesFile{pagesel.drv}
% \fi
%         \ProvidesFile{pagesel.dtx}
  [2003/06/05 v1.2 Selecting shipout output pages (HO)]
%
% \GetFileInfo{pagesel.dtx}
%
% \CheckSum{216}
%
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%%
% \iffalse
%<*driver>
\documentclass{ltxdoc}
\usepackage{holtxdoc}
\begin{document}
   \DocInput{pagesel.dtx}
\end{document}
%</driver>
% \fi
%
% \DoNotIndex{\maxdimen,\\,\END}
% \DoNotIndex{\ifodd,\ifx,\ifnum}
%
% \newenvironment{param}{^^A
%   \newcommand{\entry}[1]{\meta{\###1}:&}^^A
%   \begin{tabular}[t]{@{}l@{ }l@{}}^^A
% }{^^A
%   \end{tabular}^^A
% }
%
% \newcommand*{\Option}[1]{\textsf{#1}}
%
% \SetupTitle{page,select,shipout,LaTeX}
% \maketitle
%
% \begin{abstract}
%    With this package single pages or page areas can be selected
%    for output.
% \end{abstract}
%
% \tableofcontents
%
% \section{Usage}
%    The package \Package{pagesel} is a \LaTeXe\ package:
%    \begin{quote}
%      |\usepackage|\oarg{options}|{pagesel}|
%    \end{quote}
%    (For plain\TeX\ and \LaTeX\,2.09 the similar package
%    \URL{\Package{selectp}}^^A
%    {ftp://ftp.dante.de/tex-archive/macros/latex209/contrib/misc/selectp.sty}
%    from \Email[Donald Arsenau]{asnd@triumf.ca} can be used.)
%
%    Depending on the options the package works in two modes:
%    \begin{enumerate}
%    \item If no page selecting option is present, so the package
%          ignores the other options and finishes itself. So no
%          page will be suppressed by the package and auxiliary files
%          will be written.
%    \item With at least one page selecting option the specified
%          pages are selected and the other are suppressed.
%          The default for this mode is that auxiliary will not be
%          overwritten. (This can be changed by an option.)
%    \end{enumerate}
%
% \subsection{Page selecting}
%    The package \Package{pagesel} sets up a new counter that is
%    incremented by each \cmd{\shipout}.
%    In this way the package counts the output pages regardless the value
%    of the page counter. So each page can individually by addressed,
%    even if there are several pages with the same page number.
%
% \subsubsection{Options\texorpdfstring{ for selecting pages}{}}
%    \begin{description}
%    \item[\Option{odd}:] The output pages must have an odd number.
%         All even output pages are suppressed. If there are no
%         page areas specified so all odd pages are print. With
%         page areas only the odd pages in this areas are selected.
%    \item[\Option{even}:] The opposite of option \Option{odd}.
%    \item[Page area:] A page area consists of three elements:
%         the starting output page number, an ``area'' hyphen, and
%         the output page number of the last page in this area.
%         Each component is optional, so there are four kinds
%         to spezify a page area:
%         \begin{description}
%         \item[\meta{m}\Option{-}\meta{n}:] All pages between
%              \meta{m} and \meta{n} inclusive.
%         \item[\Option{-}\meta{n}:] All pages until \meta{n} inclusive.
%         \item[\meta{m}\Option{-}:] The page area starts with \meta{m}
%              and all pages to the end of document are selected.
%         \item[\Option{-}:] All pages (not very useful).
%         \item[\meta{s}:] The single page \meta{s}.
%         \end{description}
%    \end{description}
%
% \subsubsection{Examples}
%    \newcommand*{\exam}[1]{\texttt{\strut[#1]}}
%    \begin{tabular}{ll}
%      Options & Output pages\\
%      \hline
%      \exam{1, 4, 9}&  1, 4, and 9\\
%      \exam{7-10, 3}&  3, 7, 8, 9, and 10\\
%      \exam{odd, 3-6}& 3, and 5\\
%      \exam{-4, 3, even, 7-8}& 2, 4, and 8\\
%    \end{tabular}
%
% \subsection{Auxiliary files}
%    If a page is suppressed, the \cmd{\write} commands are not
%    performed. Labels, index entries, or entries for the
%    table of contents aren't written. So it is likely that
%    the table of contents, registers, and lists are incomplete.
% \subsubsection{Options\texorpdfstring{ for handling auxiliary files}{}}
%    \begin{description}
%    \item[\Option{nofiles}:] This is the default. Auxiliary files are
%         read but not written or changed.
%    \item[\Option{nonofiles}/\Option{files}:] Auxiliary files are
%         written.
%    \end{description}
% \subsubsection{\texorpdfstring{Package }{}\Package{hyperref}}
%    In old versions of \Package{hyperref} [1999/04/12 v6.55] (and below)
%    there is a bug with \cmd{\nofiles}:
%    \begin{itemize}
%    \item Some ``garbage'' appears on terminal and in the log file.
%          This is harmless and can be ignored.
%    \item The outline auxiliary file \cmd{\jobname.out}, however,
%          is opened and truncated to zero bytes.
%          Version \ref{Hist10} of this package had
%          loaded a patch file \File{hypnofil.tex}, if it detects
%          \Package{hyperref} to get \cmd{\nofiles} work.
%
%          With the new version of \Package{hyperref} [1999/04/13 v6.56]
%          \cmd{\nofiles} works now. Therefore the workaround code
%          is no longer needed and removed.
%    \end{itemize}
%
% \Installation
%
% \StopEventually{}
%
% \section{Implementation}
% \subsection{Package}
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
%    If the package is loaded twice, the package code does not
%    work. So stop loading the package, if it is already loaded.
%    \begin{macrocode}
\@ifundefined{ps@makevoid}{}{%
  \PackageWarningNoLine{pagesel}{Package already loaded.}%
  \endinput
}
%    \end{macrocode}
%
%    \begin{macro}{\ps@makevoid}
%    Macro \cmd{\ps@makevoid} clears the output box. Because
%    nothing is shipped out and this is intended, we reduce
%    the counter \cmd{\deadcycles} in order to avoid problems, if
%    more than \cmd{\maxdeadcycles} pages are omitted.
%    \begin{macrocode}
\newcommand*{\ps@makevoid}{%
  \global\setbox\@cclv\copy\voidb@x
  \begingroup
    \count@=\deadcycles
    \advance\count@ by -1\relax
    \deadcycles=\count@
  \endgroup
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\ps@oddpages}
%    \begin{macrocode}
\newcommand*\ps@oddpages{0}
\DeclareOption{odd}{\renewcommand*\ps@oddpages{1}}
\DeclareOption{even}{\renewcommand*\ps@oddpages{2}}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macrocode}
\DeclareOption{nofiles}{\let\ps@nofiles\nofiles}
\DeclareOption{nonofiles}{\let\ps@nofiles\@empty}
\DeclareOption{files}{\let\ps@nofiles\@empty}
\ExecuteOptions{nofiles}
%    \end{macrocode}
%
%    \begin{macrocode}
\DeclareOption*{%
  \begingroup
    \expandafter\ps@checkoption\CurrentOption-\END
    \edef\x{\endgroup\noexpand\ps@store{\ps@first}{\ps@last}}%
  \x
}
%    \end{macrocode}
%
%    \begin{macro}{\ps@checkoption}
%    \begin{macrocode}
\newcommand\ps@checkoption{}
\def\ps@checkoption#1-#2\END{%
  \ifx\\#2\\%
    \ifx\\#1\\%
      % empty option
      \def\ps@first{\maxdimen}%
      \def\ps@last{\maxdimen}%
    \else
      \edef\ps@first{#1}%
      \edef\ps@last{#1}%
    \fi
  \else
    \ifx\\#1\\%
      \def\ps@first{-\maxdimen}%
    \else
      \edef\ps@first{#1}%
    \fi
    \ps@checklast#2%
  \fi
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\ps@checklast}
%    \begin{macrocode}
\newcommand\ps@checklast{}
\def\ps@checklast#1-{%
  \ifx\\#1\\%
    \def\ps@last{\maxdimen}%
  \else
    \edef\ps@last{#1}%
  \fi
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\ps@store}
%    \begin{macrocode}
\newcommand*{\ps@store}[2]{%
  \expandafter\def\expandafter\ps@testlist\expandafter{%
    \ps@testlist\ps@pagetest{#1}{#2}%
  }%
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\ps@testlist}
%    \begin{macrocode}
\newcommand*\ps@testlist{}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macrocode}
\ProcessOptions
%    \end{macrocode}
%
%    \begin{macrocode}
\begingroup
  \edef\x{%
    \ifnum\ps@oddpages>0 \relax\fi
    \ifx\ps@testlist\@empty\else\relax\fi
  }%
  \ifx\x\@empty
    \endgroup
    \PackageInfo{pagesel}{Nothing to do}
    \expandafter\endinput
  \fi
\endgroup
%    \end{macrocode}
%
%    \begin{macrocode}
\RequirePackage{everyshi}
%    \end{macrocode}
%
%    \begin{macrocode}
\ps@nofiles
%    \end{macrocode}
%
%    \begin{macro}{\c@ps@count}
%    \begin{macrocode}
\newcounter{ps@count}
\setcounter{ps@count}{0}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\ps@ReturnAfterElseFi}
%    \begin{macro}{\ps@ReturnAfterFi}
%    \begin{macrocode}
\long\def\ps@ReturnAfterElseFi#1\else#2\fi{\fi#1}
\long\def\ps@ReturnAfterFi#1\fi{\fi#1}
%    \end{macrocode}
%    \end{macro}
%    \end{macro}
%
%    \begin{macro}{\ps@ifinset}
%    \begin{macrocode}
\newcommand{\ps@ifinset}[4]{%
  \ifnum#1>\value{ps@count}%
    \ps@ReturnAfterElseFi{#4}%
  \else
    \ps@ReturnAfterFi{%
      \ifnum#2<\value{ps@count}%
        \ps@ReturnAfterElseFi{#4}%
      \else
        \ps@ReturnAfterFi{#3}%
      \fi
    }%
  \fi
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\ps@pagetest}
%    \begin{macrocode}
\newcommand*{\ps@pagetest}[2]{%
  \ps@ifinset{#1}{#2}{\let\ps@next\@empty}{}
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macrocode}
\EveryShipout{%
  \stepcounter{ps@count}%
  \let\ps@next\@empty
  \ifx\ps@testlist\@empty
  \else
    \let\ps@next\ps@makevoid
    \ps@testlist
  \fi
  \ifnum\ps@oddpages=1
    \ifodd\value{ps@count}%
    \else
      \let\ps@next\ps@makevoid
    \fi
  \fi
  \ifnum\ps@oddpages=2
    \ifodd\value{ps@count}%
      \let\ps@next\ps@makevoid
    \else
    \fi
  \fi
  \ps@next
}
%    \end{macrocode}
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \StartHistory
% \HistVersion{1999/03/01 v0.9}
%   \begin{itemize}
%   \item The first version was built as a response to a question
%     of \Email[Dirk Kuypers]{dk@comnets.rwth-aachen.de},
%     published in the newsgroup
%     \href{news:de.comp.text.tex}{de.comp.text.tex}:\\
%     \URL{``Re: pdflatex nur fuer bestimmte Seiten?!?''}^^A
%     {http://www.dejanews.com/[ST_rn=ps]/getdoc.xp?AN=450111880}
%   \end{itemize}
% \HistVersion{1999/04/05 v1.0}\HistLabel{Hist10}
%   \begin{itemize}
%   \item Documentation added in dtx format.
%   \item Copyright: LPPL (\CTAN{macros/latex/base/lppl.txt})
%   \item Options |odd|, |even| added.
%   \item \cmd{\nofiles} added, bug fix for \Package{hyperref}.
%   \item Abort loading of package, if nothing to do.
%   \end{itemize}
% \HistVersion{1999/04/13 v1.1}
%   \begin{itemize}
%   \item \cmd{\nofiles} bug fix removed
%         because of \Package{hyperref} 6.55.
%   \item First CTAN release.
%   \end{itemize}
% \HistVersion{2003/06/05 v1.2}
%   \begin{itemize}
%   \item \cmd{\deadcyles} is decremented for omitted pages.
%   \item LPPL 1.2.
%   \end{itemize}
% \hfuzz10pt
% \PrintIndex
%
% \Finale
\endinput
