% \CheckSum{440}
% \iffalse meta-comment
% (the version number in the next line is an internal CVS version!)
% $Id: volumes.dtx,v 1.31 2004/06/18 13:57:47 frank Exp $
% ======================================================================
% volumes.dtx 
% Copyright (C) 2004 Frank Küster
%
% This file can be redistributed and/or modified under the terms
% of the LaTeX Project Public Licence Version 1.3 or later, at your choice.
% See: http://www.latex-project.org/lppl/
% ----------------------------------------------------------------------
% volumes.dtx
% Copyright (C) 2004 Frank Küster
%
% Diese Datei kann nach den Regeln der LaTeX Project Public
% Licence Version 1.3 oder neuer weiterverbreitet und/oder modifiziert 
% werden.
%
% ======================================================================
%
% \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         \~}
% \fi
% \iffalse
%<*dtx>
            \ProvidesFile{volumes.dtx}
%</dtx>
%\NeedsTeXFormat{LaTeX2e}[1995/12/01]
%<driver>\ProvidesFile{volumes.drv}
%<volumes>\ProvidesPackage{volumes}
%<*driver|dtx|volumes>
            [2004/06/02 v1.0 print only parts of a LaTeX document,
            with complete indices etc.]
%</driver|dtx|volumes>
%<*nowtoaux>
\ProvidesPackage{nowtoaux}[2004/06/14 v1.0 write to the aux file immediately]
%</nowtoaux>
%<*driver>
%% \documentclass{article}
%% \usepackage{doc}
\documentclass{ltxdoc}
\EnableCrossrefs
\DoNotIndex{\@for,\@gobble,\@gobblefour,\@ifnextchar,\@ifpackageloaded,\@ifundefined,\@tempcnta}
\DoNotIndex{\@tempswatrue,\@tempswafalse,\@unexpandable@protect,\@whilenum}
\DoNotIndex{\begingroup,\csname,\DeclareOption,\def,\do}
\DoNotIndex{\edef,\if,\else,\fi,\endcsname,\endgroup,\expandafter,\glossary}
\DoNotIndex{\if@nobreak,\if@tempswa,\ifnum,\ifvmode,\ifx,\fi,\fi,\fi,\fi,\fi,\index}
\DoNotIndex{\label,\let,\long,\MessageBreak,\newcommand,\newcounter,\newif,\nobreak}
\DoNotIndex{\PackageError,\PackageWarningNoLine,\protect,\ProcessOptions,\providecommand,\relax,\RequirePackage,\reserved@a}
\DoNotIndex{\setcounter,\space,\stepcounter,\string,\undefined}
\CodelineIndex
%
\begin{document}
 \DocInput{volumes.dtx}
