% \iffalse
% File: ktv-texdata.dtx
% Copyleft 2003 Ky Anh
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% See the GNU General Public License for more details.
%
% For error reports,
% or offers to help improve the `ktv-texdata' package,
% please mail to
%               Ky` Anh, kyanh@linuxmail.org
% \fi
%
% \iffalse
%<*dtx>
\ProvidesFile{ktv-texdata.dtx}
    [2003/11/20 v1468 KTV, Data Manager]
%</dtx>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{ktv-texdata}
%<package>[2003/10/06 v05.34 KTV, Data Manager]
%<*driver>
\documentclass[oneside]{ltxdoc}
\usepackage{ktv-buildnum}
\CodelineIndex
\RecordChanges
\DocInput{ktv-texdata.dtx}
\begin{thebibliography}{VE}
\addcontentsline{toc}{section}{References}
\bibitem[VE]{VE}
    Victor EIJKHOUT, {\it \TeX\ by Topic}, Addison-Wesley, 1992.
\end{thebibliography}
\end{document}
%</driver>
%
% \fi
%
% \GetFileInfo{ktv-texdata.dtx}
%
% \def\docversion{\the\buildnum}
% \def\docdate{\filedate}
%
% \makeatletter
% \let\@afterindentfalse\@afterindenttrue
% \@afterindenttrue
% \makeatother
%
% \def\pkg{{\bf ktv-texdata}}
% \def\back:{$\backslash$}
% \def\trick:{\DescribeMacro{Trick}{:: \bf TRICK}\hrulefill\par}
%
% \begin{document}
% \title{\pkg\ package}
% \author{K\`{y} Anh\\(\ttfamily{kyanh@inic.biz, kyanh@linuxmail.org})}
% \date{\begin{tabular}{rll}
%   \texttt{package:}
%       &version 05.34,         & last update 2003/10/06
% \\
%   \texttt{documentation:}
%       &version \docversion,   & last update \docdate
% \end{tabular}}
% \maketitle
%
% ^^M\CheckSum{656}
%
% \begin{abstract}
%
% This package provides a simple way to use
% the \TeX\ input files whose contents
% are in the numbered environments.
%
% This package is useful for
% \underline{\bf the teachers of mathematics},
% who often work with large libraries of mathematical exercises.
%
% \end{abstract}
%
% \tableofcontents
%
% \section{How will you do?}
% \label{sec:howwillyoudo}
%
% Assume that you have an input file (named `|foo.tex|')
% that specifies 16 exercies
% \begin{verbatim}
% % --- first line of `foo.tex'
% \begin{exercice}\label{ex:1}
%       This is the first exercice.
% \end{exercice}
%
% \begin{exercice}\label{ex:2}
%       This is the second exercice.
% \end{exercice}
%
% ...
% \begin{exercice}[*]\label{ex:16}
%       This is the 16th exercice
%       (with a star mark *).
% \end{exercice}
% % --- last line of `foo.tex'
% \end{verbatim}
% On Tuesday, for e.g., you want to create a student test
% that contains the first  8 exercises of the `|foo.tex|'.
% However, on Wednesday, you want to create another test
% that contains the last 8 exercises of the `|foo.tex|'.
%
% Of course, the simplest way to do that
% is \emph{copying} and \emph{pasting}.
% Of course, this way becomes too complex in case,
% for e.g., you need only the exercies that are numbered oddly
% (1, 3, 5, 7, 9, 11, 13, 15).
%
% You may think of a solution like this
% \begin{verbatim}
%   \getonly{1,2,3,4,5,6,7,8}        % on Tuesday
%   \getonly{9,10,11,12,13,14,15,16} % on Wednesday
%   \getonly{1,3,5,7,9,11,13,15}     % on Friday
% \end{verbatim}
%
% Yes, here we go....
%
% \section{Introduction}
%
% \subsection{Convention}
%
% Instead of using |#1|, |#2|, etc., to specify
% the first, the second,... parameter or argument of a macro,
% we will use |#foo|, |#xfoo|,... where |#foo| is
% a short description of |#1|,
% and |#xfoo| is a short description of |#2|, etc.
%
% \emph{Parameter} is something declared in the macro's definitons.
%
% \emph{Argument} is something you pass to a macro when you call it.
%
% The optional parameters are enclosed in the brackets (`|[|' and `|]|'). 
% 
% \subsection{Data item `bxx'}
%
% \subsubsection{Form}
% \label{ssec:bxx.form}
%
% Each `|bxx|', or a \emph{data item}, is of the form
% \begin{center}
% \begin{tabular}{l}
%  |\bxx(#env)[#thm]ID;|\\
%  \emph{something to typeset}\\
%  |\exx|
% \end{tabular}
% \end{center}
% Note that `|\exx|' must be located at
% \emph{the beginning of a new line}\\
% and the semicolon `|;|' is mandatory.
%
% \DescribeMacro{ID}
% |ID| is any non-empty string of
% characters, numbers or punctuations:
% \begin{center}
% \begin{tabular}{rl}
% |ID|      &$\longrightarrow$ |char-num| / |xpunct| / |ID|\,|ID|\\
% |char-num|&$\longrightarrow$ |a..z| / |A..Z| / |0..9|\\
% |xpunct|  &$\longrightarrow$ |:| / |.| / |,|
% \end{tabular}
% \end{center}
% For example, `|ex:1|', `|Ex:2003|' are the |ID|s, but `|ex;1|' isnot.
%
% \DescribeMacro{#env}
% |#env| is any predefined environment.
% Parameter |(#env)| is \emph{optional}.
% 
% \DescribeMacro{#thm}
% |#thm|\, is the \emph{optional argument} for the environment |#env|.
% It is also the \emph{optional parameter} for the macro |\bxx|,
% because it is enclosed in the brackets;
% so you can specify a `|bxx|' like this
% \begin{center}
% \begin{tabular}{l}
%  |\bxx(#env)ID;|\\
%  \emph{something to typeset}\\
%  |\exx|\\
% \end{tabular}
% \end{center}
% or more simply (because |(#env)| is optional)
% \begin{center}
% \begin{tabular}{l}
%  |\bxx ID;|\\
%  \emph{something to typeset}\\
%  |\exx|\\
% \end{tabular}
% \end{center}
%
% \subsubsection{Meaning}
%
% \DescribeMacro{\bxx}
% \DescribeMacro{\exx}
% What does `|bxx|' mean?
%
% Yes, it's very familar with |\begin{#env}| and |\end{#env}|.
% \begin{center}
% \begin{tabular}{r@{\hspace{5mm}\emph{means}\hspace{5mm}}l}
%   |\bxx(#env)[#thm]ID;| & |\begin{#env}[#thm]|\\
%   \emph{something to typeset} & \emph{itself}\\
%   |\exx|                & |\end{#env}|
% \end{tabular}
% \end{center}
%
% \subsubsection{Identifying the `bxx'}
% \label{ssec:bxx.identify}
%
% We use |ID| and |#env| to identify a `|bxx|'
% by associating that `|bxx|' with the string |#envID|.
% This string is named \emph{string-id} of the `|bxx|'.
% Then two `|bxx|'s are different
% if their \emph{string-id}s are different.
% For e.g., after the declaration
% \begin{center}
% \begin{verbatim}
% \bxx(exercise)100;
% ...
% \exx
% \end{verbatim} 
% \end{center}
% the \emph{string-id} of the `|bxx|' is `|exercise100|'.
%
% \subsection{Data file. Example}
%
% An input file containing one or more `|bxx|'
% is called a \emph{data fille}.
% Using the `|bxx|', we reedit the `foo.tex' in the first section.
% Then, `|foo.tex|' becomes a \emph{data file}.
% \begin{verbatim}
% % --- first line of the new `foo.tex'
% \bxx(exercice)ex:1;
% \label{ex:1}
%       This is the first exercice.
% \exx
% \bxx(exercice)ex:2; \label{ex:2}
%       This is the second exercice.
% \exx
% ...
% \bxx(exercice)ex:16;\label{ex:16}
%       This is the 16th exercice.
% \exx
% % --- last line of the new `foo.tex'
% \end{verbatim}
%
% \section{User's macros}
%
% \subsection{Turning on/off the detail(s)}
%
% \DescribeMacro{\xdetailon}
% \DescribeMacro{\xdetailoff}
% Syntax
% \begin{center}
% |\xdetailon| \\ |\xdetailoff|
% \end{center}
%
% By default, the package shows \emph{string-id} of the `|bxx|'
% you want to get on the margin. You can turn this feature
% on/off by these marcos. The macros can be put anywhere in your
% document. See the package's test for an illustration.
%
% You can also pass an option to the package, like this
% \begin{center}
% |\usepackage[detailon]{ktv-texdata}|, or\\
% |\usepackage[detailoff]{ktv-texdata}|\phantom{xx}
% \end{center}
%
% \subsection{Specifying the default environment} 
%
% \DescribeMacro{\xenv}
% Syntax
% \begin{center}
% |\xenv(#env)|
% \end{center}
% This macro specifies the default environment used for the macros
% `|bxx|', `|\xget|', `|\xgetall|', etc.
% Here |#env| is any predefined environment.
%
% This macro can be put anywhere.
% It keeps the effect until the next |\xenv|.
%
% \subsection{Getting/Ignoring the `bxx'}
% 
% There are 7 macros used to get/remove the `bxx' from the data file.
% \begin{center}
% \begin{tabular}{l@{ \ }l@{ \ }l}
% |\xget|   & |\xgetall|    & |\xgetallbut|\\
% |\xkill|  & |\xkillall|   & |\xkillallbut|\\
%           & |\xspec|      &
% \end{tabular}
% \end{center}
% The macro |\xspec| is described in subsection \ref{ssec:xspec}.
% 
% \DescribeMacro{\xgetall}
% \DescribeMacro{\xkillall}
% Syntax
% \begin{center}
% |\xgetall|\\ |\xkillall|
% \end{center}
% |\xgetall| means that
% you want to \emph{get all the data items from the data file},
% while |\xkillall| means that you want to \emph{ignore all...}.
% These macros remove the effects of any |\xget|, |\xkill|
% that is called before them.
%
% \DescribeMacro{\xget}
% Syntax
% \begin{center}
% |\xget(#env){#ID-list}|
% \end{center}
% where
% \begin{itemize}
% \item
%   |#env| is an environment name. The parameter |(#env)| is optional.
% \item
%   |#ID-list| is a list of |ID|s that're seperated by the comma (`|,|'),
%   each |ID| can be prefixed by a plus sign (`|+|')
%   or by a minus sign (`|-|').
% \end{itemize}
% For e.g., the specification
% \begin{center}
% |\xget(exercise){-12,-3,+5,6,-99,+100}|
% \end{center}
% means that you want to get from the data file all the `|bxx|'s
% whose \emph{string-id}s are
% \begin{center}
% |exercise5|, |exercise6|, |exercise100|
% \end{center}
% Because you specified |-12|, |-3|, |-99|,
% all the `|bxx|' whose \emph{string-id}s are
% |exercise12|, |exercise3|, |exercise99| are ignored.
% Of course, |exercise7| is ignored, too.
%
% \trick:
% The parameter |(#env)| is optional; so if you specified
% \begin{center}
% |\xenv(exercise)|
% \end{center}
% then you can write shortly
% \begin{center}
% |\xget{-12,-3,+5,6,-99,+100}|
% \end{center}
%
% \trick:
% A sequence of two |\xget|s can be replaced by one |\xget|, for e.g.,
% \begin{center}
% |\xget{1,6}| |\xget{3,-4}|
% $\longrightarrow$ |\xget{1,6,3,-4}|
% \end{center}
%
% \DescribeMacro{\xkill}
% The syntax and tricks are the same as |\xget|.
% Try to guess the usage of this macro.
%
% \DescribeMacro{\xgetallbut}
% \DescribeMacro{\xkillallbut}
% The syntax is the same as |\xget|.
%
% The macro |\xgetallbut| means that you want to
% \emph{get all the data items} \underline{but}
% \emph{the data items specified in the} |#ID-list|.
%
% Inversely, the macro |\xkillallbut| means that you want to
% \emph{ignore all the data items} \underline{but}
% \emph{the data items specified in the} |#ID-list|.
%
% \trick:
% These macros are affected by |\xget| and |\xkill|.
% For e.g., the sequence
% \begin{center}
% |\xget{1,2,3}| |\xgetallbut{3,4,7}|
% \end{center}
% will be understood as |\xgetallbut{1,2,3,4,7}|.
%
% Here're some other examples (may be useless for you)
% \begin{center}
% \begin{tabular}{r@{  }l@{ $\longrightarrow$ }l}
% |\xget{1,2}|  &|\xgetallbut{2,3,4}|  &|\xgetallbut{1,2,3,4}|\\
% |\xkill{1,2}| &|\xgetallbut{2,3,4}|  &|\xgetallbut{2,3,4}|\\
% |\xget{1,2}|  &|\xkillallbut{2,3,4}| &|\xkillallbut{1,2,3,4}|\\
% |\xkill{1,2}| &|\xkillallbut{2,3,4}| &|\xkillallbut{2,3,4}|\\[2mm]
% |\xgetallbut{2,3,4}| &|\xget{1,2}|   &|\xgetallbut{1,2,3,4}|\\
% |\xgetallbut{2,3,4}| &|\xkill{1,2}|  &|\xgetallbut{3,4}|\\
% |\xkillallbut{2,3,4}|&|\xget{1,2}|   &|\xkillallbut{1,2,3,4}|\\
% |\xkillallbut{2,3,4}|&|\xkill{1,2}|  &|\xkillallbut{3,4}|\\
% \end{tabular}
% \end{center}
%
% \subsection{Openning the data file}
%
% \DescribeMacro{\xopenlib}
% Syntax
% \begin{center}
% |\xopenlib foo;|
% \end{center}
% where |foo|\footnote{The extension
% `|.tex|' may be omitted, so \back:|xopenlib foo;|
% is the same as \back:|xopenlib foo.tex;|.}
% is a data file.
% The semicolon `|;|' is mandatory.
%
% The  commands |\xget|, |\xkill|, etc.,
% in the previous section play no rule with the data file.
% They just tell |\xopenlib| that the `|bxx|' should be got/ignored.
% It's |\xopenlib| that opens the data file, looks for the `|bxx|'
% and does many other things. So if you want to |\xget{1,3,2}|,
% the really code is
% \begin{center}
% |\xget{1,3,2}| |\xopenlib foo;|
% \end{center}
% See subsection \ref{ssec:xlib} for a trick.
% 
% \subsection{Specifying the default data file}
% \label{ssec:xlib}
%
% \DescribeMacro{\xlib}
% It's convenient to specify a default data file. Let's do it by
% \begin{center}
% |\xlib foo;|
% \end{center}
% The file extension `|.tex|' can be omitted.
%
% \trick:
% After specifying the default data file, you can open that file by
% \begin{center}
% |\xopenlib;|
% \end{center}
%
% \subsection{Getting orderly the data items}
% \label{ssec:xspec}
%
% Do you have any questions about the order of the `bxx's?
%
% Yes, if you had, the answer is:
% the calling of |\xget|, |\xgetall|, |\xkill|, etc.,
% will produce the output in which the order of the data items
% is same as the order of data items in the data file.
% More concretely, if in `|foo.tex|' you specify
% \begin{verbatim}
% \bxx 15;
%       ITEMA.
% \exx
% \bxx 14;
%       ITEMB.
% \exx
% \end{verbatim}
% then |\xget{14,15}| |\xopenlib foo;|\\
% or\phantom{en} |\xget{15,14}| |\xopenlib foo;|\\
% gives the same results:
% `|ITEMA|' is put before `|ITEMB|'.
%
% So we need the macro described below.
%
% \DescribeMacro{\xspec}
% Syntax (same as |\xget|)
% \begin{center}
% |\xspec(#env){#ID-list}|
% \end{center}
% |(#env)| is also an optional parameter.
%
% This macro opens the data file (so you needn't to
% specify a |\xopenlib...| after it), extract the data items
% from the data file within the order in the |#ID-list|.
%
% So if you want to put `|ITEMB|' before the `|ITEMA|'
% (see above example), you should call
% \begin{center}
% |\xspec{14,15}|
% \end{center}
%
% \section{Advanced features}
%
% \subsection{Enabling/Disabling section in data file}
%
% A data file may be divided into sections.
% But when looking for data file, this package
% will be typeset normally anything that isnot inside any a `|bxx|'
% (for e.g, a |\section| command),
% so you can see some strange outputs.
%
% \DescribeMacro{\xenablesection}
% \DescribeMacro{\xdisablesection}
% Syntax
% \begin{center}
% |\xdisablesection|\\|\xenablesection|
% \end{center}
% The first macro disables three commands
% \begin{center}
% |\section| |\subsection| |\subsubsection|
% \end{center}
% The second enables three above section commands
% by restoring them to the values that this package
% captured at the beginning of the document.
% (So if you redefine a section command inside
% |\begin{document}| and |\end{document}|,
% you may lost that new command after using the sequence
% |\xdisablesection| |\xenablesection|.
%
% {\bf NOTE:}
%
% |\xdisablesection| cannot disable the command
% \begin{center}
% |\section[optional]{...}|
% \end{center}
%
% \subsection{Data item followed by `hint' environment}
%
% \DescribeEnv{hint}
% If a data item, or a `|bxx|', in data file is an exercise,
% it is often followed by a hint (a solution).
% All the hints should be collected in a private place.
% The `|hint|' environment helps you in this behaviour.
% You put `|hint|' just after the declaration of a data item,
% like this
% \begin{verbatim}
% \bxx 100;
%       This is Exercise A
% \exx
% \begin{hint}
%       This is the hint of excercise A.
% \end{hint}
% \end{verbatim}
% When you get the above `|bxx|', for e.g., by |\xget{100}|,
% the content of the hint, here's \emph{\lq\lq This is the hint...\rq\rq}
% will be written automatically to a file called `|hintfile|'.
%
% \DescribeMacro{\xopenhint}
% If you want to open the hint file, just call
% \begin{center}
% |\xopenhint|
% \end{center}
%
% {\bf IF YOU WANT TO KNOW MORE...}
%
% The `|hintfile|' is really a data file, whose name is
% \begin{center}
% |\jobname.KTVhint|
% \end{center}
% |\jobname| is the name of current job,
% and `|.KTVhint|' is the extension\footnote{If your system
% doesnot support the long extension file name,
% please report to the author.} provided by the author.
%
% By default, all the contents
% of the `|hintfile|' will be loaded (|\xgetall|).
%
% It's too complex to explain the structure of the `|hintfile|'.
% Let's typset the test, look for the `|hintfile|'
% and the results for more details. Thank you!
%
% \section{Important notes (to the users)}
%
% \begin{itemize}
% \item[a)]
%   (bug)
%   You must put |\xenv(#env)| before any calling |\xget|, |\xkill|, etc.
%   Try this bug to know how the package works.
% \item[b)]
%   Donot put |\xdisablesection| just before |\tableofcontents|.
%   (Otherwise, an error will be reported.)
% \item[c)]
%   The environment(s) of the data items in the data file must be predefined.
% \item[d)]
%   |\xopenhint| should be called at the very end of the document,
%   and must be called after any |\xspec|, |\xopenlib| commands.
%   (|\xopenhint| will closed the `|hintfile|';
%   after that the writting operations have no effect on this file.)
% \item[e)]
%   (bug)
%   Labelling the `|bxx|' maynot work well.
%   If you try to load twice a `|bxx|',
%   then two loadings will have a same label.
%   In next version, we will redefine the |\label| and |\ref| to fix this bug.
% \end{itemize}  
%
%
% \section{Generating package and example}
%
% Excuting
% \begin{center}
% {{\bf latex} \ttfamily ktv-texdata.ins}
% \end{center}
% to generate the package ({\tt ktv-texdata.sty})
% and a test
% ({\it two files:} {\tt ktv-test.tex}; {\tt ktv-data.tex}).
% Then typeset file {\ttfamily ktv-test.tex}.
%
% \section{Implementation}
%
% \subsection{Notes}
%
% \emph{The implementation} is the document
% {\bf used only} by the package's hacker!!!
%
% This package is written by |top-down| technique:
% A macro |\fooA| sometimes calls the macro |\fooB|
% whose the definitions appears after the defintions of |\fooA|.
%
% This package uses some advanced and interesting macro techniques
% (\emph{unknown-number-of-parameters-macro},
% \emph{two-optional-parameters-macro}, and much more).
% If you're a newbie of \LaTeX\ programming,
% you will learn some new things by learning the code of package.
%
% \subsection{Requirements. Options}
%
%    \begin{macrocode}
%<*package>
\RequirePackage{verbatim}
%    \end{macrocode}
%
% \begin{macro}{\@@xdetail}
% This macro shows \emph{string-id} of the current `|bxx|'
% on the margin.
%    \begin{macrocode}
\def\@@xdetail{\marginpar{{\bf\@xenv}\@xlbl}}
%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{detailon}
% |detailon| is the default option.
% Anything else means |detailoff|.
%    \begin{macrocode}
\DeclareOption{detailon}{\let\@xdetail\@@xdetail}
\DeclareOption*{\let\@xdetail\relax}
\ExecuteOptions{detailon}
\ProcessOptions
%    \end{macrocode}
% \end{environment}
%
% \subsection{General purpose macros}
%
% \begin{macro}{\b@sy}
% |\b@sy| means `not |\relax|'.
%    \begin{macrocode}
\def\b@sy{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\if@xNIL}
% Check if string |#1| is empty.
%
%    \begin{macrocode}
\def\if@xNIL#1;{\ifx\relax#1\relax}
%    \end{macrocode}
% \end{macro} 
%
% \subsection{Scanning and setting flag for \emph{string-id}}
% \label{ssec:scanning}
%
% First, consider the example when the user calls
% \begin{center}
% |\xget(exercise){ex:1,-ex:3,ex:5,ex:7,ex:9,ex:11,ex:13,ex:15}|
% \end{center}
% Here he (the user) wants to get the `|bxx|'
% that is numbered oddly,
% and that uses the environment `|exercise|'
% from the data file
% (\underline{\bf but} he doesn't want to get |ex:3|).
%
% Because the number of exercises he wants to get is unknown
% (he just specifies a list of the items he needs),
% we must design a macro that scans for all the items in the list.
% Moreover, this macro will set the flag `|get|' or `|donotget|'
% for each item (in the above example, the flag for |ex:3|
% is `|donotget|', for the others is `|get|').
%
% \begin{macro}{\usr@xenv}
% \begin{macro}{\usr@xlbl}
% These macros are used to store the result of the scanning.
%    \begin{macrocode}
\def\usr@xenv{}
\def\usr@xlbl{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@multact}
% Scan and set the flag (|get|, |donotget|) for the items of a list.
% The syntax is
% \begin{center}
%       |\@multact(#usr-env){#ID-list}|
% \end{center}
% where |#ID-list| is a list of |ID|(s)\footnote{%
% see subsection \ref{ssec:bxx.form}.}
% that're seperated by the comma (`|,|').
% Moreover, each |ID| can be prefixed by a plus/minus sign (|+|, |-|).
% \begin{center}
% \begin{tabular}{rl}
%    |#ID-list| &$\longrightarrow$ |XID| / |#ID-list|, |XID| \\
%    |XID|      &$\longrightarrow$ |ID| / |+ID| / |-ID|
% \end{tabular}
% \end{center}
% If |ID| is prefixed by a |+| (resp., a |-|), it means
% that this |ID| has a `{\it positive}' (resp., `{\it negative}')
% meaning\footnote{%
% If the user wants to get something,
% the |+| means `{\it he wants}', but |-| means `{\it he doesn't want}'.
% If the user doesn't want to get someting,
% the |+| means `{\it no, he doesn't want}',
% while the |-| means `{\it yes, he wants}'.}
% in the current context.
% An |ID| without prefix is the same as |+ID|.
%
% The |#usr-env| is any environment (maynot be predefined).
% This |#usr-env| is concatenated with every |ID| in the |#ID-list|.
% The concatenation will create the \emph{string-id}(s)
% for which we will later set the flag.
%
% Here we use (|#usr-env|) instead of (|#env|)
% because the environment |#usr-env| is used at the time the user
% wants to \emph{get some thing} from the data file (for e.g.,
% by the command |\xget{ex:1,ex:2}|).
% This environment may be different from the environment we
% specify in the data file, it maynot even be predefined.
%
% Note that |(#usr-env)| is optional parameter of macro |\@multact|.
% In case that |(#usr-env)| is omitted,
% |#usr-env| will get the default value saved in |\@@xenv|.
%    \begin{macrocode}
\def\@multact{\futurelet\@tchar\chk@multact}
%    \end{macrocode}
% We first check if the next char is a `|(|'.
%    \begin{macrocode}
\def\chk@multact{%
    \ifx(\@tchar
        \let\@txen\opt@multact
    \else
        \let\@txen\nop@multact
    \fi
    \@txen}
%    \end{macrocode}
% If the next char is a `|(|'
%    \begin{macrocode}
\def\opt@multact(#1)#2{%
    \def\usr@xenv{#1}%
%    \end{macrocode}
% then read the |#ID-list| by calling |\@xmultarg|%
% (see subsection \ref{ssec:ID.extract} below)
%    \begin{macrocode}
    \@xmultarg{#2}}
%    \end{macrocode}
% If the next char is not a `|(|', |\usr@xenv| gets the default value
%    \begin{macrocode}
\def\nop@multact#1{%
    \def\usr@xenv{\@@xenv}
%    \end{macrocode}
% then read the |#ID-list|  by calling |\@xmultarg|
%    \begin{macrocode}
    \@xmultarg{#1}}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Extracting ID from the \#ID-list}
% \label{ssec:ID.extract}
%
% \begin{macro}{\@action}
% |\@action| is \emph{something} we want to affect
% each \emph{string-id}\footnote{\emph{string-id}%
% is created by concatenation |\#usr-env| and |ID|.
% See subsection \ref{ssec:bxx.identify}.
% } found.
% We first let |\@action| to |\relax|,
% so we avoid seeing the error `|! Undefined control...|'.
% (This letting existed in some previous versions of package.
% Now the author doesnot know what's going on if this line is removed!)
%    \begin{macrocode}
\let\@action\relax
%    \end{macrocode} 
% \end{macro}
% \begin{macro}{\@@MINUS}
% \begin{macro}{\@@MINUS}
% \begin{macro}{\@@ZERO}
% There are three kinds of actions
% (|plus| action, |minus| action, and |nosign| action).
% We'll see in subsection \ref{ssec:action.collection} that
% every action is named `|MINUS|\emph{something}',
% or `|ZERO|\emph{something}', or `|PLUS|\emph{something}'.
%    \begin{macrocode}
\def\@@MINUS{MINUS}
\def\@@PLUS{PLUS}
\def\@@ZERO{ZERO}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@xmultarg}
% |\@xmultarg| is used to extract the |ID|(s) from the |#ID-list|.
% Here we use the macro technique specified in \cite{VE}.
% First, we add to the argument |#1| the terminator\footnote{Because
% that `|@|' is never used to create an |ID|,
% the terminator `|@@@|' works well.} |\@endlbl| (`|@@@|').
% Then we call |\@@xmultarg|.
%    \begin{macrocode}
\def\@xmultarg#1{\@@xmultarg#1,@@@,}
\def\@endlbl{@@@}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@@xmultarg}
% Now is the extracting.
%    \begin{macrocode}
\def\@@xmultarg#1,{%
    \def\@xtempi{#1}
    \ifx\@endlbl\@xtempi
        % do nothing
    \else\@@@xmultarg#1,
        \expandafter\@@xmultarg
    \fi}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@@@xmultarg}
% Every |ID| extracted will be concatenated
% with the |#usr-env| to create the \emph{string-id}.
% The \emph{string-id} will be affected
% by one of three actions (|plus|, |minus| or |nosign|/|zero|).
% We first check the sign of the |ID| in the |#ID-list|.
%    \begin{macrocode}
\def\@@@xmultarg{\futurelet\@tchar\chk@@@xmultarg}
\def\chk@@@xmultarg{% 2003/05/14
    \ifx-\@tchar\relax
        \let\@txen\@@MINUS
    \else\ifx+\@tchar\relax
            \let\@txen\@@PLUS
        \else
            \let\@txen\@@ZERO
        \fi
    \fi
%    \end{macrocode}
% To affect |\@action| on the \emph{string-id},
% we must specify `|type|' of the action.
% The type is saved in |\@txen| (see previous codes).
%    \begin{macrocode}
\csname\@txen\@action\endcsname}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Comment creator macro}
%
% \begin{macro}{\c@mm@nt}
% If we donot want to get a `|bxx|' from the data file,
% we just let temporarily macro `|\bxx|'  to `|\c@mm@nt|'.
% This macro scans and passes (or {\it ignore})
% the input \emph{line by line}, until it finds the fisrt `|\exx|'.
% That's why every `|bxx|' must be ended by a `|\exx|' in a single line!
% And that's why `|bxx|' cannot be nested!
%    \begin{macrocode}
\gdef\c@mm@nt{%
    \begingroup
    \catcode`\^^M=12 %
    \x@comment}
{\catcode`\^^M=12 \endlinechar=-1 %
\gdef\x@comment#1^^M{%
    \def\@xtest{#1}%
    \ifx\@xtest\exx
        \let\@txen=\endgroup
    \else
        \let\@txen\x@comment
    \fi
    \@txen}}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Hint file. Hint creator environment}
%
% An exercise, for e.g., may be followed by a hint (or  a solution).
% In a test, a book of excercises,
% all hints should be collected in a single file
% (named `|hint file|') that is genererated automatically by the package.
% The |hint file| is also a data file; of course, it contains
% only the hint that follows the `|bxx|' we want to get from the data file
% (the \emph{active} `|bxx|'s).
%
% Because a hint sometimes will be disabled,
% we need a boolean test |\if@xhint|.
%    \begin{macrocode}
\newif\if@xhint
%    \end{macrocode}
% The name of hint file is `|\jobname.KTVhint|'.
%    \begin{macrocode}
\newwrite\@xfhint
\immediate\openout\@xfhint=\jobname.KTVhint
%    \end{macrocode}
% We put some information in the first two lines of hint file.
%    \begin{macrocode}
{\catcode`\%=12
\immediate\write\@xfhint{%% File created automatically by `ktv-texdata'.}
\immediate\write\@xfhint{%% DONOT EDIT THIS FILE MANUALLY.}}
%    \end{macrocode}
%
% \begin{environment}{hint}
% Here is a trick about `hint' environment.
% We collect hints in a same file.
% So we need something to know exactly what `|bxx|'
% a `hint' (in the hint file) associates with.
% \emph{So.... we assume} that `|#env|'
% specified in `|bxx|' (in the data file) is a numberable environment.
% Then when a `hint' follows a `|bxx|' (named |bxxA|) is written to hint file,
% its number is the same as the number of `|bxx|A' in the output.
% So we need a counter\footnote{Please try to figure out this counter.}
% to index the active `|bxx|'.
%    \begin{macrocode}
\newcount\c@bxx
%    \end{macrocode}
%    \begin{macrocode}
\newenvironment{hint}{% begin-part of `hint'
%    \end{macrocode}
% If the hint is enabled
%    \begin{macrocode}
\if@xhint
    \let\exx\relax
%    \end{macrocode}
% In the numbered environment `|#env|',
% \LaTeX\ automatically creates a counter\footnote{%
% We should not check for the existing of this counter:
% the checking may eat so much time!}%
% named `|c@#env|'.
% We let `|c@bxx|' to the current value of `|c@#env|',
% then subtract `|c@bxx|' by 1 (\LaTeX\ will increase `|c@bxx|' later).
% We create an data item in the hint file,
% whose \emph{string-id} is the same as the current `|bxx|'.
%    \begin{macrocode}
    \c@bxx=\csname c@\@xenv\endcsname%
    \advance\c@bxx by-1 %
    \immediate\write\@xfhint{\string\setcounter{\@xenv}{\the\c@bxx}}
    \immediate\write\@xfhint{\string\bxx(\@xenv)\@xlbl;}
%    \end{macrocode}
% The contents of the hint environment now will be written to the hint file.
%    \begin{macrocode}
    \let\do\@makeother\dospecials\catcode`\^^M\active%
    \def\verbatim@processline{%
        \immediate\write\@xfhint{\the\verbatim@line}}%
    \expandafter\verbatim@start
%    \end{macrocode}
% If the hint is disable, let it start a comment environment.
% Here we use `|\comment|' defined in |verbatim| package,
% not the `|\c@mm@nt|' defined in this package\protect\footnote{%
% \emph{A fun story:} in the first version of package, the author
% used `\back:|comment|' by a mistake, but package ran very well.
% He later found this error, and changed `\back:|comment|'
% to `\back:|c@mm@nt|', then the package dumped into the errors!}.
%    \begin{macrocode}
\else
    \def\exx{\exx}%
    \expandafter\comment
\fi}%
%    \end{macrocode}
% Now the end-part of hint environment. If the hint is enabled,
% we put an `|\exx|' to terminate `|\bxx|' in the hint file.
% The line |\noexpand\exx| was added here but the author forgot the reason.
% Please help him!
%    \begin{macrocode}
{%
\noexpand\exx
\if@xhint
    \immediate\write\@xfhint{\string\exx}
\fi}
%    \end{macrocode}
% \end{environment}
%
% \subsection{Replacements of macro \back:bxx}
%
% Macro |\bxx| will be replaced by one of these macros
% |\@bxx|, |\@bxy|, |\@ball|, |\@bnone|, |\@bnonebut|, |\@ballbut|.
% The replacement depends on what the macro |\xgetfoo|, |\xkillfoo|, ...
% are specified by the user.
%
% For e.g., if the user calls |\xgetall|,
% then |\bxx| is replaced by |\@ball|;
% if they call |\xkillall|, then |\@bnone| takes place.
%
% For more details about the replacement,
% see the definition of |\xget|, |\xgetall|, |\xkill|, |xkillall|, etc.
% in the subsection \ref{ssec:user.macros}.
%
% Here's the summary of the replacements.
% \begin{center}
% \begin{tabular}{rl}
% {Calling } & {The replacement of |\bxx|}
% \\[3mm]\hline\\
% |\xget|        & |\@bxx|\\
% |\xgetall|     & |\@ball|\\
% |\xgetallbut|  & |\@ballbut|\\[3mm]
% |\xkill|       & |\@bxx|\\
% |\xkillall|    & |\@bnone|\\
% |\xkillallbut| & |\@bnonebut|\\[3mm]
% |\xspec|       & |\@bxy| 
% \\\\\hline
% \end{tabular}
% \end{center}
%
% The common syntax for the macros |\@bfoo| is
% \begin{center}
%     |\@bfoo(#env)[#thm]{#ID}|;
% \end{center}
% Here |#thm| is the optional argument of the environment |#env|.
% Both `|[#thm]|' and `|(#env)|' are the optional parameters
% of the |\@bfoo| (this means you can omit `|[#thm]|' or `|(#env)|'
% or both of them while callling `|\@bfoo|').
% \begin{macro}{\@bxx, \@bxy}
% Each macro |\@bxx|, |\@bxy| does two things.
%
% \emph{Firstly,} it gets the information about the current `|bxx|'
% (information contains: |#env|, |#thm| and |#ID|).
% We use the |\@bxxarg| macro .
%
% \emph{Secondly,}
% it calls |\@bfoodone| to typeset the content of the `|bxx|'.
%    \begin{macrocode}
\def\@bxx#1;{%
    \@bxxarg#1;%
    \@bxxdone}
\def\@bxy#1;{%
    \@bxxarg#1;%
    \@bxydone}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@ball}
% Macro |\@ball| first gets the information about the `|bxx|'
% (like |\@bxx|), but then it calls `|\@bdone@kern|',
% the kernel of the |\@bdone|.
%    \begin{macrocode}
\def\@ball#1;{%
    \@bxxarg#1;%
    \@bdone@kern}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@bnone}
% Macro |\@bnone| starts the comment by calling |\c@mm@nt|.
% This macro also disable the hint environment that follows the `|bxx|'.
%    \begin{macrocode}
\def\@bnone#1;{%
  \@xhintfalse
  \def\exx{\exx}
  \expandafter\c@mm@nt}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@bnonebut}
% Macro |\@bnonebut| ignores the contents of the `|bxx|'
% if and only if the command `|\csname\@xenv\@xlbl\endcsname|'
% equals to `|\relax|'.
%
% First, the macro scans for information.
%    \begin{macrocode}
\def\@bnonebut#1;{%
    \@bxxarg#1;%
%    \end{macrocode}
% Then it checks the status of the command
% `|\csname\@xenv\@xlbl\endcsname|'.
% If this command is undefined, we turn off the next `hint'
% and start the comment command (to ignore the content of the `|bxx|').
%    \begin{macrocode}
    \expandafter\ifx\csname\@xenv\@xlbl\endcsname\relax
        \@xhintfalse
        \def\exx{\exx}
    \expandafter\c@mm@nt
%    \end{macrocode}
% If this command is defined, the contents of the
% `|bxx|' will be typeset. Before calling `|\@bdone@kern|',
% we let the status of the command `|...\@xenv\@xlbl...|'
% to `|\relax|'. By doing this, we ensure that two `|bxx|'s in the
% data file that have the same \emph{string-id} will be typeset only once.
%    \begin{macrocode}
    \else
        \expandafter\let\csname\@xenv\@xlbl\endcsname\relax
        \expandafter\@bdone@kern
    \fi}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@ballbut}
% Macro |\@ballbut| is inverse from the macro `|\@bnonebut|'.
%    \begin{macrocode}
\def\@ballbut#1;{%
    \@bxxarg#1;
    \expandafter\ifx\csname\@xenv\@xlbl\endcsname\relax
        \expandafter\@bdone@kern
    \else
        \expandafter\let\csname\@xenv\@xlbl\endcsname\relax
        \@xhintfalse
        \def\exx{\exx}
        \expandafter\c@mm@nt
    \fi}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Getting information from `bxx'}
%
% \begin{macro}{\@bxxarg}
% This macro reads the information about the `|bxx|'.
% The information contains |#env| (saved in `|\@xenv|'),
% |#thm| (saved in `|\xhead|'), |#ID| (saved in `|\xlbl|').
%
% We first set the initial value for three macros:
%    \begin{macrocode}
\def\@xenv{}
\def\@xhed{}
\def\@xlbl{}
%    \end{macrocode}
% We define two macros that save the default values
% for the |#env| and |#thm|. The default value of |#thm|
% should be null (why?). Now the default |#thm|
% is aslo null, but in the subsection \ref{ssec:user.macros},
% we define macro |\xenv| to change this behaviour.
%    \begin{macrocode}
\def\@@xenv{}
\def\@@xhed{}
%    \end{macrocode}
% Now we define |\@bxxarg|.
%    \begin{macrocode}
\def\@bxxarg{\futurelet\@tchar\chk@bxxarg}
%    \end{macrocode}
% We check if the next char is a `|(|'.
%    \begin{macrocode}
\def\chk@bxxarg{%
    \ifx(\@tchar
        \let\@txen\env@bxxarg
    \else
        \let\@txen\nop@bxxarg
    \fi
    \@txen}
%    \end{macrocode}
% If the next char is a `|(|', we hope that
% the `|bxx|' specifies the `|(#env)|'.
% Here the author uses an `|\edef|', but he really
% doesnot know the reasons.
% The `|\def|' version sometimes causes a strange error.
%    \begin{macrocode}
\def\env@bxxarg(#1)#2;{%
    \edef\@xenv{#1}%
%    \end{macrocode}
% Now we look for the rest of the argument by calling |\@@bxxarg|.
%    \begin{macrocode}
  \@@bxxarg#2;}
%    \end{macrocode}
% If the next char isnot a `|(|',
% `|bxx|' uses the default environment.
%    \begin{macrocode}
\def\nop@bxxarg#1;{%
    \edef\@xenv{\@@xenv}%
%    \end{macrocode}
% Now we look for the rest of the argument (call |\@@bxxarg|).
%    \begin{macrocode}
  \@@bxxarg#1;}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@@bxxarg}
% The calling of |\@@bxxarg| will first check if
% the next char is a `|[|'.
%    \begin{macrocode}
\def\@@bxxarg{\futurelet\@tchar\chk@@bxxarg}
%    \end{macrocode}
%    \begin{macrocode}
\def\chk@@bxxarg{%
    \ifx[\@tchar
        \let\@txen\hed@@bxxarg
    \else
        \let\@txen\nop@@bxxarg
    \fi
    \@txen}
%    \end{macrocode}
% If the next char is a `|[|',
% we hope that `|bxx|' specifies the `|[#thm]|'
%    \begin{macrocode}
\def\hed@@bxxarg[#1]#2;{%
    \edef\@xhed{#1}%
    \edef\@xlbl{#2}}
%    \end{macrocode}
% If the next char isnot a `|[|', the `|#thm|' gets the default value.
%    \begin{macrocode}
\def\nop@@bxxarg#1;{%
    \edef\@xhed{\@@xhed}%
    \edef\@xlbl{#1}}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Typesetting the content of `bxx'}
%
% \begin{macro}{\@bxxdone}
% This macro starts a |\c@mm@nt| (if the command
% `|...\@xenv\@xlbl...|' is `|\relax|'),
% or calls |\@bdone@kern| (the kernel of the |\@bxxdone|).
%    \begin{macrocode}
\def\@bxxdone{%
    \expandafter\ifx\csname\@xenv\@xlbl\endcsname\relax
        \@xhintfalse
        \def\exx{\exx}
        \expandafter\c@mm@nt
    \else
        \expandafter\let\csname\@xenv\@xlbl\endcsname\relax
        \expandafter\@bdone@kern
   \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@bdone@kern}
% This macro typesets the contents of the `|bxx|'
% by starting the environment `|#env|'. It also turns on the
% next hint environment.
%
% If the `|bxx|' specifies the `|#thm|',
% we should call `|\begin{#env}[#thm]|';
% otherwise, we just call `|\begin{#env}|'.
% (Then the `|\exx|' equals to `|\end{#env}|'.)
%
% \emph{Note that} `|\begin{#env}[]|' is different from `|\begin{#env}|.
% So we must check if the `|#thm|' is empty.
%    \begin{macrocode}
\def\@bdone@kern{%
    \@xhinttrue%
    \def\exx{\end{\@xenv}}
    \expandafter\if@xNIL\@xhed;
        \def\@txen{%
            \begin{\@xenv}\@xdetail}
    \else
        \def\@txen{%
            \begin{\@xenv}[\@xhed]\@xdetail}
    \fi
    \@txen}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@bxydone}
% This macro just passes the arguments to |\@bxy@kern|.
%    \begin{macrocode}
\def\@bxydone{\@bxy@kern(\@xenv){\@xlbl}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@bxy@kern}
% This macro is the kernel of |\@bxydone|.
% It opens the data file, searchs for the `|bxx|'
% whose \emph{string-id} is `|\@bxy@id|'.
% If such `|bxx|' is found, the macro stops the seaching\footnote{%
% We really donot stop the searching. We just ignore the other
% `|bxx|'(s) in the data file.
% \emph{Trick:} Each time this macro opens the data file,
% it gets at most one data item,
% while, for e.g., \back:|\@bdone@kern| may get more than one.}
% and calls `|\@bdone@kern|' to typeset the contents of that `|bxx|'.
% Any `|bxx|' in the data file whose \emph{string-id} doesnot match
% the string saved in |\@bxy@id| will be ignored.
%
% Because a \emph{string-id} never accepts the value `@',
% we just let |\@bxy@id| to `@' to stop the searching.
%    \begin{macrocode}
\def\@bxy@id{@}
\def\@bxy@kern(#1)#2{%
    \edef\@xtempi{#1#2}
    \ifx\@bxy@id\@xtempi
        \def\@bxy@id{@}
        \expandafter\@bdone@kern
    \else
        \@xhintfalse
        \def\exx{\exx}
        \expandafter\c@mm@nt
    \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@xspec@i}
% If you read the above codes carefully,
% you donot see anything about the `\emph{openning the data file}'.
% Yes, the truth is that |\@bxy|, |\@bxydone| and aslo |\@bxy@kern|
% are always used within the |\@xspec@i|.
% This macro does the openning.
%    \begin{macrocode}
\def\@xspec@i(#1)#2{
    \let\bxx\@bxy
    \edef\@bxy@id{#1#2}
%    \end{macrocode}
% Now start the searching (openning)
%    \begin{macrocode}
    \input \@xlib}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Actions affect on the \emph{string-id}}
% \label{ssec:action.collection}
%
% Each action |foo| must be associated with 3 routines
% \begin{center}
%    |PLUSfoo+#1|,\,\, |MINUSfoo-#1|,\,\, |ZEROfoo#1|,
% \end{center}
%
% \begin{macro}{@@@setflag}
% The positive meanings of this action is to define the command
% \begin{center}
%    |\csname\usr@xenv#1\endcsname| 
% \end{center}
% by letting this command to `|\b@sy|'.
%    \begin{macrocode}
\def\PLUS@@@setflag+#1,{%
    \expandafter\let\csname\usr@xenv#1\endcsname\b@sy}
\def\MINUS@@@setflag-#1,{%
    \expandafter\let\csname\usr@xenv#1\endcsname\relax}
\def\ZERO@@@setflag#1,{%
    \expandafter\let\csname\usr@xenv#1\endcsname\b@sy}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{@@@killflag}
% The positive meanings of this action is to undefine the command
% \begin{center}
%    |\csname\usr@xenv#1\endcsname| 
% \end{center}
% by letting this command to `|\relax|'.
%    \begin{macrocode}
\def\PLUS@@@killflag+#1,{%
    \expandafter\let\csname\usr@xenv#1\endcsname\relax}
\def\MINUS@@@killflag-#1,{%
    \expandafter\let\csname\usr@xenv#1\endcsname\b@sy}
\def\ZERO@@@killflag#1,{%
    \expandafter\let\csname\usr@xenv#1\endcsname\relax}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{@@@xspec}
% This action associates with the |\xspec| (see subsection
% \ref{ssec:user.macros} for details).
% This action, unlike |@@@killflag| nor |@@@setflag|,
% doesnot define/undefine the command `|...\usr@xenv#1...|'.
% The positive meaning of this action is
% \emph{openning data file
% and getting the specified {\normalfont `|bxx|'}}.
%    \begin{macrocode}
\def\PLUS@@@xspec+#1,{%
    \@xspec@i(\usr@xenv){#1}}
\def\MINUS@@@xspec-#1,{}
\def\ZERO@@@xspec#1,{%
    \@xspec@i(\usr@xenv){#1}}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Openning the data file}
% \label{ssec:data.file.open}
%
% \begin{macro}{\@xlib}
% \begin{macro}{\@@xlib}
% |\@xlib| stores the data file specified by users.\\
% |\@@xlib| stores the data file used by the |\@openlib|.\\
% Currently, two macros save the null values.
%    \begin{macrocode}
\def\@xlib{}
\def\@@xlib{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@openlib}
% Open the data file. The syntax of this macro is |\@openlib#1;|,
% where |#1| is the file name. If |#1| is omitted, the |\@openlib|
% uses the data file whose name is stored in |\@xlib|. Note that
% in this case, the calling of macro is |\@openlib;|.
%    \begin{macrocode}
\def\@openlib#1;{%
    \if@xNIL#1;
        \expandafter\if@xNIL\@xlib;
            % do nothing
        \else
            \def\@@xlib{\@xlib}
        \fi
    \else
        \def\@@xlib{#1}
    \fi
    \input \@@xlib}
%    \end{macrocode}
% \end{macro}
%
% \subsection{User's macros}
% \label{ssec:user.macros}
%
% \begin{macro}{\xspec}
% This macro provides a special ways to read the data file.
% After its scanning the data file, the order of the `|bxx|'s in the
% ouput is the same the order of the |ID|s specified in the |#ID-list|.
%    \begin{macrocode}
\def\xspec{%
    \def\@action{@@@xspec}%
    \@multact}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\xlib}
% \begin{macro}{\xopenlib}
% |\xlib| specifies the data file.
% |\xopenlib| opens the data file.
%    \begin{macrocode}
\def\xlib#1;{%
    \edef\@xlib{#1}}
\let\xopenlib\@openlib
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\xkill}
% \begin{macro}{\xkillall}
% \begin{macro}{\xkillallbut}
% Macro's names speak that...
%    \begin{macrocode}
\def\xkill{%
    \let\bxx\@bxx
    \def\@action{@@@killflag}%
    \@multact}
\def\xkillall{%
    \let\bxx\@bnone}
\def\xkillallbut{%
    \let\bxx\@bnonebut
    \def\@action{@@@setflag}%
    \@multact}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\xget}
% \begin{macro}{\xgetall}
% \begin{macro}{\xgetallbut}
% Macro's names speak that...
%    \begin{macrocode}
\def\xget{%
    \let\bxx\@bxx
    \def\@action{@@@setflag}%
    \@multact}
\def\xgetall{%
    \let\bxx\@ball}
\def\xgetallbut{%
    \let\bxx\@ballbut
    \def\@action{@@@setflag}%
    \@multact}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\xhintready}
% The hint file must be closed (`ready') before being opened.
%    \begin{macrocode}
{\catcode`\%=12
\gdef\xhintready{
    \begingroup\catcode`\%=12
    \immediate\write\@xfhint{\string\endinput}
    \immediate\write\@xfhint{%% END OF FILE %%}
    \endgroup\immediate\closeout\@xfhint}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\xopenhint}
% Open the hint file.
%    \begin{macrocode}
\def\xopenhint{%
    \xhintready
    \xgetall
    \xlib \jobname.KTVhint;
    \xopenlib;}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\xenv}
% Specify the default environment.
%    \begin{macrocode}
\def\xenv(#1){%
    \edef\@@xenv{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\xdetailon}
% \begin{macro}{\xdetailoff}
% Turn the details on/off.
%    \begin{macrocode}
\def\xdetailon{%
    \let\@xdetail\@@xdetail}
\def\xdetailoff{%
    \let\@xdetail\relax}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\xenablesection}
% \begin{macro}{\xdisablesection}
% If we put some `|\section|' commands in the data file,
% we may want to disable/enable them.
%
% First, we define a null section.
%    \begin{macrocode}
\def\nil@section#1{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% Then we save the old values of section-relatives
% just before |\begin{document}|.
% We mention only |\section|, |\subsection|, |\subsubsection|.
%    \begin{macrocode}
\AtBeginDocument{%
    \let\old@section\section
    \let\old@subsection\subsection
    \let\old@subsubsection\subsubsection}
%    \end{macrocode}
% Everytime you want to disable the section,
% we let |\section| to |\nil@section|.
%    \begin{macrocode}
\def\xdisablesection{%
    \let\section\nil@section%
    \let\subsection\nil@section%
    \let\subsubsection\nil@section}
%    \end{macrocode}
% To restore, let |\section| to his old value
% (that we captured at the beginning of document).
% If you redefine |\section|  inside |\begin{document}|
% and |\end{document}|, you cannot get that new |\section|
% after using any |\xdisablesection|.
%    \begin{macrocode}
\def\xenablesection{%
    \let\section\old@section%
    \let\subsection\old@subsection%
    \let\subsubsection\old@subsubsection}
%    \end{macrocode}
%
% \subsection{Initialization}
%
% See section \ref{sec:history} for a reason.
%    \begin{macrocode}
\let\bxx\@bxx
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \section{History}
% \label{sec:history}
%
% \begin{tabular}{ccl}
%   {\bf v01}.xx &2002/12/xx &the first design\\[2mm]
%   {\bf v02}.xx &2002/12/yy &\emph{changes forgotten}\\[2mm]
%   {\bf v03}.14 &2002/12/18 &the first good version\\
%   v03.15 &2003/03/26 &some litle changes\\
%   v03.17 &2003/04/19 &use two-step macro technique\\
%          &           &|\bxx|,|\xlib|,|\xopenlib|
%                      needn't any ended-char\\[2mm]
%   {\bf v04}.18 &2003/04/22 &modify: |\@bxy@kern|\\
%   v04.19 &2003/04/23 &change: use |\par| instead of
%                      |\enlinechar| in def. of |\bxx|\\
%   v04.19 &2003/05/06 &use `|;|'  as delimiter
%                      in definition of |\bxx|\\
%   v04.20 &2003/05/11 &optimize: |\@bnone|\\
%   v04.21 &2003/05/12 &\emph{changes forgotten}\\[2mm]
%   {\bf v05}.23 &2003/05/13 &add: |\xgetallbut|, |\xkillallbut|;
%                       optimize\\
%   v05.24 &2003/05/13 &remove: |\ifdetail|, |\if@xnil| (boolean var.)\\
%          &           &and optimize\\
%   v05.25 &2003/05/14 &fix: some litle bugs;
%                       remove: |\@act|\\
%   v05.26 &2003/05/14 &remove: bad options\\
%   v05.27 &2003/05/15 &bug: |\bxx(verbatim)...;| failed\\
%   v05.28 &2003/05/17 &bug: |\foo-section| cause error\\
%          &           &move: |\foo-section| to the very end of package\\
%   v05.29 &2003/05/18 &bug: |\bxx| canot be nested.\\
%          &           &(nesting: |deactive bxx| causes error)\\
%          &           &(nesting: |\xgetall| works well, but\\
%          &           &contents of |hint| is too bad.)\\
%   v05.30 &2003/08/xx &rewrite: document (vietnamese), optimize\\
%   v05.31 &2003/09/20 &rewrite: document (english), optimize\\
%   v05.32 &2003/09/20 &rewrite: document (english), optimize\\
%          &           &remove: token list |\everyactivebxx| \\
%          &           &bug: cannot use |\xgetallbut|, |\xkillallbut|,\\
%          &           &|\xget|, etc., before any |\xenv(#env)|\\
%   v05.33 &2003/09/21 &remove: initialization\\
%          &           &add: |\xopenhint|\\
%   v05.34 &2003/09/22 &bug: otions donot work.\\
%          &           &fix: put |\@@xdetail|
%                       before |\DeclareOption{detailon}|\\
%          &           &bug: if |\xopenlib;| before any |\xget...|\\
%          &           &then |\bxx| is |unknown control sequence|\\
%          &           &fix: |\let\bxx\@bxx| to initialize\\
%   v05.39 &2003/09/20 &(future) optimize: |\@xspec|
% \end{tabular}
%
% \section{Miscellanea}
%
% The author write this package because he
% has some big libraries of mathematical exercises,
% but he really doesn't like |copying| and |pasting|
% everytime he edits a new test for students.
%
% This package may contains some bugs.
% So the author hopes that you can help him, even a bit.
% 
% In section \ref{sec:howwillyoudo},
% the author mentioned the ability of getting the data items
% whose |ID|s are numbered oddly....
% Currently this feature is unsupported .
% Please wait for the next version,
% or you should do something by yourself!
%
% The author's English isnot quite well.
% He often mis-spells and dumps into error.
% He would like to be sorry!\\[1mm]
%
% If you find out that the package is useful for your private works,
% please send to the author a little notice. Thank you very much!
%
% The emails of the author is\\
% \centerline{\ttfamily kyanh@inic.biz, kyanh@linuxmail.org}
%
% \Finale
%
% \iffalse
%
%<*example>
\documentclass{article}
\usepackage{ktv-texdata}

\usepackage{amsthm}

\def\back:{$\backslash$}

\begin{document}

\begin{center}
\Large\bf TEST :: package == {\tt ktv-texdata.sty}
\end{center}

\section{Create a data file}
See file {\tt ktv-data.tex}.
There're three part in the data file.
In the first and the last part, all `\verb~bxx~'
specify their own environment. In the second part,
the data items omit the enrironment (use default).

There're some special data items:
the \verb~\bxx(yyy)2;..\exx~ is followed by a hint,
the \verb~\bxx(yyy)[TT]5;..\exx~ specifies
an optional argument for enrivonment \verb~yyy~.

\section{Define the envrinoment}
\begin{verbatim}
\theoremstyle{definition}
\newtheorem{xxx}{XX}
\newtheorem{zzz}{ZZ}
\newtheorem{yyy}{YY}
\end{verbatim}
\theoremstyle{definition}
\newtheorem{xxx}{XX}
\newtheorem{zzz}{ZZ}
\newtheorem{yyy}{YY}

\section{Specified default environment by \back:env(xxx)}

\xenv(xxx)

\begin{verbatim}
\xenv(xxx)
\end{verbatim}
You should specify the default enrironment
before any calling to \back:xget, \back:xkill, \back:xgetll,
\back:xkillall, etc.

Otherwise, if your data file contains some `\verb|bxx|'
that doesnot specify the environment, you will receive an error!
(In the source code of the test, remove the line \back:env(xxx)
to see what will be going.)

\section{Specified data file by \back:env(xxx)}
\xlib ktv-data;

\begin{verbatim}
\xlib ktv-data;
\end{verbatim}
You also specify the the library, or data file.
The above specification affects on any \back:xopenlib;
that's put after this line in the source code of the test.

\section{\back:xget\{2,4\}}
\begin{verbatim}
\xget{2,4}
\xopenlib;
\end{verbatim}
\xget{2,4}
\xopenlib;

\section{Uneffect \back:xenv(zzz) after \back:xget\{1,4\}}
\begin{verbatim}
\xget{1,4}
\xenv(zzz)
\xopenlib;
\end{verbatim}
This example illustrate that any \back:xenv(...)
that follows a \back:xget doesnot take any affect on that \back:xget.
Try to figure out this illustration.
\xget{1,4}
\xenv(zzz)
\xopenlib;

\section{Now use \back:xenv(yyy)}
...to change the default environment to zzz.
\begin{verbatim}
\xenv(yyy)
\end{verbatim}
\xenv(yyy)

\section{Use \back:xgetallbut(zzz)\{1,2\}}
\begin{verbatim}
\xgetallbut(zzz){1,2}
\xopenlib;
\end{verbatim}
\xgetallbut(zzz){1,2}
\xopenlib;

\section{Use \back:xkillallbut(zzz)\{1,2\}}
\begin{verbatim}
\xkillallbut(zzz){1,2}
\xopenlib;
\end{verbatim}
\xkillallbut(zzz){1,2}
\xopenlib;

\section{Use \back:xspec}
This macro reserves the order of the `bxx'
that you specify in the argument.

\subsection{This is \back:xspec\{1,5,2\}}
\begin{verbatim}
\xspec{1,5,2}
\end{verbatim}
This calling uses default environment  \verb~zzz~.
\xspec{1,5,2}

\subsection{This is \back:xspec(zzz)\{1,5,2\}}
\begin{verbatim}
\xspec(zzz){1,5,2}
\end{verbatim}
\xspec(zzz){1,5,2}

\section{Turn off the details by \back:xdetailoff}
\xdetailoff
\begin{verbatim}
\xdetailoff
\end{verbatim}
You now cannot see anything on the right margin.

\section{Hint}
\begin{verbatim}
\xopenhint
\end{verbatim}
Some exercise should have a hint.\\
Of course, only hints of the active `bxx' will be accepted.
\xopenhint

\section{Now the time you try the package yourself!}
Thank you for your enjoying.\\[3mm]
Please send any bug, report,... to the author at
\begin{verbatim}
                      kyanh@linuxmail.org
\end{verbatim}
\end{document}
%</example>
%
%<*datafile>
\bxx(yyy)1;
This is {\it yyyyy}.one.
\exx
\bxx(yyy)2;
This is {\it yyyyy}.two with hint.
\exx
\begin{hint}
This is HINT of {\it yyyyy}.2
\end{hint}
\bxx(yyy)3;
This is {\it yyyyy}.three.
\exx
\bxx(yyy)4;
This is {\it yyyyy}.four.
\exx
\bxx(yyy)[TT]5;
This is {\it yyyyy}.six with header TT
\exx

^^M % ------------------------------------------------------------------

\bxx1;
This is one.
\\In the data file, this `bxx' doesnot specify the enrvironment.
\\So this used default environment.
\exx
\bxx2;
This is two with hint.
\\In the data file, this `bxx' doesnot specify the enrvironment.
\\So this used default environment.
\exx
\begin{hint}
This is hint of two.
\\In the data file, this `bxx' doesnot specify the enrvironment.
\\So this used default environment.
\end{hint}
\bxx3;
This is three.
\\In the data file, this `bxx' doesnot specify the enrvironment.
\\So this used default environment.
\exx
\bxx4;
This is four.
\\In the data file, this `bxx' doesnot specify the enrvironment.
\\So this used default environment.
\exx
\bxx5;
This is six.
\\In the data file, this `bxx' doesnot specify the enrvironment.
\\So this used default environment.
\exx

^^M % ------------------------------------------------------------------

\bxx(zzz)1;
This is ZZZZZZZ.one.
\exx
\bxx(zzz)2;
This is ZZZZZZZ.two.
\\Containning an equation:
\begin{equation}
\int_{0}^{\infty}f(x)\,dx = \pi
\end{equation}
\exx
\bxx(zzz)3;
This is ZZZZZZZ.three.
\exx
\bxx(zzz)4;
This is ZZZZZZZ.four.
\exx
\bxx(zzz)[TT]5;
This is ZZZZZZZ.six with header TT.
\exx
%</datafile>
% \fi