\end{document}
%</driver>
% \fi
% 
% \newcommand*{\EmacsCheckParens}[1]{}
% \GetFileInfo{volumes.dtx}
% \RecordChanges
%
% \MacroIndent=1em
% \setcounter{IndexColumns}{2}
% 
% \MakeShortVerb{\|}
% \MakeShortVerb{\§}
% \newcommand{\package}[1]{\textsf{#1}}
% \renewcommand{\partname}{Part}
%
% \changes{v1.0}{2004/06/02}{initial public release}
% \title{volumes.sty: Support for Printing of only parts of a LaTeX
% document, with complete indices etc. }
% \author{Frank K\"uster}
% \date{\filedate}
% \maketitle{}
%
% \begin{abstract}
%   \noindent This package tries to help you if you want to produce
%   separate printed volumes from one LaTeX document, as well as one
%   comprehensive "all inclusive" version. It suppresses the parts of
%   the table of contents that are not typeset, while counters,
%   definitions, index entries etc. are kept consistent throughout the
%   input file. The same goal can probably also be achieved by
%   different approaches, e.g. packages to combine separate documents
%   in one typeset version. But for me it was easier this way -- I
%   define lots of macros in the document on-the-fly, and have to
%   avoid double definitions in the different parts.
%
%   This file also provides \package{nowtoaux.sty} which allows writing
%   to the |aux| file, and thus to the |toc|, |lof| and |lot| files,
%   without waiting for a page to be shipped out.
% \end{abstract}
%
% \tableofcontents{}
%
% \part{User documentation}
% \label{part:user-documentation}
%
%\section{Purpose, alternatives, and credits}
%\label{sec:purpose}
%
% If you use \cmd{\includeonly} to typeset only parts of your
% document, you have only two choices: Either you delete the old aux
% files, and get no information at all about the parts that are not
% typeset, or you keep it, but then you get a full table of contents,
% lists of figures and tables, etc., even for the parts that are not
% included. Thus, \cmd{\includeonly} is only suitable to keep
% compilation times short while writing parts of your document, but
% not for the final typesetting of parts of a document.
% 
% If you want to have a more fine-grained control over what is being
% typeset, \package{volumes.sty} may help you. There are, however,
% some alternatives. If you know in advance that you will want to
% typeset your package in two or more volumes, it might be better to
% develop it as separate documents, using \package{xr.sty} for
% references to the other document, or to use tools as
% \package{shorttoc}. 
%
% When I developed this package, I had yet written a large part of the
% document -- in fact it was a laboratory diary (using my own
% \package{labbook} class) -- and was using lots of automated indexing
% commands that occurred in all parts, and macros that defined and
% re-used command sequences on-the-fly. I wanted those commands to be
% consistent throughout my work in this lab, avoiding dobule
% definitions, and I wanted the complete index, also for older
% volumes, in every volume. Therefore it seemed harder to take apart
% the whole document, than suppressing a part of the table of
% contents.
% 
% In a discussion with Markus Kohm, he came up with the basic idea for
% this, and I am much in debt to him for his help. Some definitions
% where also taken and/or adapted from his \package{scrclass.dtx}, as
% well as from basic \LaTeXe{} files, and the contributors of the
% newsgroup \texttt{de.comp.text.tex} also helped me, as usual.
%
%\section{Using the \package{volumes} Package}
%\label{sec:using-volumes}
%
% \DescribeMacro{\onlyvolume}
% To use the Package, you must at least load it and call the
% \cmd{\onlyvolumes} macro:
% \begin{verbatim}
% \usepackage{volumes}
% \onlyvolume
% \end{verbatim}
% The reason why you need two commands is that it makes it possible to
% customize it's behavior between the \cmd{\usepackage} and
% \cmd{\onlyvolume} (see below). 
%
% \package{volumes.sty} assumes that you are using \cmd{\include} for
% the parts that you want to typeset conditionally, and standardized,
% numbered filenames (see below). However, the appearance of your
% document still won't change if you insert just the lines above. You
% also need to specify which part should be typeset. There are two
% ways to accomplish that:
% \begin{enumerate}
% \item You can specify the part you want as an optional argument to
% \cmd{\onlyvolume}, for example:
% \begin{verbatim}
% \onlyvolume[2]
% \end{verbatim}
% to get the second part. The disadvantage for this is that you have
% to change the document when you want to change the part to be
% typeset. 
% \item \DescribeMacro{\volume}Alternatively, you can define the macro
%   \cmd{\volume} outside the file, it should expand to the number of
%   the part you want. This can be done on the command line, e.g. like
%   this:
% \begin{verbatim}
% latex "\def\volume{2} \input{filename}"
% \end{verbatim}
% \end{enumerate}
%
% \package{volumes.sty} will handle each file that is included with
% \cmd{\include} as one volume, and treat the rest as fixed parts. It
% does not take care of \cmd{\input} at all, therefore you're free to
% use it as you want.
%
%\subsection{Customization}
%\label{sec:custom}
%
%
%\paragraph{Filenames}
%\label{sec:filenames}
%
% \DescribeMacro{\volumename}
% By default, \package{volumes.sty} expects the included
% files to be named |volume1.tex|, |volume2.tex| etc.\footnote{In fact
% this is only true for conditionally included files. If you use
% \cmd{\alwaysinclude} (see below), those files can have arbitrary
% names.} The numbers at the end must always be there, but you can
% change the basename from |volume| to \meta{anything} else using
% 
% \cmd{\volumename}\marg{anything}
% 
%
%\paragraph{Commands specific to volumes}
%\label{sec:volumecommands}
%
% \DescribeMacro{\allvolumescommand}\DescribeMacro{\volumecommand} You
% might want certain commands only to be executed when the whole
% document is typeset, and others only for a specific volume. To
% achieve this, write \cmd{\allvolumescommand}\marg{commands} and/or
% \cmd{\volumecommand}\marg{number}\marg{commands}. 
%
% The commands are currently\footnote{In future versions, this might
%   be moved to an other place - don't rely on that.}
% executed at the end of the preamble using
% \cmd{\AtBeginDocument}. Therefore you cannot typeset anything,
% instead you should define commands that are later typeset,
% e.g. change the \cmd{\title} or \cmd{\date}.
%
%\paragraph{Excluding \cmd{\addtocontents} commands}
%\label{sec:newaddtoc}
%
% The mechanisms presented so far only exclude things added to the
% |toc|, |lof|, or |lot| files by sectioning commands or floats, or
% using \cmd{\addcontentsline}. If you add some stuff manually to
% those lists using \cmd{\addtocontents}, it still gets typeset.
%
% To circumvent this, you can use \cmd{\voladdtotoc}\marg{Text},
% \cmd{\voladdtolof}\marg{Text}, and \cmd{\voladdtolot}\marg{Text},
% instead of \cmd{\addtocontents}\marg{Text}. These texts will
% automatically be suppressed if the respective list is suppressed in
% the part where they are used.
%
%
%
%\subsection{Options: Determining what to exclude}
%\label{sec:options-using}
%
% \package{volumes.sty} by default suppresses the unprinted entries
% into the Table of Contents (|lof|), List of Figures (|lof|) and List
% of Tables (|lot|). You can change this using the following options:
% \begin{description}
% \item[|tocall|] The complete table of contents is printed even when
%   only typesetting one part, i.e. only the lists of tables and of
%   figures is suppressed
% \item[|lofall|] The complete list of figures is printed even when
%   only typesetting one part, i.e. only the lists of tables and table
%   of contents is suppressed.
% \item[|lotall|] The complete list of tables is printed even when
%   only typesetting one part, i.e. only the lists of figures and
%   table of contents is suppressed.
% \end{description}
% \DescribeMacro{\volumeone}\DescribeMacro{\volumetwo}\DescribeMacro{\volumethree}\DescribeMacro{...}
% We redefine the \cmd{\include} command to achieve this. If this
% causes incompatibilities with other packages, use the option
% \begin{description}
% \item[|manual|] You must then specify the beginning of a volume by
%   putting the commands \cmd{\volumeone}, \cmd{\volumetwo},
%   \cmd{\volumethree} and so forth directly before the corresponding
%   \cmd{\include}. The filenames still need to follow the conventions
%   described above.
% \end{description}
%
%
%
%\subsection{Future plans}
%\label{sec:future}
%
%Here are some ideas that I had, and believe can be implemented quite
%easily -- but I didn't have time yet:
%\begin{itemize}
% \item Check whether the filename in \cmd{\include} maches the
%   pattern \cmd{\volumename}\meta{number} and include it always if it
%   does not.
% \item Provide a user interface that allows to suppress entries in
%   other lists, like lists of equations, listings, or user-defined
%   floats. 
%\end{itemize}
%
%\section{The \package{nowtoaux} Package}
%\label{sec:nowtoaux-usage}
%
% The \cmd{\addtocontents} macro uses the \TeX\ primitive \cmd{\write}
% to insert its text into the |aux| file, from where it will finally
% end up in the |toc| file etc. \cmd{\write} puts its contents into a
% \emph{whatsit}, and the actual writing is done when the material
% currently processed fills a page, and the page is shipped out. This
% way, putting \cmd{\thepage} into the arguments of
% \cmd{\addtocontents} will produce the correct page number. However,
% sometimes one really wants the writing to take place immediately,
% when the macro is expanded. This can be achieved with the
% \package{nowtoaux} package.
%
% Consider the following example, which tries to typeset the table of
% contents in the main part of the document in red:
% \begin{verbatim}
% \documentclass{report}
% \usepackage{color}
% \begin{document}
%
% \tableofcontents
%
% \include{intro}
% \addtocontents{toc}{\color{red}}
% \include{main}
% \end{document}
% \end{verbatim}
%
% Here, the \cmd{\addtocontents} command is at a place where nothing
% is typeset: The previous \cmd{\include} has just caused a page
% break, and the next include will open its own |aux| file, before
% also doing a \cmd{\clearpage} and starting the typesetting, probably
% of many pages with sections that go into the toc. It's only after
% the processing comes back from |main.tex| that \TeX{} notices that a
% \emph{whatsit} is left, and writes to the |aux| file.
%
% \DescribeMacro{\immediateaddtocontents}
% What you want instead is a command like \cmd{\addtocontents} that
% writes immediately to the |aux| file, without waiting for a page
% shipout. The \package{nowaux} package defines
% \cmd{\immediateaddtocontents}\marg{table}\marg{Text} which does
% exactly that. If you substitute it for the \cmd{\addtocontents} in
% the above example, the entries in the table of contents that come
% from |main.tex| are colored red (how horrible\dots).
%
% \DescribeMacro{\immediatewriteaux} You can also write arbitrary
% commands to the aux file, if you find a use for this.
% \cmd{\writeaux}\marg{commands} writes the commands to the aux file,
% using a \emph{whatsit} as usual.
% \cmd{\immediatewriteaux}\marg{commands} does the same, but with
% \cmd{\immediate}. The commands in their argument need to be properly
% protected. It is best to define one command to perform all the tasks
% that you want to be done in the aux file. You can then put this
% command into the aux file with \cmd{\macrotoaux}\marg{one\_command},
% without further protection, because this is done internally. Here's
% an example:
%
%\section{The test suite}
%\label{sec:testsuite}
%
% The principles of the test suite will be described elsewhere. Here
% are just some remarks on how it works in this particular case.
% \begin{itemize}
% \item In the first run, the file switch-onlytwo.tex does not
% exist. Therefore, \cmd{\volume} is undefined, and the complete
% document is processed, producing aux files also for the files that
% will not be included in later runs.
% \end{itemize}
%
% \StopEventually{\PrintIndex\PrintChanges}
%
% \part{Implementation}
%
%
%
%\section{The \package{nowtoaux} package}
%\label{sec:nowtoaux}
%
% \begin{macro}{\immediateaddtocontents}
% \begin{macro}{\immediate@protected@write}
% The definitions of \cmd{\immediateaddtocontents} and
% \cmd{\immediate@protected@write} are taken from the definition of
% \cmd{\addtocontents} in \package{ltsect.dtx} and \cmd{\protected@write}
% in \package{ltfiles.dtx}, and the only change is the addition of
% \cmd{\immediate} before \cmd{\write}.
%
% 
%    \begin{macrocode}
%<*nowtoaux>
\long\def\immediateaddtocontents#1#2{%
  \immediate@protected@write\@auxout
  {\let\label\@gobble \let\index\@gobble \let\glossary\@gobble}%
  {\string\@writefile{#1}{#2}}
}
\long\def \immediate@protected@write#1#2#3{%
  \begingroup
  \let\thepage\relax
  #2%
  \let\protect\@unexpandable@protect
  \edef\reserved@a{\immediate\write#1{#3}}%
  \reserved@a
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi 
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\macrotoaux}
% \begin{macro}{\immediatewriteaux}
% \begin{macro}{\writeaux}
%   Here come three goodies: \cmd{\immediatewriteaux} allows you to
%   write arbitrary commands to the |aux| file. \cmd{\writeaux} does
%   the same, but in the standard way, without \cmd{\immediate}. The
%   commands in their argument need to be properly protected. It is
%   best to define one command to perform all the tasks that you want
%   to be done in the aux file. You can then put this command into the
%   aux file with \cmd{\macrotoaux}, without further protection,
%   because this is done internally.
%
%    \begin{macrocode}
\long\def\writeaux#1{%
  \protected@write\@auxout
  {\let\label\@gobble \let\index\@gobble \let\glossary\@gobble}%
  {#1}
}
\long\def\immediatewriteaux#1{%
  \immediate@protected@write\@auxout
  {\let\label\@gobble \let\index\@gobble \let\glossary\@gobble}%
  {#1}
}
\long\def\macrotoaux#1{%
  \immediatewriteaux{\string#1}
}
%</nowtoaux>
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% 
%\section{The \package{volumes} Package}
%\label{sec:volumes-impl}
%
%\subsection{Option handling and needed packages}
%\label{sec:options}
%
% For the all options, we simply create a conditional that is checked
% later, when the necessary commands have been or will be
% defined. Furthermore, we need the \package{nowtoaux} helper package.
% 
%    \begin{macrocode}
%<*volumes>
\newif\if@allincludes\@allincludestrue
\newif\if@tocall\@tocallfalse
\newif\if@lofall\@lofallfalse
\newif\if@lotall\@lotallfalse
\DeclareOption{manual}{\@allincludesfalse}
\DeclareOption{tocall}{\@tocalltrue}
\DeclareOption{lofall}{\@lofalltrue}
\DeclareOption{lotall}{\@lotalltrue}
\ProcessOptions%
\RequirePackage{nowtoaux}
%    \end{macrocode}
%
%\subsection{Suppressing \cmd{\addtocontents} commands}
%\label{sec:voladdtotoc}
%
%    \begin{macrocode}
\newcommand{\vol@do@addto}[1]{#1}
\let\vol@dont@addto\@gobble
\newcommand{\voladdtotoc}[1]{%
  \addtocontents{toc}{\protect\vol@maybe@addto@toc{#1}}%
}%
\let\vol@maybe@addto@toc\vol@do@addto
\newcommand{\voladdtolof}[1]{%
  \addtocontents{lof}{\protect\vol@maybe@addto@lof{#1}}%
}%
\let\vol@maybe@addto@lof\vol@do@addto
\newcommand{\voladdtolot}[1]{%
  \addtocontents{lot}{\protect\vol@maybe@addto@lot{#1}}%
}%
\let\vol@maybe@addto@lot\vol@do@addto
%    \end{macrocode}
%
%\subsection{Suppressing entries in the table of contents}
%\label{sec:notoc}
%
% In order to get the table of contents, list of figures and list of
% tables with entries only for the part that we want to print, we
% redefine |\contentsline| to do nothing. This is done by writing
% |\let| assignments into the respective files (|*.toc|, |*.lof|,
% |*.lot|).
%
% \begin{macro}{\contentsline}
% \begin{macro}{\volumes@orig@contentsline}
%   But we need to be able to suppress parts at the beginning, and
%   reenable later parts. Therefore we save the old definition of
%   \cmd{\contentsline} in the macro |\volumes@orig@contentsline|. If
%   the \package{hyperref} Package will be loaded later, it will
%   redefine |\contentsline|.  Therefore we have to repeat our command
%   after (and only if) \package{hyperref} has been loaded:
%
%    \begin{macrocode}
\let\volumes@orig@contentsline\contentsline \RequirePackage{scrlfile}
\AfterPackage{hyperref}{\let\volumes@orig@contentsline\contentsline}
%    \end{macrocode}
%
% The new |\contentsline| macro should do nothing, but just gobble its
% arguments. Again a \package{hyperref} problem: If the package has
% been loaded, |\contentsline| will have 4 arguments instead of 3. We
% do the same trick, but this time we also have to check whether
% \package{hyperref} has been loaded yet:
%
%    \begin{macrocode}
\@ifpackageloaded{hyperref}{%
  \let\volumes@new@contentsline\@gobblefour
}{%
  \def\volumes@new@contentsline#1#2#3{}
  \AfterPackage{hyperref}{%
    \let\volumes@new@contentsline\@gobblefour
  }
}
%    \end{macrocode}
% 
% We want to |\let| the |\contentsline| macro to the meaning we want
% in the |*.toc| file etc., but we cannot write directly to those
% files. Instead, we write to the |*.aux| file using
% \cmd{\immediateaddtocontents}. Commands written to the |aux| and
% |toc| file this way need to be proctected; in order to make this
% easier, we define macros that will do the |\let| assignment and can
% be proctected with one |\protect| command:
%
%    \begin{macrocode}
\def\volumes@switch@orig@contentsline@toc{%
  \let\contentsline\volumes@orig@contentsline%
  \let\vol@maybe@addto@toc\vol@do@addto
}
\def\volumes@switch@new@contentsline@toc{%
  \let\contentsline\volumes@new@contentsline%
  \let\vol@maybe@addto@toc\vol@dont@addto
}
\def\volumes@switch@orig@contentsline@lof{%
  \let\contentsline\volumes@orig@contentsline%
  \let\vol@maybe@addto@lof\vol@do@addto
}
\def\volumes@switch@new@contentsline@lof{%
  \let\contentsline\volumes@new@contentsline%
  \let\vol@maybe@addto@lof\vol@dont@addto
}
\def\volumes@switch@orig@contentsline@lot{%
  \let\contentsline\volumes@orig@contentsline%
  \let\vol@maybe@addto@lot\vol@do@addto
}
\def\volumes@switch@new@contentsline@lot{%
  \let\contentsline\volumes@new@contentsline%
  \let\vol@maybe@addto@lot\vol@dont@addto
}
% \def\volumes@switch@orig@contentsline{%
%   \let\contentsline\volumes@orig@contentsline}
% \def\volumes@switch@new@contentsline{%
%   \let\contentsline\volumes@new@contentsline}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\volume@switch@off}
% \begin{macro}{\volume@switch@on}
%   The actual writing to |*.aux| will done by \cmd{\volume@switch@on}
%   and\linebreak[4] \cmd{\volume@switch@off}. While
%   \cmd{\volume@switch@on} switches on unconditionally,
%   \cmd{\volume@switch@off} checks whether any of the |tocall|,
%   |lofall|, or |lotall| options was given; if writes to the
%   respective file only if the option wasn't given.
%
%    \begin{macrocode}
\def\volume@switch@off{%
  \if@tocall\else%
    \immediateaddtocontents{toc}{\protect\volumes@switch@new@contentsline@toc}%
  \fi%
  \if@lofall\else%
    \immediateaddtocontents{lof}{\protect\volumes@switch@new@contentsline@lof}%
  \fi%
  \if@lotall\else%
    \immediateaddtocontents{lot}{\protect\volumes@switch@new@contentsline@lof}%
  \fi%
}
\def\volume@switch@on{%
  \immediateaddtocontents{toc}{\protect\volumes@switch@orig@contentsline@toc}%
  \immediateaddtocontents{lof}{\protect\volumes@switch@orig@contentsline@lof}%
  \immediateaddtocontents{lot}{\protect\volumes@switch@orig@contentsline@lof}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% 
%\subsection{User commands for the appearance of volumes}
%\label{sec:conditional-usercomm}
%
%
% \begin{macro}{\allvolumescommand}
% \begin{macro}{\volumecommand} In order to allow the user to define
%   commands that should be executed conditionally only if a
%   particular volume is typeset, we provide the command
%   \cmd{\volumecommand}\marg{number}\marg{\LaTeX\ commands}.
%   Similarly, the macro\linebreak[4]
%   \cmd{\allvolumescommand}\marg{\LaTeX{} commands} is executed when
%   the whole document is typeset.  The internal commands that are
%   generated by both macros need to be set to \cmd{\relax}, so that
%   no error occurs if the user does not use (some of) the
%   \cmd{\volumecommand}s and \cmd{\allvolumescommand}. For the
%   numbered \cmd{\volume@command@..}, this is done later.
%
%    \begin{macrocode}
\def\volumecommand#1#2{%
  \expandafter\def\csname volume@command@#1\endcsname{%
    #2%
  }%
}
\def\allvolumescommand#1{%
  \def\all@volumes@command{#1}%
}
\let\all@volumes@command\relax
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%\subsection{Determining which part(s) to typeset}
%\label{sec:determine-which}
%
%
% Now we define the macro that will finally be used to determine if,
% and which volume is typeset. The key is the macro \cmd{\volume} -- if
% it is undefined, the whole document will be typeset. If it is set to
% a number, the respective volume will be typeset. 
%
%
%\subsubsection{Helper macros: checking for numbers}
%\label{sec:helper-macros}
%
%  \begin{macro}{\ifnumber}
%    In order to test whether \cmd{\volume}, if defined, is in fact a
%    number, we use a command taken from \package{scrclass.dtx}, and
%    explained there (albeit in german):
%
%    \begin{macrocode}
\providecommand\ifnumber[3]{
 \begingroup\@tempswafalse\let\scr@next\test@number
 \expandafter\scr@next#1\scr@next
 \if@tempswa\endgroup#2\else\endgroup#3\fi
}
\providecommand*{\test@number}[1]{
 \ifx \scr@next#1
  \let\scr@next\relax
 \else
  \@tempcnta=\expandafter\expandafter\expandafter\number
  \expandafter`#1\relax
  \ifnum \@tempcnta>47\relax
   \ifnum \@tempcnta<58\relax
    \@tempswatrue
   \else\@tempswafalse\fi
  \else\@tempswafalse\fi
  \if@tempswa\else\let\scr@next\gobble@till@next\fi\fi
 \scr@next}
\providecommand*{\gobble@till@next}{}
\def\gobble@till@next#1\scr@next{}
%    \end{macrocode}
%  \end{macro}
%
%
%\subsubsection{Specifying the basename of included files}
%\label{sec:basename}
%
% \begin{macro}{\volumenname}
% The name of the included files corresponding to the individual
% volumes must end with a number, but it can have any legal filename
% before this. By default, |Buch1.tex|, |Buch2.tex| etc. are used, but
% this is stored in \cmd{\volume@name} and can be changed with the user
% command \cmd{\volumename}:
%
%    \begin{macrocode}
\newcommand*{\volumename}[1]{%
  \def\volume@name{#1}%
}%
\volumename{volume}%
%    \end{macrocode}
% \end{macro}
%
%
%\subsubsection{Specifying constant \cmd{\include}'s}
%\label{sec:constants}
%
% \begin{macro}{\alwaysinclude}
% This macro is used to specify parts that should always be included,
% and can only be used with the |manual| option. 
%    \begin{macrocode}
\newcommand*{\alwaysinclude}[1]{%
  \if@allincludes
    \PackageError{volumes}{%
      need option "manual" for \string\alwaysinclude.%
    }{%
      You must use the option "manual" when you want to use
      \string\alwaysinclude, and\MessageBreak  specify
       the volumes using \string\volumeone\space etc. - see 
       the package documentation. 
    }
  \else
    \def\always@include{,#1}
  \fi
}
\def\always@include{}
%    \end{macrocode}
% \end{macro}
% 
%\subsubsection{Typesetting the right part}
%\label{sec:the-right}
%
% \begin{macro}{\onlyvolume}
% The command that triggers conditional typesetting is
% \cmd{\onlyvolume}. If it is called with a number as optional
% argument, it defines \cmd{\volume} to expand to that number, meaning
% the volume to be typeset. If no optional argument is given,
% \cmd{\volume} is not assigned, but the check whether it is assigned is
% still done. This enables the user to switch between
% typesetting the whole document or only one volume without changing
% the file, by assigning \cmd{\volume} on the command line:
% \begin{verbatim}
% latex "\def\volume{3} \input{filename}"
% \end{verbatim}
% The real work is then done by \cmd{\@@onlyvolume}:
%    \begin{macrocode}
\newcommand*{\onlyvolume}[1]{}
\def\onlyvolume{%
  \@ifnextchar [{\@onlyvolume}{\@@onlyvolume}
}
\def\@onlyvolume[#1]{%
  \ifnumber{#1}{%
    \def\volume{#1}
  }{%
    \PackageError{volumes}{%
      Argument to \string\onlyvolume\space must be a number%
    }{%
      The optional argument to \string\onlyvolume\space is used to
      tell volumes.sty which volume it should typeset. You should have
      specified a number there, but instead, you said: \volume%
    }%
  }%
  \@@onlyvolume
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\volume}
% \begin{macro}{\@@onlyvolume}
%   To define \cmd{\@@onlyvolume}, we first neeed two counters. Then
%   we check whether \cmd{\volume} is defined -- if not, we \cmd{\let}
%   all \cmd{\volume@switch@...}es to \cmd{\relax}. This is done for
%   |numberofvolumes| volumes -- there is no user interface yet to
%   change this value. Then we tell \LaTeX{} to execute
%   \cmd{\all@volumes@command} at the begin of the document
%
%    \begin{macrocode}
\newcounter{volume}%
\newcounter{numberofvolumes}\setcounter{numberofvolumes}{10}%
\def\@@onlyvolume{%
  \ifx\volume\undefined%
    \PackageWarningNoLine{volumes}{Typesetting complete document.}
    \setcounter{volume}{0}%
    \@whilenum\c@volume<\c@numberofvolumes\do
    {%
      \expandafter\let%
      \csname volume@switch@\arabic{volume}\endcsname\relax%
      \stepcounter{volume}%
    }%
    \setcounter{volume}{0}%
    \AtBeginDocument{\all@volumes@command}%
%    \end{macrocode}
%
% If \cmd{\volume} is defined, we \cmd{\let} all
% \cmd{\volume@switch@...}es to \cmd{\volume@switch@off}. Then we
% schedule the execution of the respective \cmd{\volume@command@...}
% to the begin of the document, and make sure only the right part is
% included.
%
%    \begin{macrocode}
  \else
    \ifnumber{\volume}{%
      \PackageWarningNoLine{volumes}{Typesetting part \volume.}  
    }{%
      \PackageError{volumes}{\string\volume\space is defined, but not a
        number}{%
        The macro \string\volume\space is used to tell volumes.sty which
        volume it should typeset. You have defined this macro, but it is
        not a number. Instead, it is: \volume%
      }%
    }%
    \setcounter{volume}{0}%
    \@whilenum\c@volume<\c@numberofvolumes\do {%
      \expandafter\let%
      \csname volume@switch@\arabic{volume}\endcsname%
      \volume@switch@off%
      \stepcounter{volume}%
    }
    \setcounter{volume}{0}%
    \AtBeginDocument{\csname volume@command@\volume\endcsname}
    \expandafter\let%
    \csname volume@switch@\volume\endcsname\volume@switch@on
    \includeonly{\volume@name\volume\always@include}%
  \fi
%    \end{macrocode}
%
% If the |manual| option was given, we define \cmd{\volumeone},
% \cmd{\volumetwo},\dots{} to be the corresponding
% \cmd{\volume@switch@...}.
%
%    \begin{macrocode}
  \if@allincludes\else%
    \@ifundefined{volumelist}{%
      \def\volumelist{one,two,three,four,five,six,seven,eight,nine,ten}%
    }{}%
    \@for\vol@num:=\volumelist\do
    {%
      \stepcounter{volume}%
      \expandafter\let\csname volume\vol@num\expandafter\endcsname%
        \csname volume@switch@\arabic{volume}\endcsname%
    }%
    \setcounter{volume}{0}%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% 
% \begin{macro}{\include}
%   If the |manual| option was not given, we redefine |\include| so
%   that it does the necessary switching. Otherwise, we define
%   commands with no |@| signs as aliases for
%   |\volume@switch@|\meta{number}. If the user wants more then ten
%   volumes, or different names, she can define |\volumelist| herself,
%   but before the package is loaded.
%
%   TODO: Hier die Nummer als include-Argument einarbeiten!
%    \begin{macrocode}
\setcounter{volume}{0}%
\if@allincludes%
  \let\volume@orig@include\include%
  \def\include{%
    \stepcounter{volume}%
    \csname volume@switch@\arabic{volume}\endcsname%
    \volume@orig@include%
  }
\fi
%</volumes>
%    \end{macrocode}
% \end{macro}
% 
% \clearpage
% \Finale
%
\endinput
