% \iffalse meta-comment
%
% xkeyval package by Hendri Adriaens.
%
% Extract package files and examples and create documentation:
%    latex xkeyval.dtx
%    latex xkeyval.dtx
%    bibtex xkeyval
%    makeindex -s gglo.ist -o xkeyval.gls xkeyval.glo
%    makeindex -s gind.ist -o xkeyval.ind xkeyval.idx
%    latex xkeyval.dtx
%    latex xkeyval.dtx
%
% To finish the installation you have to move the following
% files into a directory searched by TeX:
%    xkeyval.tex
%    keyval.tex
%    xkvtxhdr.tex
% Additional to these files, LaTeX should be able to locate
% the following files:
%    xkeyval.sty
%    xkvltxp.sty
%
%% ---------------------------------------
%% Copyright (C) 2004-2005 Hendri Adriaens
%% ---------------------------------------
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% 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.3 or later is part of all distributions of LaTeX
%% version 2003/12/01 or later.
%%
%% This work has the LPPL maintenance status "maintained".
%%
%% This Current Maintainer of this work is Hendri Adriaens.
%%
%% This work consists of the files xkeyval.dtx and pst-xkey.dtx and
%% the from xkeyval.dtx derived files keyval.tex, xkvtxhdr.tex,
%% xkeyval.sty, xkeyval.tex, xkvltxp.sty, xkveca.cls, xkvecb.cls,
%% xkvesa.sty, xkvesb.sty, xkvesc.sty, xkvex1.tex, xkvex2.tex,
%% xkvex3.tex and xkvex4.tex and the from pst-xkey.dtx derived
%% files pst-xkey.sty and pst-xkey.tex.
%%
%% The following files constitute the xkeyval bundle and must be
%% distributed as a whole: readme, pst-xkey.pdf, xkeyval.pdf, keyval.tex,
%% pst-xkey.sty, pst-xkey.tex, xkeyval.sty, xkeyval.tex, xkvltxp.sty,
%% xkvtxhdr.tex, pst-xkey.dtx and xkeyval.dtx.
%%
% \fi
%
% \iffalse
%<*batchfile>
\begingroup
\input docstrip
\keepsilent
\preamble
\endpreamble
\askforoverwritefalse
\generate{
  \file{xkeyval.tex}{\from{xkeyval.dtx}{tex}}
  \file{xkeyval.sty}{\from{xkeyval.dtx}{latex}}
  \file{xkvltxp.sty}{\from{xkeyval.dtx}{ltxpatch}}
  \file{keyval.tex}{\from{xkeyval.dtx}{keyval}}
  \file{xkvtxhdr.tex}{\from{xkeyval.dtx}{header}}
  \file{xkvex1.tex}{\from{xkeyval.dtx}{xkvex1}}
  \file{xkvex2.tex}{\from{xkeyval.dtx}{xkvex2}}
  \file{xkvex3.tex}{\from{xkeyval.dtx}{xkvex3}}
  \file{xkvex4.tex}{\from{xkeyval.dtx}{xkvex4}}
  \file{xkveca.cls}{\from{xkeyval.dtx}{xkveca}}
  \file{xkvecb.cls}{\from{xkeyval.dtx}{xkvecb}}
  \file{xkvesa.sty}{\from{xkeyval.dtx}{xkvesa}}
  \file{xkvesb.sty}{\from{xkeyval.dtx}{xkvesb}}
  \file{xkvesc.sty}{\from{xkeyval.dtx}{xkvesc}}
  \file{xkvpream.ble}{\from{xkeyval.dtx}{preamble}}
  \file{xkeyval.bib}{\from{xkeyval.dtx}{bib}}
}
\endgroup
%</batchfile>
%<*driver>
\documentclass[a4paper]{ltxdoc}
\input{xkvpream.ble}
\begin{document}
\DocInput{xkeyval.dtx}
\let\Section\section\def\section*#1{\Section*{#1}\addcontentsline{toc}{section}{#1}}
\bibliographystyle{plain}
\bibliography{xkeyval}
\section*{Acknowledgements}
The author is grateful to Josselin Noirel, Till Tantau, Herbert
Vo\ss\ and Carsten Heinz for help and suggestions. Thanks go to
Donald Arseneau for contributing the |\@ifnextcharacter|
macro. Special thanks go to Uwe Kern for his ideas for improving the
functionality of this package, a lot of useful comments on the
package and the documentation and for contributing the
|\@selective@sanitize| macro.
\PrintChangesX
\PrintIndex
\end{document}
%</driver>
% \fi
%
% \changes{v1.0}{2004/04/29}{Initial release}
% \changes{v1.2}{2004/05/08}{Change to \ci{DeclareOptionXf}; macro is now replaced}
% \changes{v1.3}{2004/05/09}{Revision of documentation}
% \changes{v1.4}{2004/08/24}{Made package \TeX\ compatible}
% \changes{v1.4}{2004/08/24}{Renamed macros to \pf{keyval} names}
% \changes{v1.5}{2004/09/27}{Corrected some minor bugs}
% \changes{v1.5}{2004/09/27}{Added \pf{pst-xkey}}
% \changes{v1.5}{2004/09/27}{Revised documentation}
% \changes{v1.6}{2004/10/05}{Changed loading preventions}
% \changes{v1.6}{2004/10/05}{Corrected typos}
% \changes{v1.7}{2004/11/25}{Improved coding}
% \changes{v1.7}{2004/11/25}{Package uses \eTeX\ if available}
% \changes{v1.7}{2004/11/25}{Changed \LaTeX\ patch from \texttt{.tex} to \texttt{.sty}}
% \changes{v1.7}{2004/11/25}{Updated license information}
% \changes{v1.7}{2004/11/25}{Added system to control the scope of definitions to various macros}
% \changes{v1.8 }{2004/12/13}{Made package robust for catcode changes of equality character}
% \changes{v1.8 }{2004/12/13}{Fixed inconsistency of treatment of spaces in key and family names, all left untouched now}
% \changes{v1.8c}{2005/01/01}{Simplified and improved some code}
% \changes{v1.8d}{2005/01/02}{Restructured package to indicate general tools and their implementation}
% \changes{v1.8d}{2005/01/02}{Renamed \texttt{xkeyval.def} to \texttt{xkvtxhdr.tex}}
% \changes{v1.9}{2005/01/16}{Added \ci{delpresetkeys}}
% \changes{v1.9}{2005/01/16}{Added \ci{delsavekeys}}
% \changes{v1.9}{2005/01/16}{Updated license information}
% \changes{v2.0}{2005/01/30}{Made \ci{setkeys} nestable}
%
% \GetFileInfo{xkeyval.sty}
%
% \CheckSum{2208}
%
%\DoNotIndex{\:,\@,\@@,\@backslashchar,\@ehc,\@empty,\@expandtwoargs,
%\@for,\@forloop,\@fornoop,\@fortmp,\@gobble,\@gobbletwo,\@if,\@ifnch,
%\@ifnextchar,\@iforloop,\@ifundefined,\@let@token,
%\@namedef,\@nil,\@nnil,\@sptoken,\@temptokena,\@testopt,\@undefined,
%\@xifnch,\\,\afterassignment,\AtEndOfPackage,\begingroup,\bgroup,
%\catcode,\count@,\csname,\def,\do,\edef,\else,\endcsname,\endgroup,
%\endinput,\errmessage,\escapechar,\eTeXversion,\expandafter,\fi,
%\futurelet,\gdef,\global,\if,\ifcsname,\iffalse,\ifin@,\iftrue,\ifx,
%\in@,\in@@,\in@false,\in@true,\input,\kernel@ifnextchar,\let,\long,
%\lowercase,\m@ne,\meaning,\message,\NeedsTeXFormat,\newif,\newtoks,
%\noexpand,\protect,\ProvidesFile,\ProvidesPackage,\relax,\reserved@a,
%\reserved@b,\reserved@c,\reserved@d,\space,\string,\strip@prefix,\the,
%\toks@,\xdef,\XKV@header,\XKV@inpoxfalse,\XKV@inpoxtrue,\XKV@knffalse,
%\XKV@knftrue,\XKV@plfalse,\XKV@pltrue,\XKV@resa,\XKV@rkvfalse,
%\XKV@rkvtrue,\XKV@stfalse,\XKV@sttrue,\XKV@tempa,\XKV@tempb,\XKV@tempc,
%\XKV@tempd,\XKV@tfam,\XKV@tkey,\XKV@toks,\XKVcatcodes,\XKV@sgtrue,
%\zap@space,\@@cmd,\@@tok,\@i,\@ii,\@iii,\@iv,\=,\XKV@rgtrue,\XKV@rgfalse,
%\z@,\@ne,\m@ne,\count@,\advance,\ifnum,\,,\@M,\XKV@resb,\XKV@sgfalse}
%
% \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         \~}
%
%\title{The \pf{xkeyval} package
%\thanks{This package can be downloaded from the CTAN mirrors:
%\texttt{/macros/latex/contrib/xkeyval}. See \texttt{xkeyval.dtx} for
%information on installing \pf{xkeyval} into your \TeX\ or \LaTeX\
%distribution and for the license of this package.}}
%\author{Hendri Adriaens\\\url{http://stuwww.uvt.nl/~hendri}}
%\date{\fileversion\ (\filedate)}
%\maketitle
%
%\begin{abstract}\noindent
%This package is an extension of the \pf{keyval} package and offers
%more flexible macros for defining and setting keys. The package
%provides a pointer and a preset system. Furthermore, it supplies
%macros to allow class and package options to contain options of the
%|key=value| form. A \LaTeX\ kernel patch is provided to avoid
%premature expansions of macros in class or package options. A
%specialized system for setting \pf{PSTricks} keys is provided by
%the \pf{pst-xkey} package.
%\end{abstract}
%
%\begin{multicols}{2}
%[\section*{Contents}
%\setlength{\columnseprule}{.4pt}
%\setlength{\columnsep}{18pt}]
%\tableofcontents
%\end{multicols}
%
%\newpage
%\section{Introduction}\label{sec:intro}
%This package is an extension of the \pf{keyval} package by David Carlisle
%\cite{keyval} and offers more flexible and robust macros for defining and
%setting keys. Using keys in macro definition has the advantage that
%the 9 arguments maximum can easily be avoided and that it reduces
%confusion in the syntax of your macro when compared to using a lot
%of (optional) arguments. Compare for instance the following
%possible syntaxes of the macro |\mybox| which might for instance
%use its arguments to draw some box containing text.
%\begin{example}
% \mybox[5pt][20pt]{some text}[red][white][blue]
% \mybox[text=red,background=white,frame=blue,left=5pt,right=20pt]{some text}
%\end{example}
%Notice that, to be able to specify the frame color in the first
%example, the other colors need to be specified as well. This is not
%necessary in the second example and these colors can get default
%values. The same thing holds for the margins.\\
%
%The idea is that one first defines some keys using the tools
%presented in section~\ref{sec:define} in the preamble or in a
%package or class. These keys can perform some function with the user
%input. The way to submit user input to these key macros, is by using
%one of the user interfaces described in sections~\ref{sec:setkeys},
%\ref{sec:pointers} and~\ref{sec:preset}. The main user interface is
%provided by the |\setkeys| command. Using these interfaces, one can
%simplify macro syntax and for instance define the |\mybox| macro
%above as follows.
%\begin{example}
% \define@cmdkey{mybox}{background}
% \define@key{mybox}{left}{\setlength\myleft{#1}}
% % and some other keys
% \def\mybox{\@ifnextchar[\@mybox{\@mybox[]}}
% \def\@mybox[#1]#2{%
%   \setkeys{mybox}{#1}%
%   % some operations
% }
%\end{example}\bigskip
%
%Several remarks should be made with respect to processing the user
%input. Assuming that |keya| up to |keyd| are properly defined, one
%could do the following.
%\begin{example}
% \setkeys{family}{keya= test a, keyb={test b,c,d}, ,keyc , keyd=end}
%\end{example}
%From values consisting entirely of a |{ }| group, the outer braces
%will be stripped off. This allows the user to `hide' any commas or
%equality signs that appear in the value of a key. This means that
%when using braces, \pf{xkeyval} will not terminate the |key=value|
%pair when it encounters a comma. For instance, see the value of
%|keyb| in the example above. The same story holds for the equality
%sign. Notice further that any white space around the characters |=|
%and |,| is ignored. Finally, |keyc| did not get a value. If no
%default value has been defined for this key, an error will be
%generated. More details can be found in sections~\ref{sec:define},
%\ref{sec:setkeys}, \ref{sec:pointers} and \ref{sec:preset}.\\
%
%Both keys defined using the \pf{keyval} and \pf{xkeyval} can be set
%by this package. The \pf{xkeyval} macros allow for scanning
%multiple families for keys. This can, for example, be used to create
%local families for custom macros and environments which may not
%access keys meant for other macros and environments, while at the
%same time, allowing the use of a single command to set all of the
%keys from the different families globally.\\
%
%The package is compatible to plain \TeX\ and redefines several
%\pf{keyval} macros to provide an easy way to switch between using
%\pf{keyval} and \pf{xkeyval}. This might be useful for package
%writers that cannot yet rely on the availability of \pf{xkeyval} in
%a certain distribution. After loading \pf{xkeyval}, loading
%\pf{keyval} is prevented to make sure that the extended macros of
%\pf{xkeyval} will not be redefined. Some basic \pf{keyval} macros
%are supplied in |keyval.tex| to guarantee compatibility to packages
%that use those macros. Section~\ref{sec:issues} provides more
%information about this issue.\\
%
%To load \pf{xkeyval}, plain \TeX\ users do |\input xkeyval|. \LaTeX\
%users do either |\usepackage{xkeyval}| or
%|\RequirePackage{xkeyval}|. It is mandatory for \LaTeX\ users to
%load \pf{xkeyval} at any point after the |\documentclass| command.
%Loading \pf{xkeyval} from the class which is the document class
%itself is possible. The package will use the \eTeX\ engine when
%available. In particular, |\ifcsname| is used whenever possible to
%avoid filling \TeX's hash with useless entries for instance when
%searching for keys in families.\\
%
%\pf{PSTricks} \cite{PSTricksWeb,PSTricks} package authors should
%have a look at the \pf{pst-xkey} package contained in the
%\pf{xkeyval} package distribution \cite{pst-xkey} for an options
%system based on \pf{xkeyval}, but which is specialized in defining
%and setting \pf{PSTricks} keys.\\
%
%The organization of this documentation is as follows.
%Section~\ref{sec:define} will discuss the macros available to define
%keys. Section~\ref{sec:setkeys} will continue with describing the
%macros that can set the keys. Section~\ref{sec:pointers} explains
%special syntax which will allow saving and copying key values. In
%section~\ref{sec:preset}, the preset system will be introduced.
%Section~\ref{sec:catcodes} will explain how \pf{xkeyval} protects
%itself for catcode changes of the comma and the equality sign by
%other packages. The \pf{xkeyval} package also provides commands to
%declare and process class and package options. These will be
%discussed in section~\ref{sec:options}. An extension of the \LaTeXe\
%kernel is discussed in section~\ref{sec:macrooptions}. This
%extension provides a way to use expandable macros in package
%options. Sections~\ref{sec:errors} and~\ref{sec:issues} discuss
%feedback that \pf{xkeyval} might give and known issues,
%respectively.\\
%
%Throughout this documentation, you will find some examples with a
%short description. More examples can be found in some example files
%that come with this package. See section~\ref{sec:sande} for more
%information. This section also provides the information how to
%generate the source code documentation from the source. This documentation
%provides the programming details of \pf{xkeyval}.
%
%\section{Defining and checking keys}\label{sec:define}
%\changes{v1.4}{2004/08/24}{Added prefix options to macros}
%\changes{v1.7}{2004/11/25}{Removed optional key checking from define macros}
%
%\subsection{Ordinary keys}\label{sec:odefine}
%This section describes how to define ordinary keys.\\
%
%\begin{command}
% `\ci{define@key}\oarg{prefix}\marg{family}\marg{key}\oarg{default}\marg{function}'
%\end{command}
%\SpecialUsageIndex{\define@key}
%This defines a macro |\prefix@family@key| with one argument holding
%\meta{function}. The default value for \meta{prefix} is |KV|. This
%is the standard throughout the package to simplify mixing
%\pf{keyval} and \pf{xkeyval} keys. When \meta{key} is used in a list
%of options containing |key=value|, the macro |\prefix@family@key|
%receives |value| as its argument. The argument can be accessed by
%\meta{function} by using |#1| inside the function.
%\begin{example}
% \define@key{family}{key}{The input is: #1}
%\end{example}
%\pf{xkeyval} will generate an error when the user omits |=value| for
%a key in the options list. To avoid this, the optional argument can
%be used to specify a  default value.
%\begin{example}
% \define@key{family}{key}[none]{The input is: #1}
%\end{example}
%This will additionally define a macro |\prefix@family@key@default|
%as a macro with no arguments and definition
%|\prefix@family@key{none}| which will be used when |=value| is
%missing for |key|.\\
%
%When \meta{prefix} is specified and empty, the macros created by
%|\define@key| will have the form |\family@key|. When \meta{family}
%is empty, the resulting form will be |\prefix@key|. When both
%\meta{prefix} and \meta{family} are empty, the form is |\key|.\\
%
%The intended use for \meta{family} is to create distinct sets of
%keys. This can be used to avoid a macro setting keys meant for
%another macro only. The optional \meta{prefix} can be used to
%identify keys specifically for your package. Using a package
%specific prefix reduces the probability of multiple packages
%defining the same key macros. This optional argument can also be
%used to set keys of some existing packages which use a system based
%on \pf{keyval}.\footnote{Like \pf{PSTricks}, which uses a system
%originating from \pf{keyval}, but which has been modified to use no
%families and |psset| as prefix.}\label{page:pstricks}\\
%
%We now define some keys to be used in examples throughout this
%documentation.
%\begin{example}
% \define@key[my]{familya}{keya}{#1}
% \define@key[my]{familya}{keyb}{#1}
% \define@key[my]{familyb}{keyb}{#1}
% \define@key[my]{familya}{keyc}{#1}
%\end{example}
%
%\subsection{Boolean keys}\label{sec:bdefine}
%\changes{v1.5}{2004/09/27}{Added boolean keys}
%This section describes boolean keys which are either true or false.
%When comparing the macro of this section to |\define@key| of
%section~\ref{sec:odefine}, we see that the
%\meta{function} is known (namely, set a conditional to true or
%false) and hence the macro has one mandatory argument less.\\
%
%\begin{command}
% `\ci{define@boolkey}\oarg{prefix}\marg{family}\marg{key}\oarg{default}'
%\end{command}
%\SpecialUsageIndex{\define@boolkey}
%This creates a conditional of the form
%|\ifprefix@family@key|\footnote{\label{foot:1}When you want to use
%this macro directly, either make sure that neither of the input
%parameters contains characters with a catcode different from 11
%(hence no \texttt{-} for instance), reset the catcode of the
%offending characters internally to 11 or use
%\texttt{\ci{csname}\dots\ci{endcsname}} to construct macro names,
%(for instance, \texttt{\ci{csname} ifpre@some-fam@key\ci{endcsname}}).
%See for more information section~\ref{sec:macrostructs}.} using
%|\newif|\footnote{The \LaTeX\ of implementation \ci{newif} is used
%because it can be used in the replacement text of a macro, whereas
%the plain \TeX\ \ci{newif} is defined \ci{outer}.} (which initiates
%the conditional to |\iffalse|) and a key macro of the form
%|\prefix@family@key| which is defined as
%|\XKV@setbool{prefix@family@key}{#1}|. The macro |\XKV@setbool| only
%takes the values |true| and |false| and uses that to set the
%conditional. The default value can only be |true| or |false| as
%well.
%\begin{example}
% \define@boolkey{fam}{frame}
%\end{example}
%This example creates |\ifKV@fam@frame| and defines |\KV@fam@frame|
%to expand to |\XKV@setbool{KV@fam@frame}{#1}|.
%
%\subsection{Command keys}\label{sec:cdefine}
%\changes{v1.7}{2004/11/25}{Added command keys}
%This section describes command keys. The macro described here is a
%specialized version of |\define@key| described in section~\ref{sec:odefine}
%and \meta{function} will store the user input in a macro.\\
%
%\begin{command}
% `\ci{define@cmdkey}\oarg{prefix}\marg{family}\marg{key}\oarg{default}'
%\end{command}
%\SpecialUsageIndex{\define@cmdkey}
%This defines the key macro |\prefix@family@key|\footref{foot:1} with one argument
%to define a macro in the following way: |\def\prefix@family@key@cmd{#1}|.
%\begin{example}
% \define@cmdkey{fam}{text}
%\end{example}
%This example defines the key |\KV@fam@text| to store user input to
%the |\setkeys| command (see section~\ref{sec:setkeys}) in the macro
%|\KV@fam@key@text@cmd|.
%
%\subsection{Checking keys}\label{sec:check}
%\changes{v1.7}{2004/11/25}{Changed name of \ci{XKV@ifku}}
%This section provides a macro to check the existence of keys.\\
%
%\begin{command}
% `\ci{key@ifundefined}\oarg{prefix}\marg{families}\marg{key}\marg{undefined}\marg{defined}'
%\end{command}
%\SpecialUsageIndex{\key@ifundefined}
%This macro executes \meta{undefined} when \meta{key} is not defined
%in a family listed in \meta{families} using \meta{prefix} (which is
%|KV| by default) and \meta{defined} when it is. If \meta{defined} is
%executed, |\XKV@tfam| holds the first family in the list \meta{families}
%that holds \meta{key}. If \meta{undefined} is executed, |\XKV@tfam|
%contains the last family of the list \meta{families}.
%\begin{example}
% \key@ifundefined[my]{familya,familyb}{keya}{`keya' not defined}{`keya' defined}
%\end{example}
%This example results in |`keya' defined| and |\XKV@tfam| holds |familya|.
%
%\subsection{Disabling keys}\label{sec:disable}
%\changes{v1.7}{2004/11/25}{Added \ci{disable@keys}}
%It is also possible to disable keys after use as to prevent the key
%from being used again.\\
%
%\begin{command}
% `\ci{disable@keys}\oarg{prefix}\marg{family}\marg{keys}'
%\end{command}
%\SpecialUsageIndex{\disable@keys}
%When you disable a key, the use of this key will produce a warning
%in the log file. Disabling a key that hasn't been defined will result
%in an error message.
%\begin{example}
% \disable@keys[my]{familya}{keya,keyb}
%\end{example}
%This would make |keya| and |keyb| produce a warning when one tries to
%set these keys.
%
%\section{Setting keys}\label{sec:setkeys}
%\changes{v1.4}{2004/08/24}{Added prefix options to macros}
%This section describes the available macros for setting keys. All of
%the macros in this section have an optional argument \meta{prefix}
%which determines part of the form of the keys that the macros will
%be looking for. See section~\ref{sec:define}. This optional argument
%takes the value |KV| by default.\\
%
%\begin{command}
% `\ci{setkeys}\oarg{prefix}\marg{families}\oarg{na}\marg{keys}'
%\end{command}
%\SpecialUsageIndex{\setkeys}
%This macro sets keys of the form |\prefix@family@key| where |family|
%is an element of the list \meta{families} and |key| is an element of
%the options list \meta{keys} and not of \meta{na}. The latter list
%can be used to specify keys that should be ignored by the macro. If
%a key is defined by more families in the list \meta{families}, the
%first family from the list defining the key will set it. No errors
%are produced when \meta{keys} is empty. If |family| is empty, the
%macro will set keys of the form |\prefix@key|. If \meta{prefix} is
%specified and empty, the macro will set keys of the form
%|\family@key|. If both \meta{prefix} and |family| are empty, the
%macro will set keys of the form |\key|. This is in line with how key
%macros are constructed (see section~\ref{sec:define}).
%\begin{example}
% \setkeys[my]{familya,familyb}{keya=test}
% \setkeys[my]{familya,familyb}{keyb=test}
% \setkeys[my]{familyb,familya}{keyb=test}
%\end{example}
%In the example above, line 1 will set |keya| in family |familya|.
%The next line will set |keyb| in |familya|. The last one sets |keyb|
%in family |familyb|.\\
%
%When you want to use commas or equality signs in the value of a key,
%surround the value by braces, as shown in the example below.
%\begin{example}
% \setkeys[my]{familya}{keya={some=text,other=text}}
%\end{example}\bigskip
%
%It is possible to nest |\setkeys| commands in other |\setkeys| commands
%or in key definitions. The following, for instance,
%\begin{example}
% \define@key[my]{familyb}{keyc}{#1~and~\setkeys[my]{familya}{keya=#1}}
% \setkeys[my]{familyb}{keyc=a\setkeys[my]{familya}{keya=~and~b}}
%\end{example}
%returns |a and b and a and b|.\\
%
%\begin{command}
% `\ci{setkeys*}\oarg{prefix}\marg{families}\oarg{na}\marg{keys}'
%\end{command}
%\SpecialUsageIndex{\setkeys*}
%The starred version of |\setkeys| sets keys which it can locate in
%the given families and will not produce errors when it cannot find a
%key. Instead, these keys and their values will be appended to a list
%of remaining keys in the macro |\XKV@rm| after the use of
%|\setkeys*|. Keys listed in \meta{na} will be ignored fully and
%will not be appended to the |\XKV@rm| list.
%\begin{example}
% \setkeys*[my]{familyb}{keya=test}
%\end{example}
%Since |keya| is not defined in |familyb|, the value in the example
%above will be stored in |\XKV@rm| (so |\XKV@rm| expands to
%|keya=test|) for later use and no errors are raised.\\
%
%\begin{command}
% `\ci{setrmkeys}\oarg{prefix}\marg{families}\oarg{na}'
%\end{command}
%\SpecialUsageIndex{\setrmkeys}
%The macro |\setrmkeys| sets the remaining keys given by the list
%|\XKV@rm| stored previously by a |\setkeys*| (or |\setrmkeys*|)
%command in \meta{families}. \meta{na} again lists keys that should
%be ignored. It will produce an error when a key cannot be located.
%\begin{example}
% \setrmkeys[my]{familya}
%\end{example}
%This submits |keya=test| from the previous
%|\setkeys*| command to |familya|. |keya| will be set.\\
%
%\begin{command}
% `\ci{setrmkeys*}\oarg{prefix}\marg{families}\oarg{na}'
%\end{command}
%\SpecialUsageIndex{\setrmkeys*}
%The macro |\setrmkeys*| acts as the |\setrmkeys| macro but now,
%as with |\setkeys*|, it ignores keys that it cannot find and puts
%them again on the list stored in |\XKV@rm|. Keys listed in \meta{na}
%will be ignored fully and will not be appended to the list in
%|\XKV@rm|.
%\begin{example}
% \setkeys*[my]{familyb}{keya=test}
% \setrmkeys*[my]{familyb}
% \setrmkeys[my]{familya}
%\end{example}
%In the example above, the second line tries to set |keya| in
%|familyb| again and no errors are generated on failure. The last
%line finally sets |keya|.\\
%
%The combination of |\setkeys*| and |\setrmkeys| can be used to
%construct complex macros in which, for instance, a part of the keys
%should be set in multiple families and the rest in another family or
%set of families. Instead of splitting the keys or the inputs, the
%user can supply all inputs in a single argument and the two macros
%will perform the splitting and setting of keys for your macro, given
%that the families are well chosen.\\
%
%\begin{command}
% `\ci{setkeys+}\oarg{prefix}\marg{families}\oarg{na}\marg{keys}'
% `\ci{setkeys*+}\oarg{prefix}\marg{families}\oarg{na}\marg{keys}'
% `\ci{setrmkeys+}\oarg{prefix}\marg{families}\oarg{na}'
% `\ci{setrmkeys*+}\oarg{prefix}\marg{families}\oarg{na}'
%\end{command}
%\SpecialUsageIndex{\setkeys+}
%\SpecialUsageIndex{\setkeys*+}
%\SpecialUsageIndex{\setrmkeys+}
%\SpecialUsageIndex{\setrmkeys*+}
%\changes{v1.4}{2004/08/24}{Added \texttt{+} option to macros}
%These macros act as their counterparts without the |+|. However,
%when a key in \meta{keys} is defined by multiple families, this key
%will be set in \emph{all} families in \meta{families}. This can, for
%instance, be used to set keys defined by your own package and by
%another package with the same name but in different families with a
%single command.
%\begin{example}
% \setkeys+[my]{familya,familyb}{keyb=test}
%\end{example}
%The example above sets |keyb| in both families. See also
%section~\ref{sec:sande} for more examples.
%
%\section{Pointers}\label{sec:pointers}
%\changes{v1.4}{2004/08/24}{Added pointer syntax}
%\changes{v1.5}{2004/09/27}{Removed \ci{ifrecordkeyvals}}
%\changes{v1.7}{2004/11/25}{Changed value saving mechanism}
%\changes{v1.7}{2004/11/25}{Changed pointer mechanism}
%The \pf{xkeyval} package allows the use of pointers in key values.
%These pointers can be used to copy values of keys. Hence, one can
%reuse the value that has been submitted to a particular key in the
%value of another key. This section will first describe how
%\pf{xkeyval} can be made to save key values. After that, it will
%explain how to use these saved values again.\\
%
%\subsection{Saving values}\label{sec:savevalue}
%Saving a value for a particular key can be accomplished by using the
%|\savevalue| command with the key name as
%argument.
%\begin{example}
% \setkeys[my]{familya}{\savevalue{keya}=test}
%\end{example}
%\SpecialUsageIndex{\savevalue}
%This example will set |keya| as usual, but will additionally define
%the macro |\XKV@my@familya@keya@value| to expand to |test|. This
%macro can be used later on by \pf{xkeyval} to replace pointers. In
%general, values of keys will be stored in macros of the form
%|\XKV@prefix@family@key@value|. This implies that the pointer system
%can only be used within the same family (and prefix). We will come
%back to that in section~\ref{sec:usevalue}.\\
%
%Using the global version of this command, namely |\gsavevalue|,
%will define the value macro |\XKV@my@family@key@value| globally.
%In other words, the value macro won't survive after a
%|\begingroup|\texttt\dots|\endgroup| construct (for instance, an
%environment), when it has been created in this group using
%|\savevalue| and it will survive afterwards if |\gsavevalue| is
%used.
%\begin{example}
% \setkeys[my]{familya}{\gsavevalue{keya}=test}
%\end{example}
%\SpecialUsageIndex{\gsavevalue}
%This example will globally define |\XKV@my@familya@keya@value| to
%expand to |test|.\\
%
%Actually, in most applications, package authors do not want to require
%users to use the |\savevalue| form when using the pointer system internally.
%To avoid this, the \pf{xkeyval} package also supplies the following commands.
%\begin{command}
% `\ci{savekeys}\oarg{prefix}\marg{family}\marg{keys}'
% `\ci{gsavekeys}\oarg{prefix}\marg{family}\marg{keys}'
%\end{command}
%\SpecialUsageIndex{\savekeys}
%\SpecialUsageIndex{\gsavekeys}
%The |\savekeys| macro stores a list of keys for which the values
%should always be saved to a macro of the form
%|\XKV@prefix@family@save|. This will be used by |\setkeys| to check
%whether a value should be saved or not. The global version will
%define this internal macro globally so that the settings can escape
%groups (and environments). The |\savekeys| macro works
%incrementally. This means that new input will be added to an
%existing list for the family at hand if it is not in yet.
%\begin{example}
% \savekeys[my]{familya}{keya,keyc}
% \savekeys[my]{familya}{keyb,keyc}
%\end{example}
%The first line stores |keya,keyc| to |\XKV@my@familya@save|. The
%next line changes the content of this macro to |keya,keyc,keyb|.\\
%
%\begin{command}
% `\ci{delsavekeys}\oarg{prefix}\marg{family}\marg{keys}'
% `\ci{gdelsavekeys}\oarg{prefix}\marg{family}\marg{keys}'
% `\ci{unsavekeys}\oarg{prefix}\marg{family}'
% `\ci{gunsavekeys}\oarg{prefix}\marg{family}'
%\end{command}
%\SpecialUsageIndex{\delsavekeys}
%\SpecialUsageIndex{\gdelsavekeys}
%\SpecialUsageIndex{\unsavekeys}
%\SpecialUsageIndex{\gunsavekeys}
%The |\delsavekeys| macro can be used to remove some keys from an
%already defined list of save keys. No errors will be raised when one
%of the keys in the list \meta{keys} was not in the list. The global
%version |\gdelsavekeys| does the same as |\delsavekeys|, but will
%also make the resulting list global. The |\unsavekeys| macro can be
%used to clear the entire list of key names for which the values
%should be saved. The macro will make |\XKV@prefix@family@save|
%undefined. |\gunsavekeys| is similar to |\unsavekeys| but makes the
%internal macro undefined globally.
%\begin{example}
% \savekeys[my]{familya}{keya,keyb,keyc}
% \delsavekeys[my]{familya}{keyb}
% \unsavekeys[my]{familya}
%\end{example}
%The first line of this example initializes the list to contain
%|keya,keyb,keyc|. The second line removes |keyb| from this list and
%hence |keya,keyc| remains. The last line makes the list undefined and
%hence clears the settings for this family.\\
%
%It is important to notice that the use of the global version
%|\gsavekeys| will only have effect on the definition of the macro
%|\XKV@prefix@family@save|. It will not have an effect on how the key
%values will actually be saved by |\setkeys|. To achieve that a particular key
%value will be saved globally (like using |\gsavevalue|), use the
%|\global| specifier in the |\savekeys| argument.
%\begin{example}
% \savekeys[my]{familya}{keya,\global{keyc}}
%\end{example}
%\SpecialUsageIndex{\global}
%This example does the following. The argument |keya,\global{keyc}|
%is saved (locally) to |\XKV@my@familya@save|. When |keyc| is used in
%a |\setkeys| command, the associated value will be saved globally to
%|\XKV@my@familya@keya@value|. When |keya| is used, its value will be
%saved locally.\\
%
%All macros discussed in this section for altering the list of save keys
%only look at the key name. If that is the same, old content will be
%overwritten with new content, regardless whether |\global| has been
%used in the content. See the example below.
%\begin{example}
% \savekeys[my]{familya}{\global{keyb},keyc}
% \delsavekeys[my]{familya}{keyb}
%\end{example}
%The first line changes the list in |\XKV@my@familya@save| from
%|keya,\global{keyc}| to |keya,keyc,\global{keyb}|. The second line
%changes the list to |keya,keyc|.
%
%\subsection{Using saved values}\label{sec:usevalue}
%The syntax of a pointer is |\usevalue{keyname}| and can only be used
%inside |\setkeys| and friends. \pf{xkeyval} will
%replace a pointer by the value that has been saved for the key that
%the pointer is pointing to. If no value has been saved for this key,
%an error will be raised. The following example will demonstrate how
%to use pointers (using the keys defined in
%section~\ref{sec:odefine}).
%\begin{example}
% \setkeys[my]{familya}{\savevalue{keya}=test}
% \setkeys[my]{familya}{keyb=\usevalue{keya}}
%\end{example}
%The value submitted to |keyb| points to |keya|. This has the effect
%that the value recorded for |keya| will replace |\usevalue{keya}| and this
%value (here |test|) will be submitted to the key macro of |keyb|. Since
%the saving of values is prefix and family specific, pointers can
%only locate values that have been saved for keys with the same
%prefix and family as the key for which the pointer is used. Hence this
%\begin{example}
% \setkeys[my]{familya}{\savevalue{keya}=test}
% \setkeys[my]{familyb}{keyb=\usevalue{keya}}
%\end{example}
%will never work. An error will be raised in case a key value points
%to a key for which the value cannot be found or has not been stored.\\
%
%It is possible to nest pointers as the next example shows.
%\begin{example}
% \setkeys[my]{familya}{\savevalue{keya}=test}
% \setkeys[my]{familya}{\savevalue{keyb}=\usevalue{keya}}
% \setkeys[my]{familya}{keyc=\usevalue{keyb}}
%\end{example}
%This works as follows. First \pf{xkeyval} records the value |test|
%in a macro. Then, |keyb| uses that value. Besides that, the value
%submitted to |keyb|, namely |\usevalue{keya}| will be recorded in another
%macro. Finally, |keyc| will use the value recorded previously for
%|keyb|, namely |\usevalue{keya}|. That in turn points to the value saved for
%|keya| and that value will be used.\\
%
%It is important to stress that the pointer replacement will be done
%before \TeX\ or \LaTeX\ performs the expansion of the key macro and
%its argument (which is the value that has been submitted to the
%key). This allows pointers to be used in almost any application. (The
%exception is grouped material, to which we will come back later.)
%When programming keys (using |\define@key| and friends), you won't
%have to worry about the expansion of the pointers which might be
%submitted to your keys. The value that will be submitted to your key
%macro in the end, will not contain pointers. These have
%already been expanded and been replaced by the saved values.\\
%
%A word of caution is necessary. You might get into an infinite
%loop if pointers are not applied with care, as the examples below
%show. The first example shows a direct back link.
%\begin{example}
% \setkeys[my]{familya}{\savevalue{keya}=\usevalue{keya}}
%\end{example}
%The second example shows an indirect back link.
%\begin{example}
% \setkeys[my]{familya}{\savevalue{keya}=test}
% \setkeys[my]{familya}{\savevalue{keyb}=\usevalue{keya}}
% \setkeys[my]{familya}{\savevalue{keya}=\usevalue{keyb}}
%\end{example}
%In these cases, an error will be issued and further pointer replacement
%is canceled.\\
%
%As mentioned already, pointer replacement does not work inside
%grouped material, |{...}|, if this group is not around the entire
%value (since that will be stripped off, see section~\ref{sec:intro}).
%The following, for instance, will not work.
%\begin{example}
% \setkeys[my]{familya}{\savevalue{keya}=test}
% \setkeys[my]{familya}{keyb=\parbox{2cm}{\usevalue{keya}}}
%\end{example}
%The following provides a working alternative for this situation.
%\begin{example}
% \setkeys[my]{familya}{\savevalue{keya}=test}
% \setkeys[my]{familya}{keyb=\begin{minipage}{2cm}\usevalue{keya}\end{minipage}}
%\end{example}
%In case there is no appropriate alternative, we can work around this
%restriction, for instance by using a value macro directly.
%\begin{example}
% \setkeys[my]{familya}{\savevalue{keya}=test}
% \setkeys[my]{familya}{keyb=\parbox{2cm}{\XKV@my@familya@keya@value}}
%\end{example}
%When no value has been saved for |keya|, the macro
%|\XKV@my@familya@keya@value| is undefined. Hence one might want to
%do a preliminary check to be sure that the macro exists.\\
%
%Pointers can also be used in default values. We finish this section
%with an example which demonstrates this.
%\begin{example}
% \define@key{fam}{keya}{keya: #1}
% \define@key{fam}{keyb}[\usevalue{keya}]{keyb: #1}
% \define@key{fam}{keyc}[\usevalue{keyb}]{keyc: #1}
% \setkeys{fam}{\savevalue{keya}=test}
% \setkeys{fam}{\savevalue{keyb}}
% \setkeys{fam}{keyc}
%\end{example}
%Since user input is lacking in the final two commands, the default
%value defined for those keys will be used. In the first case, the
%default value points to |keya|, which results in the value |test|.
%In the second case, the pointer points to |keyb|, which points to
%|keya| (since its value has been saved now) and hence also in the
%final command, the value |test| will be submitted to the key macro
%of |keyc|.
%
%\section{Presetting keys}\label{sec:preset}
%\changes{v1.5}{2004/09/27}{Added preset system}
%In contrast to the default value system where users are required to
%specify the key without a value to assign it its default value, the
%presetting system does not require this. Keys which are preset will
%be set automatically by |\setkeys| when the user didn't use those
%keys in the |\setkeys| command. When users did use the keys which
%are also preset, |\setkeys| will avoid setting them again. This
%section again uses the key definitions of section~\ref{sec:odefine}
%in examples.\\
%
%\begin{command}
% `\ci{presetkeys}\oarg{prefix}\marg{family}\marg{head keys}\marg{tail keys}'
% `\ci{gpresetkeys}\oarg{prefix}\marg{family}\marg{head keys}\marg{tail keys}'
%\end{command}
%\SpecialUsageIndex{\presetkeys}
%\SpecialUsageIndex{\gpresetkeys}
%This macro will save \meta{head keys} to |\XKV@prefix@family@preseth|
%and \meta{tail keys} to |\XKV@prefix@family@presett|. Savings are
%done locally by |\presetkeys| and globally by |\gpresetkeys|
%(compare |\savekeys|, section~\ref{sec:savevalue}). The saved macros
%will be used by |\setkeys|, when they are defined, whenever
%\meta{family} is used in the \meta{families} argument of that macro.
%Head keys will be set before setting user keys, tail keys will be
%set afterwards. However, if a key appears in the user input, this
%particular key will not be set by any of the preset keys.\\
%
%The macro works incrementally. This means that new input for a
%particular key replaces already present settings for this key. If no
%settings were present yet, the new input for this key will be
%appended to the end of the existing list. The replacement ignores
%the fact whether a |\savevalue| or an |=| has been specified in the key
%input. Assuming that all keys in the next example have a default
%value, we could do the following.
%\begin{example}
% \presetkeys{fam}{keya=red,\savevalue{keyb},keyc}{}
% \presetkeys{fam}{\savevalue{keya},keyb=red,keyd}{}
%\end{example}
%After the first line of the example, the macro |\XKV@KV@fam@preseth|
%contains |keya=red,\savevalue{keyb},keyc|. After the second line of the
%example, the macro will contain |\savevalue{keya},keyb=red,keyc,keyd|.
%The \meta{tail keys} remain empty throughout the example.\\
%
%\begin{command}
% `\ci{delpresetkeys}\oarg{prefix}\marg{family}\marg{head keys}\marg{tail keys}'
% `\ci{gdelpresetkeys}\oarg{prefix}\marg{family}\marg{head keys}\marg{tail keys}'
%\end{command}
%\SpecialUsageIndex{\delpresetkeys}
%\SpecialUsageIndex{\gdelpresetkeys}
%These commands can be used to (globally) delete entries from the
%presets by specifying the key names for which the presets should be
%deleted. Continuing the previous example, we could do the following.
%\begin{example}
% \delpresetkeys{fam}{keya,keyb}{}
%\end{example}
%This redefines the list of head presets |\XKV@KV@fam@preseth| to
%contain |keyc,keyd|. As can be seen from this example, the exact
%use of a key name is irrelevant deletion.\\
%
%\begin{command}
% `\ci{unpresetkeys}\oarg{prefix}\marg{family}'
% `\ci{gunpresetkeys}\oarg{prefix}\marg{family}'
%\end{command}
%\SpecialUsageIndex{\unpresetkeys}
%\SpecialUsageIndex{\gunpresetkeys}
%This command clears the presets for \meta{family} and works just as
%|\unsavekeys|. It makes |\XKV@prefix@family@preseth| and
%|\XKV@prefix@family@presett| undefined. The global version will make the
%macros undefined globally.\\
%
%Two type of problems in relation to pointers could appear when
%specifying head and tail keys incorrectly. This will be demonstrated
%with two examples. In the first example, we would like to set |keya|
%to |blue| and |keyb| to copy the value of |keya|, also when the user
%has changed the preset value of |keya|. Say that we implement the
%following.
%\begin{example}
% \savekeys[my]{familya}{keya}
% \presetkeys[my]{familya}{keya=blue,keyb=\usevalue{keya}}{}
% \setkeys[my]{familya}{keya=red}
%\end{example}
%This will come down to executing
%\begin{example}
% \savekeys[my]{familya}{keya}
% \setkeys[my]{familya}{keyb=\usevalue{keya},keya=red}
%\end{example}
%since |keya| has been specified by the user. At best, |keyb| will
%copy a probably wrong value of |keya|. In the case that no value for
%|keya| has been saved before, we get an error. We observe that the
%order of keys in the simplified |\setkeys| command is wrong. This
%example shows that the |keyb=\usevalue{keya}| should have been in the tail
%keys.\\
%
%The following example shows what can go wrong when using presets
%incorrectly and when |\setkeys| contains pointers.
%\begin{example}
% \savekeys[my]{familya}{keya}
% \presetkeys[my]{familya}{}{keya=red}
% \setkeys[my]{familya}{keyb=\usevalue{keya}}
%\end{example}
%This will come down to executing the following.
%\begin{example}
% \savekeys[my]{familya}{keya}
% \setkeys[my]{familya}{keyb=\usevalue{keya},keya=red}
%\end{example}
%This results in exactly the same situation as we have seen in the
%previous example and hence the same conclusion holds. In this case,
%we conclude that the |keya=red| argument should have been specified
%in the head keys of the |\presetkeys| command.\\
%
%For most applications, one could use the rule of thumb that preset
%keys containing pointers should go in the tail keys. All other keys
%should go in head keys. There might, however, be applications
%thinkable in which one would like to implement the preset system as
%shown in the two examples above, for instance to easily retrieve
%values used in the last use of a macro or environment. However, make
%sure that keys in that case receive an initialization in order to
%avoid errors of missing values.\\
%
%For completeness, the working example is below.
%\begin{example}
% \savekeys[my]{familya}{keya}
% \presetkeys[my]{familya}{keya=blue}{keyb=\usevalue{keya}}
% \setkeys[my]{familya}{keya=red}
% \presetkeys[my]{familya}{keya=red}{}
% \setkeys[my]{familya}{keyb=\usevalue{keya}}
%\end{example}
%Other examples can be found in the example files. See section~\ref{sec:sande}.
%
%\section{Category codes}\label{sec:catcodes}
%Some packages change the catcode of the equality sign and the comma.
%This is a problem for \pf{keyval} as it then does not recognize
%these characters anymore and cannot parse the input. This problem
%can play up on the background. Consider for instance the following
%example and note that the \pf{graphicx} package is using \pf{keyval}
%and that Turkish \pf{babel} will activate the equality sign for
%shorthand notation.
%\begin{example}
% \documentclass{article}
% \usepackage{graphicx}
% \usepackage[turkish]{babel}
% \begin{document}
% \includegraphics[scale=.5]{rose.eps}
% \end{document}
%\end{example}
%The \pf{babel} package provides syntax to temporarily reset the
%catcode of the equality sign and switch shorthand back on after
%using \pf{keyval} (in the |\includegraphics| command), namely
%|\shorthandoff{=}| and |\shorthandon{=}|. But having to do this
%every time \pf{keyval} is invoked is quite cumbersome. Besides that,
%it might not always be clear that the \pf{keyval} package is used
%inside a command.\\
%
%For these reasons, \pf{xkeyval} performs several actions with user
%input before trying to parse it. First of all, it performs a check
%whether the characters |=| and |,| appear in the input with
%unexpected catcodes. If so, the |\@selective@sanitize| macro is used
%to sanitize these characters only in the top level. This means that
%characters inside (a) group(s), |{ }|, will not be sanitized. For
%instance, when using Turkish \pf{babel}, it is possible to use |=|
%shorthand notation even in the value of a key, as long as this value
%is inside a group.
%\begin{example}
% \documentclass{article}
% \usepackage{graphicx}
% \usepackage[turkish]{babel}
% \usepackage{xkeyval}
% \makeatletter
% \define@key{fam}{key}{#1}
% \begin{document}
% \includegraphics[scale=.5]{rose.eps}
% \setkeys{fam}{key={some =text}}
% \end{document}
%\end{example}
%In the example above, the |\includegraphics| command does work.
%Further, the first equality sign in the |\setkeys| command will be
%sanitized, but the second one will be left untouched and will be
%typeset as \pf{babel} shorthand notation.\\
%
%The commands |\savekeys| and |\disable@keys| are protected against
%catcode changes of the comma. The commands |\setkeys| and
%|\presetkeys| are protected against catcode changes of the comma and
%the equality sign. Note that \LaTeX\ option macros (see
%section~\ref{sec:options}) are not protected as \LaTeX\ does not
%protect them either.
%
%\section{\pf{xkeyval} and \LaTeX}
%If \pf{xkeyval} is loaded by |\RequirePackage| or |\usepackage|, the
%package will performs two action immediately. These require
%\pf{xkeyval} to be loaded at any point after |\documentclass| or by
%the document class itself.\\
%
%First, it retrieves the document class and stores that (including
%the class extension) into the following macro.
%\begin{command}
% `\ci{XKV@documentclass}'
%\end{command}
%\SpecialUsageIndex{\XKV@documentclass}
%This macro could, for instance, contain |article.cls| and can be useful
%when using |\ProcessOptionsX*| in a class. See
%page~\pageref{macro:poxstar}.\\
%
%Secondly, the global options stored in |\@classoptionslist| by \LaTeX\
%are copied to the following macro.
%\begin{command}
% `\ci{XKV@classoptionslist}'
%\end{command}
%\SpecialUsageIndex{\XKV@classoptionslist}
%This macro will be used by |\ProcessOptionsX|. Options containing
%an equality sign are deleted from the original list in
%|\@classoptionslist| to avoid packages, which do not use \pf{xkeyval}
%and which are loaded later, running into problems when trying to copy
%global options using \LaTeX's |\ProcessOptions|.\\
%
%\subsection{Declaring and setting class or package options}
%\label{sec:options}
%\changes{v1.4}{2004/08/24}{Added prefix options to macros}
%The macros in this section can be used to build \LaTeX\ class or
%package options systems using \pf{xkeyval}. These are comparable to
%the standard \LaTeX\ macros without the trailing |X|. See for more
%information about these \LaTeX\ macros the documentation of the
%source \cite{LaTeXbase} or a \LaTeX\ manual (for instance, the
%\LaTeX\ Companion \cite{companion}). The macros in this section have
%been built using |\define@key| and |\setkeys| and are not
%available to \TeX\ users.\\
%
%The macros below allow for specifying the \meta{family} (or
%\meta{families}) as an optional
%argument. This could be useful if you want to define global options
%which can be reused later (and set locally by the user) in a macro or
%environment that you define. If no \meta{family} (or
%\meta{families}) is specified, the macro will insert the default
%family name which is the filename of the file that is calling the
%macros. The macros in this section also allow for setting an
%optional prefix. When using the filename as family, uniqueness of
%key macros is already guaranteed. In that case, you can omit the
%optional \meta{prefix}. However, when you use a custom prefix for
%other keys in your package and you want to be able to set all of
%the keys later with a single command, you can use the custom prefix
%also for the class or package options system.\\
%
%Note that both \oarg{arg} and \larg{arg} denote optional arguments
%to the macros in this section. This syntax is used to identify the
%different optional arguments when they appear next to each other.\\
%
%\begin{command}
% `\ci{DeclareOptionX}\oarg{prefix}\larg{family}\marg{key}\oarg{default}\marg{function}'
%\end{command}
%\SpecialUsageIndex{\DeclareOptionX}
%Declares an option (i.e., a key, which can also be used later on in
%the package in |\setkeys| and friends). This macro is comparable to the
%standard \LaTeX\ macro |\DeclareOption|, but with this command, the
%user can pass a value to the option as well. Reading that value can
%be done by using |#1| in \meta{function}. This will contain
%\meta{default} when no value has been specified for the key. The
%value of the optional argument \meta{default} is empty by default.
%This implies that when the user does not assign a value to
%\meta{key} and when no default value has been defined, no error will
%be produced. The optional argument \meta{family} can be used to
%specify a custom family for the key. When the argument is not used,
%the macro will insert the default family name.
%\begin{example}
% \newif\iflandscape
% \DeclareOptionX{landscape}{\landscapetrue}
% \DeclareOptionX{parindent}[20pt]{\setlength\parindent{#1}}
%\end{example}
%Assuming that the file containing the example above
%is called |myclass.cls|, the example is equivalent to
%\begin{example}
% \newif\iflandscape
% \define@key{myclass.cls}{landscape}[]{\landscapetrue}
% \define@key{myclass.cls}{parindent}[20pt]{\setlength\parindent{#1}}
%\end{example}
%Notice that an empty default value has been inserted by \pf{xkeyval}
%for the |landscape| option. This allows for the usual \LaTeX\ options
%use like
%\begin{example}
% \documentclass[landscape]{myclass}
%\end{example}
%without raising \texttt{No value specified for key `landscape'} errors.\\
%
%\begin{command}
% `\ci{DeclareOptionX*}\marg{function}'
%\end{command}
%\SpecialUsageIndex{\DeclareOptionX*}
%This macro can be used to process any unknown inputs. It is
%comparable to the \LaTeX\ macro |\DeclareOption*|. Use
%|\CurrentOption| within this macro to get the entire input from
%which the key is unknown, for instance |unknownkey=value| or
%|somevalue|. These values (possibly including a key) could for
%example be passed on to another class or package or could be used as
%an extra class or package option specifying for instance a style
%that should be loaded.
%\begin{example}
% \DeclareOptionX*{\PackageWarning{mypackage}{`\CurrentOption' ignored}}
%\end{example}
%The example produces a warning when the user issues an option
%that has not been declared.\\
%
%\begin{command}
% `\ci{ExecuteOptionsX}\oarg{prefix}\larg{families}\oarg{na}\marg{keys}'
%\end{command}
%\SpecialUsageIndex{\ExecuteOptionsX}
%This macro sets keys created by |\DeclareOptionX| and is basically a
%copy of |\setkeys|. The optional argument \meta{na} specifies keys
%that should be ignored. The optional argument \meta{families} can be
%used to specify a list of families which define \meta{keys}. When
%the argument is not used, the macro will insert the default family
%name. This macro will not use the declaration done by |\DeclareOptionX*|
%when undeclared options appear in its argument. Instead, in this case
%the macro will raise an error. This mimics \LaTeX's |\ExecuteOptions|'
%behavior.
%\begin{example}
% \ExecuteOptionsX{parindent=0pt}
%\end{example}
%This initializes |\parindent| to |0pt|.\\
%
%\begin{command}
% `\ci{ProcessOptionsX}\oarg{prefix}\larg{families}\oarg{na}'
%\end{command}
%\SpecialUsageIndex{\ProcessOptionsX}
%This macro processes the keys and values passed by the user to the
%class or package. The optional argument \meta{na} can be used to
%specify keys that should be ignored. The optional argument
%\meta{families} can be used to specify the families that have been
%used to define the keys. Note that this macro will not protect
%macros in the user inputs (like |\thepage|) as explained in
%section~\ref{sec:macrooptions}. When used in a class file, this macro
%will ignore unknown keys or options. This allows
%the user to use global options in the |\documentclass| command which
%can be copied by packages loaded afterwards.\\
%
%\begin{command}
% `\ci{ProcessOptionsX*}\oarg{prefix}\larg{families}\oarg{na}'
%\end{command}
%\label{macro:poxstar}
%\SpecialUsageIndex{\ProcessOptionsX*}
%The starred version works like |\ProcessOptionsX| except that it also
%copies user input from the |\documentclass| command. When the user
%specifies an option in the document class which also exists in the
%local family (or families) of the package issuing
%|\ProcessOptionsX*|, the local key will be set as well. In this
%case, |#1| in the |\DeclareOptionX| macro will contain the value
%entered in the |\documentclass| command for this key. First the
%global options from |\documentclass| will set local keys and
%afterwards, the local options, specified with |\usepackage|,
%|\RequirePackage| or |\LoadClass| (or friends), will set local keys,
%which could overwrite the global options again, depending on the way
%the options sections are constructed. This macro reduces to
%|\ProcessOptionsX| only when issued from the class which forms the
%document class for the file at hand to avoid setting the same
%options twice, but not for classes loaded later using for instance
%|\LoadClass|. Global options that do not have a counterpart in local
%families of a package or class will be skipped.\\
%
%It should be noted that these implementations differ from the
%\LaTeX\ implementations of |\ProcessOptions| and |\ProcessOptions*|.
%The difference is in copying the global options. The \LaTeX\
%commands always copy global options if possible. As a package author
%doesn't know beforehand which document class will be used and with
%which options, the options declared by the author might show some
%unwanted interactions with the global options. When the class and
%the package share the same option, specifying this option in the
%|\documentclass| command will force the package to use that option
%as well. With |\ProcessOptionsX|, \pf{xkeyval} offers a package
%author to become fully independent of the global options and be sure
%to avoid conflicts with any class.\\
%
%The use of |\ProcessOptionsX*| in a class file might be tricky since
%the class could also be used as a basis for another package or class
%using |\LoadClass|. In that case, depending on the options system of
%the document class, the behavior of the class loaded with
%|\LoadClass| could change compared to the situation when it is
%loaded by |\documentclass|. But since it is technically possible to
%create two classes that cooperate, the \pf{xkeyval} package allows
%for the usage of |\ProcessOptionsX*| in class files. Notice that
%using \LaTeX's |\ProcessOptions| or |\ProcessOptions*|, a class file
%cannot copy document class options.\\
%
%In case you want to verify whether your class is loaded with
%|\documentclass| or |\LoadClass|, you can use the |\XKV@documentclass|
%macro which contains the current document class.
%
%\subsection{Options with macros}\label{sec:macrooptions}
%The package and class option system of \LaTeX\ contained in the
%kernel performs some expansions while processing options. This
%prevents doing for instance
%\begin{example}
% \documentclass[title=My title,author=\textsc{Me}]{myclass}
%\end{example}
%given that |myclass| uses \pf{xkeyval} and defines the options |title|
%and |author|.\\
%
%This problem can be overcome by redefining certain kernel commands.
%If you want to offer the user this functionality for the
%|\documentclass| command, the user will have to do
%|\RequirePackage{xkvltxp}| on the first line of the \LaTeX\ file. If
%you plan to use this functionality in a package, the user can use
%the package in the ordinary way with |\usepackage{xkvltxp}|. This
%package then has to be loaded before loading the package which will
%use this functionality. A description of the patch can be found in
%the source code documentation.\\
%
%The examples below summarize this information. The first example
%shows the case in which we want to allow for macros in the
%|\documentclass| command.
%\begin{example}
% \RequirePackage{xkvltxp}
% \documentclass[title=My title,author=\textsc{Me}]{myclass}
% \begin{document}
% \end{document}
%\end{example}\bigskip
%
%The second example shows the case in which we want to allow for
%macros in a |\usepackage| command.
%\begin{example}
% \documentclass{article}
% \usepackage{xkvltxp}
% \usepackage[footer=page~\thepage.]{mypack}
% \begin{document}
% \end{document}
%\end{example}\bigskip
%
%Any package or class using \pf{xkeyval} and \pf{xkvltxp} to process
%options can take options that contain macros in their value without
%expanding them prematurely. However, you can of course not use
%macros in options which are not of the |key=value| form since they
%might in the end be passed on to or copied by a package which is not
%using \pf{xkeyval} to process options, which will then produce
%errors. Options of the |key=value| form will be deleted from
%|\@classoptionslist| (see section~\ref{sec:options}) and form no
%threat for packages loaded later on. Finally, make sure not to pass
%options of the |key=value| form to packages not using \pf{xkeyval}
%to process options since they cannot process them. For examples see
%section~\ref{sec:sande}.
%
%\section{List of macro structures}\label{sec:macrostructs}
%This section provides a list of all reserved internal macros used
%for key processing. Here |pre| denotes a prefix, |fam| denotes a
%family and |key| denotes a key. These vary per application. The
%other parts in internal macro names are constant. The macros with
%additional |XKV| prefix are protected in the sense that all \pf{xkeyval}
%macros disallow the use of the |XKV| prefix. Package authors
%using \pf{xkeyval} are responsible for protecting the other types of
%internal macros.
%
%\begin{description}
%\item[\ci{pre@fam@key}]\mbox{}\\
%Key macro. This macro takes one argument. This macro will execute
%the \meta{function} of |\define@key| (and friends) on the value
%submitted through |\setkeys|.
%\item[\ci{ifpre@fam@key}, \ci{pre@fam@keytrue}, \ci{pre@fam@keyfalse}]\mbox{}\\
%The conditional created by |\define@boolkey| with parameters |pre|,
%|fam| and |key|. The |true| and |false| macros are used to set the
%conditional to |\iftrue| and |\iffalse| respectively.
%\item[\ci{pre@fam@key@cmd}]\mbox{}\\
%The macro to which input to |\setkeys| for |key| will be stored if
%this key has been defined by |\define@cmdkey|.
%\item[\ci{pre@fam@key@default}]\mbox{}\\
%Default value macro. This macro expands to |\pre@fam@key{default value}|.
%This macro is defined through |\define@key| (and friends).
%\item[\ci{XKV@pre@fam@key@value}]\mbox{}\\
%This macro is used to store the value that has been submitted
%through |\setkeys| to the key macro (without replacing pointers).
%\item[\ci{XKV@pre@fam@save}]\mbox{}\\
%Contains the names of the keys that should always be saved when they
%appear in a |\setkeys| command. This macro is defined by
%|\savekeys|.
%\item[\ci{XKV@pre@fam@preseth}]\mbox{}\\
%Contains the head presets. These will be submitted to |\setkeys|
%before setting user input. Defined by |\presetkeys|.
%\item[\ci{XKV@pre@fam@presett}]\mbox{}\\
%Contains the tail presets. These will be submitted to |\setkeys|
%after setting user input. Defined by |\presetkeys|.
%\end{description}
%
%An important remark should be made. Most of the macros listed above
%will be constructed by \pf{xkeyval} internally using
%|\csname...\endcsname|. Hence almost any input to the macros defined
%by this package is possible. However, some internal macros are
%defined to be used outside \pf{xkeyval} macros as well. These are
%the macros |\ifpre@fam@key| and |\pre@fam@key@cmd|. To be able to use
%these macros yourself, none of the input parameters should contain a
%non-letter characters. If you feel that this is somehow necessary
%anyway, there are several strategies to make things work.\\
%
%Let us consider as example the following situation (notice the hyphen
%|-| in the family name).
%\begin{example}
% \define@boolkey{some-fam}{myif}
% \define@cmdkey{some-fam}{mycmd}
% \setkeys{some-fam}{myif=false,mycmd=save this}
%\end{example}
%Using these keys in a |\setkeys| command is not a problem at all.
%However, if you want to use the |\ifKV@some-fam@myif| command itself,
%you can do either
%\begin{example}
% \edef\savedhyphencatcode{\the\catcode`\-}%
% \catcode`\-=11\relax
% \def\mymacro{%
%   \ifKV@some-fam@myif
%     % true case
%   \else
%     % false case
%   \fi}
% \catcode`\-=\savedhyphencatcode
%\end{example}
%or
%\begin{example}
% \def\mymacro{%
%   \csname ifKV@some-fam@myif\endcsname
%     % true case
%   \else
%     % false case
%   \fi}
%\end{example}
%
%\section{Warnings and errors}\label{sec:errors}
%\changes{v1.4}{2004/08/24}{Changed package options}
%There are several points where \pf{xkeyval} performs a check and
%could produce a warning or an error. All possible warnings or and
%error messages are listed below with an explanation. Here |pre|
%denotes a prefix, |name| denotes the name of a key, |fam| denotes a
%family, |fams| denotes a list of families and |val| denotes some
%value. These vary per application. Note that messages 1 to 7 could
%result from erroneous key setting through |\setkeys|, |\setrmkeys|,
%|\ExecuteOptionsX| and |\ProcessOptionsX|.
%
%\begin{description}
%\item[\normalfont 1)\, \texttt{boolean can only be `true' or `false'}]\hfill(error)\\
%A value other than |true| or |false| has been submitted to a boolean
%key.
%\item[\normalfont 2)\, \texttt{`name' undefined in families `fams'}]\hfill(error)\\
%The key |name| is not defined in the families in |fams|. Probably
%you mistyped |name|.
%\item[\normalfont 3)\, \texttt{no key specified for value `val'}]\hfill(error)\\
%\pf{xkeyval} found a value without a key, for instance something
%like |=value|, when setting keys.
%\item[\normalfont 4)\, \texttt{no value recorded for key `name'}]\hfill(error)\\
%You have used a pointer to a key for which no value has been saved
%previously.
%\item[\normalfont 5)\, \texttt{back linking pointers; pointer replacement canceled}]\hfill(error)\\
%You were back linking pointers. Further pointer replacements are canceled
%to avoid getting into an infinite loop. See section~\ref{sec:usevalue}.
%\item[\normalfont 6)\, \texttt{no value specified for key `name'}]\hfill(error)\\
%You have used the key `name' without specifying any value for it
%(namely, |\setkeys{fam}{name}| and the key does not have a default
%value. Notice that |\setkeys{fam}{name=}| submits the empty value to
%the key macro and that this is considered a legal value.
%\item[\normalfont 7)\, \label{mess:2}\texttt{key `name' has been disabled}]\hfill(warning)\\
%The key that you try to set has been disabled and cannot be used anymore.
%\item[\normalfont 8)\, \texttt{`XKV' prefix is not allowed}]\hfill(error)\\
%You were trying to use the |XKV| prefix when defining or setting
%keys. This error can be caused by any \pf{xkeyval} macro having an
%optional prefix argument.
%\item[\normalfont 9)\, \texttt{key `name' undefined}]\hfill(error)\\
%This error message is caused by trying to disable a key that does not
%exist. See section~\ref{sec:disable}.
%\item[\normalfont 10)\, \texttt{no save keys defined for `pre@fam@'}]\hfill(error)\\
%You are trying to delete or undefine save keys that have not been defined yet.
%See section~\ref{sec:savevalue}.
%\item[\normalfont 11)\, \texttt{no presets defined for `pre@fam@'}]\hfill(error)\\
%You are trying to delete or undefine presets that have not been defined yet.
%See section~\ref{sec:preset}.
%\item[\normalfont 12)\, \texttt{xkeyval loaded before \ci{documentclass}}]\hfill(error)\\
%Load \pf{xkeyval} after |\documentclass| (or in the class that is
%the document class). See section~\ref{sec:options}.
%\end{description}
%
%\section{Known issues}\label{sec:issues}
%This package redefines \pf{keyval}'s |\define@key| and |\setkeys|.
%This is risky in general. However, since \pf{xkeyval} extends the
%possibilities of these commands while still allowing for the
%\pf{keyval} syntax and use, there should be no problems for packages
%using these commands after loading \pf{xkeyval}. The package
%prevents \pf{keyval} to be loaded afterwards to avoid these commands
%from being redefined again into the simpler versions. For packages
%using internals of \pf{keyval}, like |\KV@@sp@def|, |\KV@do| and
%|\KV@errx|, these are provided separately in |keyval.tex|.\\
%
%The advantage of redefining these commands instead of making new
%commands is that it is much easier for package authors to start
%using \pf{xkeyval} instead of \pf{keyval}. Further, it eliminates
%the confusion of having multiple commands doing similar things.\\
%
%A potential problem lies in other packages that redefine either
%|\define@key| or |\setkeys| or both. Hence particular care has been
%spend to check packages for this. Only one package has been found to
%do this, namely \pf{pst-key}. This package implements a custom
%version of |\setkeys| which is specialized to set
%\pf{PSTricks} \cite{PSTricksWeb,PSTricks} keys of the form
%|\psset@somekey|. \pf{xkeyval} also provides the means to set these
%kind of keys (see page~\pageref{page:pstricks}) and work is going on
%to convert \pf{PSTricks} packages to be using a specialization of
%\pf{xkeyval} instead of \pf{pst-key}. This specialization is
%available in the \pf{pst-xkey} package \cite{pst-xkey}, which is
%distributed with the \pf{xkeyval} package. However, since a lot of
%authors are involved and since it requires a change of policy, the
%conversion of \pf{PSTricks} packages might take some time. Hence, at
%the moment of writing, \pf{xkeyval} will conflict with \pf{pst-key}
%and the \pf{PSTricks} packages using \pf{pst-key}, which are
%\pf{pst-circ}, \pf{pst-eucl}, \pf{pst-fr3d}, \pf{pst-geo},
%\pf{pst-gr3d}, \pf{pst-labo}, \pf{pst-lens}, \pf{pst-ob3d},
%\pf{pst-optic}, \pf{pst-osci}, \pf{pst-poly}, \pf{pst-stru},
%\pf{pst-uml} and \pf{pst-vue3d}.\\
%
%Have a look at the \pf{PSTricks} website \cite{PSTricksWeb} to find
%out if the package that you want to use has been converted already.
%If not, load an already converted package (like \pf{pstricks-add})
%after loading the old package to make them work.
%
%\section{Source and examples}\label{sec:sande}
%To generate the source code documentation, find the source of this
%package, |xkeyval.dtx| in your local \TeX\ installation or on CTAN
%and perform the following steps.
%\begin{example}
% latex xkeyval.dtx
% latex xkeyval.dtx
% bibtex xkeyval
% makeindex -s gglo.ist -o xkeyval.gls xkeyval.glo
% makeindex -s gind.ist -o xkeyval.ind xkeyval.idx
% latex xkeyval.dtx
% latex xkeyval.dtx
%\end{example}\bigskip
%
%If you only want to produce the package and example files from the
%source, then the first step is sufficient. This step will generate
%the package files (|xkeyval.tex|, |xkeyval.sty|, |xkvltxp.sty|,
%|keyval.tex| and |xkvtxhdr.tex|) and the example files.\\
%
%The file |xkvex1.tex| provides an example for \TeX\ users for the
%macros described in sections~\ref{sec:define}, \ref{sec:setkeys},
%\ref{sec:pointers} and \ref{sec:preset}. The file |xkvex2.tex|
%provides an example for \LaTeX\ users for the same macros. The files
%|xkvex3.tex|, |xkveca.cls|, |xkvecb.cls|, |xkvesa.sty|, |xkvesb.sty|
%and |xkvesc.sty| together form an example for the macros described
%in section~\ref{sec:options}. The set of files consisting of
%|xkvex4.tex|, |xkveca.cls|, |xkvecb.cls|, |xkvesa.sty|, |xkvesb.sty|
%and |xkvesc.sty| provides an example for
%section~\ref{sec:macrooptions}. These files also demonstrate the
%possibilities of interaction between packages or classes not using
%\pf{xkeyval} and packages or classes that do use \pf{xkeyval} to set
%options.
%
%\StopEventually{}
%
% \section{Implementation}\label{sec:imple}
% \subsection{\TeX\ program}
% Avoid loading |xkeyval.tex| twice.
%    \begin{macrocode}
%<*tex>
\csname XKeyValLoaded\endcsname
\let\XKeyValLoaded\endinput
%    \end{macrocode}
% Adjust some catcodes to define internal macros.
%    \begin{macrocode}
\edef\XKVcatcodes{%
  \catcode`\noexpand\@\the\catcode`\@\relax
  \catcode`\noexpand\=\the\catcode`\=\relax
  \catcode`\noexpand\,\the\catcode`\,\relax
  \catcode`\noexpand\:\the\catcode`\:\relax
  \let\noexpand\XKVcatcodes\relax
}
\catcode`\@11\relax
\catcode`\=12\relax
\catcode`\,12\relax
\catcode`\:12\relax
%    \end{macrocode}
% Initializations. This package uses a private token to avoid
% conflicts with other packages that use \LaTeX\ scratch token registers
% in key macro definitions (for instance, \pf{graphicx}, keys angle
% and scale).
%    \begin{macrocode}
\newtoks\XKV@toks
\newif\ifXKV@st
\newif\ifXKV@sg
\newif\ifXKV@pl
\newif\ifXKV@knf
\newif\ifXKV@rkv
\newif\ifXKV@inpox
\let\XKV@rm\@empty
%    \end{macrocode}
% Load \LaTeX\ primitives if necessary and provide information.
%    \begin{macrocode}
\ifx\ProvidesFile\@undefined
  \message{2005/01/30 v2.0 key=value parser (HA)}
  \input xkvtxhdr.tex
\else
  \ProvidesFile{xkeyval.tex}[2005/01/30 v2.0 key=value parser (HA)]
  \@addtofilelist{xkeyval.tex}
\fi
%    \end{macrocode}
% \begin{macro}{\@firstoftwo}
% \begin{macro}{\@secondoftwo}
% Two utility macros from the |latex.ltx| needed for executing
% |\XKV@ifundefined| in the sequel.
%    \begin{macrocode}
\long\def\@firstoftwo#1#2{#1}
\long\def\@secondoftwo#1#2{#2}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@afterfi}
% \begin{macro}{\XKV@afterelsefi}
% Two utility macros to move content of a conditional branch after
% the |\fi|. This avoids nesting conditional structures too deep.
%    \begin{macrocode}
\long\def\XKV@afterfi#1\fi{\fi#1}
\long\def\XKV@afterelsefi#1\else#2\fi{\fi#1}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@ifundefined}
% \marg{csname}\marg{undefined}\marg{defined}\par
% \changes{v1.8d}{2005/01/02}{Simplified definition by using \ci{XKV@afterelsefi}}
% \changes{v2.0}{2005/01/30}{Made none \eTeX\ version not leave \ci{relax}}
% Executes \meta{undefined} if the control sequence with name \meta{csname}
% is undefined, else it executes \meta{defined}. This macro uses
% \eTeX\ if possible to avoid filling \TeX's hash when checking
% control sequences like key macros in the rest of the package.
% |\XKV@afterelsefi| is necessary here to avoid \TeX\ picking up the
% second |\fi| as end of the main conditional when |\ifcsname| is
% undefined. For |\XKV@afterelsefi| this |\fi| is hidden in the
% group used to define |\XKV@ifundefined| in branch of the case that
% |\ifcsname| is defined. Notice the following. Both versions of the
% macro leave the tested control sequence undefined. However, the
% first version will execute \meta{undefined} if the control sequence
% is undefined or |\relax|, whereas the second version will only
% execute \meta{undefined} if the control sequence is undefined. This
% is no problem for the applications in this package.
%    \begin{macrocode}
\ifx\ifcsname\@undefined\XKV@afterelsefi
  \def\XKV@ifundefined#1{%
    \begingroup\expandafter\expandafter\expandafter\endgroup
      \expandafter\ifx\csname#1\endcsname\relax
      \expandafter\@firstoftwo
    \else
      \expandafter\@secondoftwo
    \fi
  }
\else
  \def\XKV@ifundefined#1{%
    \ifcsname#1\endcsname
      \expandafter\@secondoftwo
    \else
      \expandafter\@firstoftwo
    \fi
  }
\fi
%    \end{macrocode}
% \end{macro}
% Check whether \pf{keyval} has been loaded and if not, load
% \pf{keyval} primitives and prevent \pf{keyval} from being loaded
% after \pf{xkeyval}.
%    \begin{macrocode}
\XKV@ifundefined{ver@keyval.sty}{
  \input keyval.tex
  \expandafter\def\csname ver@keyval.sty\endcsname{1999/03/16}
}{}
%    \end{macrocode}
% \begin{macro}{\@ifnextcharacter}
% \changes{v1.4}{2004/08/24}{Added robust next character check}
% \begin{macro}{\@ifncharacter}
% Check the next character independently of its catcode. This will be
% used to safely perform |\@ifnextcharacter+| and
% |\@ifnextcharacter*|. This avoids errors in case any other package
% changes the catcode of these characters.\par Contributed by Donald
% Arseneau.
%    \begin{macrocode}
\long\def\@ifnextcharacter#1#2#3{%
  \@ifnextchar\bgroup
  {\@ifnextchar{#1}{#2}{#3}}%
  {\@ifncharacter{#1}{#2}{#3}}%
}
\long\def\@ifncharacter#1#2#3#4{%
  \if\string#1\string#4%
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {#2}{#3}#4%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@whilist}
% \meta{cmd}|:=|\meta{list}|\do|\meta{if}|\fi|\marg{function}\par
% \changes{v1.5}{2004/09/27}{Changed behavior}
% Based on |\XKV@for|. Execution of \meta{function} stops when either
% the list has ran out of elements or \meta{if} is not true anymore.
% When using |\iftrue| for \meta{if}, the execution of the macro is the
% same as that of |\XKV@for|, but contains an additional check and is
% hence less efficient than |\XKV@for| in that situation.
%    \begin{macrocode}
\long\def\XKV@whilist#1:=#2\do#3\fi#4{%
%    \end{macrocode}
% Check whether the condition is true and start iteration.
%    \begin{macrocode}
  #3\expandafter\XKV@wh@list#2,\@nil,\@nil\@@#1#3\fi{#4}\fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@wh@list}
% \changes{v1.6}{2004/10/05}{Added missing \%}
% \changes{v1.9}{2005/01/16}{Made running command contain last used list entry at exit}
% Performs iteration and checks extra condition. This macro is not
% optimized for the case that the list contains a single element.
%    \begin{macrocode}
\long\def\XKV@wh@list#1,#2\@@#3#4\fi#5{%
%    \end{macrocode}
% Define the running \meta{cmd} in a group to keep it local in case
% we have to exit the loop.
%    \begin{macrocode}
  \begingroup\def#3{#1}\expandafter\endgroup
%    \end{macrocode}
% If we find the end of the list, stop.
%    \begin{macrocode}
  \ifx#3\@nnil
    \expandafter\XKV@wh@l@st
  \else
%    \end{macrocode}
% If the condition is met, define the running \meta{cmd}, execute
% \meta{function} and continue. Otherwise stop.
%    \begin{macrocode}
    #4%
      \def#3{#1}#5\expandafter\expandafter\expandafter\XKV@wh@list
    \else
      \expandafter\expandafter\expandafter\XKV@wh@l@st
    \fi
  \fi
  #2\@@#3#4\fi{#5}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@wh@l@st}
% Macro to gobble remaining input.
%    \begin{macrocode}
\long\def\XKV@wh@l@st#1\@@#2#3\fi#4{}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@for}
% \meta{cmd}|:=|\meta{list}|\do|\marg{function}\par
% \changes{v1.5}{2004/09/27}{Added}
% Based on |\@for|, but also starts execution of \meta{function} when
% \meta{list} is empty. This is done to support packages that use the `empty
% family', like \pf{PSTricks}. The macro executes \meta{function} while
% \meta{if} is valid. At every iteration, the first element will be
% taken from \meta{list} and \meta{cmd} will be defined to expand to
% this element. Execution stops when the list has ran out of elements.
%    \begin{macrocode}
\long\def\XKV@for#1:=#2\do#3{\expandafter\@forloop#2,\@nil,\@nil\@@#1{#3}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@addtomacro@n}
% \marg{macro}\marg{content}\par
% Adds \meta{content} to \meta{macro} without expanding it.
%    \begin{macrocode}
\def\XKV@addtomacro@n#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@addtomacro@o}
% \marg{macro}\marg{content}\par
% Adds \meta{content} to \meta{macro} after expanding the first token
% of \meta{content} once.
%    \begin{macrocode}
\def\XKV@addtomacro@o#1#2{%
  \expandafter\expandafter\expandafter\def\expandafter\expandafter
    \expandafter#1\expandafter\expandafter\expandafter{\expandafter#1#2}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@addtolist@n}
% \marg{cmd}\marg{token}\par
% \changes{v1.8 }{2004/12/13}{Changed to respect groups}
% \changes{v2.0}{2005/01/30}{Simplified}
% Adds \meta{token} to the list in \meta{cmd} without expanding \meta{token}.
%    \begin{macrocode}
\def\XKV@addtolist@n#1#2{%
  \ifx#1\@empty
    \XKV@addtomacro@n#1{#2}%
  \else
    \XKV@addtomacro@n#1{,#2}%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@addtolist@o}
% \marg{cmd}\marg{token}\par
% \changes{v2.0}{2005/01/30}{Simplified}
% Adds \meta{token} to the list in \meta{cmd} after expanding
% \meta{token} once.
%    \begin{macrocode}
\def\XKV@addtolist@o#1#2{%
  \ifx#1\@empty
    \XKV@addtomacro@o#1#2%
  \else
    \XKV@addtomacro@o#1{\expandafter,#2}%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@addtolist@x}
% \marg{cmd}\marg{token}\par
% Adds \meta{token} to the list in \meta{cmd} after a full expansion
% of both \meta{cmd} and \meta{token}.
%    \begin{macrocode}
\def\XKV@addtolist@x#1#2{\edef#1{#1\ifx#1\@empty\else,\fi#2}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@selective@sanitize}
% \begin{macro}{\@s@lective@sanitize}
%   \oarg{level}\marg{character string}\marg{cmd}\\
% Converts selected characters, given by \meta{character string},
% within the first-level expansion of \meta{cmd} to category code~12,
% leaving all other tokens (including grouping braces) untouched.
% Thus, macros inside \meta{cmd} do not lose their function, as it is
% the case with |\@onelevel@sanitize|.
% The resulting token list is again saved in \meta{cmd}.\par
% Example: |\def\cs{ ^{\fi}~}| and |\@selective@sanitize{!^}\cs| will
% change the catcode of `|^|' to \emph{other}
% within |\cs|, while |\fi| and `|~|' will remain unchanged.
% As the example shows, unbalanced conditionals are allowed.\par
% Remarks: \meta{cmd} should not contain the control sequence |\bgroup|;
% however, |\csname bgroup\endcsname| and |\egroup| are possible.
% The optional \meta{level} command controls up to which nesting
% level sanitizing takes place inside groups; |0| will only sanitize
% characters in the top level, |1| will also sanitize within the first
% level of braces (but not in the second), etc.
% The default value is |10000|.
%    \begin{macrocode}
\def\@selective@sanitize{\@testopt\@s@lective@sanitize\@M}
\def\@s@lective@sanitize[#1]#2#3{%
  \begingroup
    \count@#1\relax\advance\count@\@ne
    \XKV@toks\expandafter{#3}%
    \def#3{#2}\@onelevel@sanitize#3%
    \edef#3{{#3}{\the\XKV@toks}}%
    \expandafter\@s@l@ctive@sanitize\expandafter#3#3
  \expandafter\endgroup\expandafter\def\expandafter#3\expandafter{#3}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\@s@l@ctive@sanitize}
%   \marg{cmd}\marg{sanitized character string}\marg{token list}\\
% \changes{v1.8 }{2004/12/13}{Bug fixed, added level control}
% Performs the main work.
% Here, the characters in \meta{sanitized character string} are already
% converted to catcode~12, \meta{token list} is the first-level expansion
% of the original contents of \meta{cmd}.
% The macro basically steps through the \meta{token list}, inspecting
% each single token to decide whether it has to be sanitized or passed
% to the result list.
% Special care has to be taken to detect spaces, grouping characters
% and conditionals (the latter may disturb other expressions).
% However, it is easier and more efficient to look for \TeX{}
% primitives in general --- which are characterized by a |\meaning|
% that starts with a backslash --- than to test whether a token equals
% specifically |\if|, |\else|, |\fi|, etc.
% Note that |\@s@l@ctive@sanitize| is being called recursively if
% \meta{token list} contains grouping braces.
%    \begin{macrocode}
\def\@s@l@ctive@sanitize#1#2#3{%
  \def\@i{\futurelet\@@tok\@ii}%
  \def\@ii{%
    \expandafter\@iii\meaning\@@tok\relax
    \ifx\@@tok\@s@l@ctive@sanitize
      \let\@@cmd\@gobble
    \else
      \ifx\@@tok\@sptoken
        \XKV@toks\expandafter{#1}\edef#1{\the\XKV@toks\space}%
        \def\@@cmd{\afterassignment\@i\let\@@tok= }%
      \else
        \let\@@cmd\@iv
      \fi
    \fi
    \@@cmd
  }%
  \def\@iii##1##2\relax{\if##1\@backslashchar\let\@@tok\relax\fi}%
  \def\@iv##1{%
    \toks@\expandafter{#1}\XKV@toks{##1}%
    \ifx\@@tok\bgroup
      \advance\count@\m@ne
      \ifnum\count@>\z@
        \begingroup
          \def#1{\expandafter\@s@l@ctive@sanitize
            \csname\string#1\endcsname{#2}}%
          \expandafter#1\expandafter{\the\XKV@toks}%
          \XKV@toks\expandafter\expandafter\expandafter
            {\csname\string#1\endcsname}%
          \edef#1{\noexpand\XKV@toks{\the\XKV@toks}}%
        \expandafter\endgroup#1%
      \fi
      \edef#1{\the\toks@{\the\XKV@toks}}%
      \advance\count@\@ne
      \let\@@cmd\@i
    \else
      \edef#1{\expandafter\string\the\XKV@toks}%
      \expandafter\in@\expandafter{#1}{#2}%
      \edef#1{\the\toks@\ifin@#1\else
              \ifx\@@tok\@sptoken\space\else\the\XKV@toks\fi\fi}%
      \edef\@@cmd{\noexpand\@i\ifx\@@tok\@sptoken\the\XKV@toks\fi}%
    \fi
    \@@cmd
  }%
  \let#1\@empty\@i#3\@s@l@ctive@sanitize
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@checksanitizea}
% Check whether the content |#1|, to be saved to macro |#2| contains
% the characters |=| or |,| with wrong catcodes. If so, sanitize them.
%    \begin{macrocode}
\def\XKV@checksanitizea#1#2{%
  \XKV@ch@cksanitize{#1}#2=%
  \ifin@\else\XKV@ch@cksanitize{#1}#2,\fi
  \ifin@\@selective@sanitize[0]{,=}#2\fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@checksanitizeb}
% Similar to |\XKV@checksanitizea|, but only checks commas.
%    \begin{macrocode}
\def\XKV@checksanitizeb#1#2{%
  \XKV@ch@cksanitize{#1}#2,%
  \ifin@\@selective@sanitize[0],#2\fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@ch@cksanitize}
% \marg{character string}\marg{token list}\marg{token}\par
% \changes{v1.8b}{2004/12/22}{Fixed bug for key value \ci{@empty}}
% This macro first check whether at least one \meta{token} is in \meta{character string}.
% If that is the case, it checks whether the character has catcode 12.
% Note that the macro will conclude that the character does not have
% catcode 12 when it is used inside a group |{}|, but that is not a
% problem, as we don't expect \meta{token} (namely |,| or |=|) inside
% a group, unless this group is in a value. But we won't worry about
% those characters anyway since the relevant key macro will have to
% process that.
%    \begin{macrocode}
\def\XKV@ch@cksanitize#1#2#3{%
  \def#2{#1}%
  \@onelevel@sanitize#2%
%    \end{macrocode}
% Check whether there is at least one |=| present.
%    \begin{macrocode}
  \@expandtwoargs\in@#3{#2}%
  \ifin@
%    \end{macrocode}
% If so, try to find it. If we can't find it, the character(s) has (or
% have) the wrong catcode. In that case sanitizing is necessary. This
% actually occurs, because the input was read by \TeX\
% before (and for instance stored in a macro or token register).
%    \begin{macrocode}
    \def#2##1#3##2\@nil{%
      \def#2{##2}%
      \ifx#2\@empty\else\in@false\fi
    }%
    #2#1#3\@nil
  \fi
  \def#2{#1}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@merge}
% \changes{v1.8 }{2004/12/13}{Simplified}
% This is a merging macro. For a given new item, the old
% items are scanned. If an old item key name matches with a new
% one, the new one will replace the old one. If not, the old one will
% be appended (and might be overwritten in a following loop). If, at
% the end of the old item loop the new item has not been used, it
% will be appended to the end of the list. This macro works irrespective
% of special syntax. The macro in argument |#3| is used to filter the
% key name from the syntax. All occurrences of a particulary key in
% the existing list will be overwritten by the new item. This macro
% is used to make |\savekeys| and |\presetkeys| incremental. The macro
% |#3| is |\XKV@getsg| and |\XKV@getkeyname| respectively.
%    \begin{macrocode}
\def\XKV@merge#1#2#3{%
  \XKV@checksanitizea{#2}\XKV@tempa
%    \end{macrocode}
% We have to do merging.
% Start the loop over the new presets. At every iteration, one new preset
% will be checked with old presets.
%    \begin{macrocode}
  \@for\XKV@tempa:=\XKV@tempa\do{%
    \XKV@pltrue
%    \end{macrocode}
% Retrieve the key name of the new preset at hand.
%    \begin{macrocode}
    #3\XKV@tempa\XKV@tempb
%    \end{macrocode}
% Store the (partially updated) old presets in a temp macro and empty
% the original macro.
%    \begin{macrocode}
    \let\XKV@tempc#1%
    \let#1\@empty
%    \end{macrocode}
% Start a loop over the old values.
%    \begin{macrocode}
    \@for\XKV@tempc:=\XKV@tempc\do{%
%    \end{macrocode}
% Retrieve the key name of the old key at hand.
%    \begin{macrocode}
      #3\XKV@tempc\XKV@tempd
      \ifx\XKV@tempb\XKV@tempd
%    \end{macrocode}
% If the key names are equal, append the new preset to the list
% and record that this key should not be added to the end of the
% presets list.
%    \begin{macrocode}
        \XKV@plfalse
        \XKV@addtolist@o#1\XKV@tempa
      \else
%    \end{macrocode}
% If the key names are not equal, then just append the current preset
% to the list.
%    \begin{macrocode}
        \XKV@addtolist@o#1\XKV@tempc
      \fi
    }%
%    \end{macrocode}
% If, after checking the old presets, no old preset has been overwritten
% then append the new preset to the end of the list.
%    \begin{macrocode}
    \ifXKV@pl\XKV@addtolist@o#1\XKV@tempa\fi
  }%
%    \end{macrocode}
% If requested, save the new list globally.
%    \begin{macrocode}
  \ifXKV@st\global\let#1#1\fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@delete}
% Delete entries by key name from a list of presets or save keys. |#1|
% is the macro currently holding the list to be updated. |#2| is the
% list of entries that should be removed and |#3| is the macro that
% should be used to retrieve the key name from an entry. For |\delpresetkeys|
% this is |\XKV@getkeyname| and for |\delsavekeys| it is |\XKV@getsg|.
%    \begin{macrocode}
\def\XKV@delete#1#2#3{%
%    \end{macrocode}
% Sanitize comma's.
%    \begin{macrocode}
  \XKV@checksanitizeb{#2}\XKV@tempa
%    \end{macrocode}
% Copy the current list and make the original empty.
%    \begin{macrocode}
  \let\XKV@tempb#1%
  \let#1\@empty
%    \end{macrocode}
% Run over the current list.
%    \begin{macrocode}
  \@for\XKV@tempb:=\XKV@tempb\do{%
%    \end{macrocode}
% Get the key name to identify the current entry.
%    \begin{macrocode}
    #3\XKV@tempb\XKV@tempc
%    \end{macrocode}
% If the current key name is in the list, do not add it anymore.
%    \begin{macrocode}
    \@expandtwoargs\in@{,\XKV@tempc,}{,\XKV@tempa,}%
    \ifin@\else\XKV@addtolist@o#1\XKV@tempb\fi
  }%
%    \end{macrocode}
% Save globally is necessary.
%    \begin{macrocode}
  \ifXKV@st\global\let#1#1\fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@warn}
% \begin{macro}{\XKV@err}
% \begin{macro}{\KV@err}
% \begin{macro}{\KV@errx}
% Warning and error macros. We redefine the \pf{keyval} error macros
% to use the \pf{xkeyval} ones. This avoids redefining them again when
% we redefine the |\XKV@warn| and |\XKV@err| macros in |xkeyval.sty|.
%    \begin{macrocode}
\def\XKV@warn#1{\message{xkeyval warning: #1}}
\def\XKV@err#1{\errmessage{xkeyval error: #1}}
\def\KV@errx{\XKV@err}
\let\KV@err\KV@errx
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@ifstar}
% \begin{macro}{\XKV@ifplus}
% Checks whether the following token is a |*| or |+|. Use
% |\XKV@ifnextchar| to perform the action safely and ignore catcodes.
%    \begin{macrocode}
\def\XKV@ifstar#1{\@ifnextcharacter*{\@firstoftwo{#1}}}
\def\XKV@ifplus#1{\@ifnextcharacter+{\@firstoftwo{#1}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@sp@deflist}
% \marg{cmd}\marg{token}\par
% \changes{v1.8a}{2004/12/20}{Modified to respect the empty family}
% Defines \meta{cmd} as \meta{token} after removing spaces surrounding
% elements of the list in \meta{token}. So, |keya, key b| becomes
% |keya,key b|. This is used to remove spaces from around elements in
% a list. Using |\zap@space| for this job, would also remove the
% spaces inside elements and hence changing key or family names with
% spaces. This method is slower, but does allow for spaces in key and
% family names, just as \pf{keyval} did. We need this algorithm at
% several places to be able to perform |\in@{,key,}{,...,}|, without
% having to worry about spaces in between commas and key names.
%    \begin{macrocode}
\def\XKV@sp@deflist#1#2{%
  \let#1\@empty
  \def\XKV@resa{#2}%
  \@for\XKV@resa:=\XKV@resa\do{%
    \expandafter\KV@@sp@def\expandafter\XKV@resa\expandafter{\XKV@resa}%
    \edef#1{#1,\XKV@resa}%
  }%
  \ifx#1\@empty\else
    \def\XKV@resa,##1\@nil{\def#1{##1}}%
    \expandafter\XKV@resa#1\@nil
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@makepf}
% \changes{v1.7}{2004/11/25}{Made macro protect internal macros}
% This macro creates the prefix, like |prefix@| in |\prefix@family@key|.
% First it deletes spaces from the input and checks wether it is empty.
% If not empty, an |@|-sign is added. The use of the |XKV| prefix is
% forbidden to protect internal macros and special macros like saved
% key values.
%    \begin{macrocode}
\def\XKV@makepf#1{%
  \KV@@sp@def\XKV@prefix{#1}%
  \def\XKV@resa{XKV}%
  \ifx\XKV@prefix\XKV@resa
    \XKV@err{`XKV' prefix is not allowed}%
    \let\XKV@prefix\@empty
  \else
    \ifx\XKV@prefix\@empty\else
      \edef\XKV@prefix{\XKV@prefix @}%
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@makehd}
% Creates the header, like |prefix@family@| in |\prefix@family@key|.
% If |family| is empty, the header reduces to |prefix@|.
%    \begin{macrocode}
\def\XKV@makehd#1{%
  \expandafter\KV@@sp@def\expandafter\XKV@header\expandafter{#1}%
  \ifx\XKV@header\@empty
    \let\XKV@header\XKV@prefix
  \else
    \edef\XKV@header{\XKV@prefix\XKV@header @}%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@testopta}
% \begin{macro}{\XKV@t@stopta}
% \begin{macro}{\XKV@t@st@pta}
% \begin{macro}{\XKV@@t@st@pta}
% Optional argument testing. Used for instance by |\setkeys|.
%    \begin{macrocode}
\def\XKV@testopta#1{%
  \XKV@ifstar{\XKV@sttrue\XKV@t@stopta#1}{\XKV@stfalse\XKV@t@stopta#1}%
}
\def\XKV@t@stopta#1{%
  \XKV@ifplus{\XKV@pltrue\XKV@t@st@pta#1}{\XKV@plfalse\XKV@t@st@pta#1}%
}
\def\XKV@t@st@pta#1{\@testopt{\XKV@@t@st@pta#1}{KV}}
\def\XKV@@t@st@pta#1[#2]#3{%
%    \end{macrocode}
% Set prefix.
%    \begin{macrocode}
  \XKV@makepf{#2}%
%    \end{macrocode}
% Store families and sanitize commas.
%    \begin{macrocode}
  \XKV@checksanitizeb{#3}\XKV@fams
  \expandafter\XKV@sp@deflist\expandafter\XKV@fams\expandafter{\XKV@fams}%
  \@testopt#1{}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@testoptb}
% \begin{macro}{\XKV@t@stoptb}
% Optional argument testing. Used for instance by |\define@key|.
%    \begin{macrocode}
\def\XKV@testoptb#1{\@testopt{\XKV@t@stoptb#1}{KV}}
\def\XKV@t@stoptb#1[#2]#3{%
%    \end{macrocode}
% Set prefix.
%    \begin{macrocode}
  \XKV@makepf{#2}%
%    \end{macrocode}
% Set header.
%    \begin{macrocode}
  \XKV@makehd{#3}%
%    \end{macrocode}
% Save family name for later use.
%    \begin{macrocode}
  \KV@@sp@def\XKV@tfam{#3}%
  #1%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@ifcmd}
% \begin{macro}{\XKV@@ifcmd}
% \marg{tokens}\marg{macro}\marg{cmd}\marg{yes}\marg{no}\par
% This macro checks whether the \meta{tokens} contains the macro
% specification \meta{macro}. If so, the argument of this macro
% will be saved to \meta{cmd} and \meta{yes} will be executed.
% Otherwise, the content of \meta{tokens} is saved to \meta{cmd}
% and \meta{no} is executed. This macro will, for instance, be used
% to distinguish |key| and |\global{key}|.
%    \begin{macrocode}
\def\XKV@ifcmd#1#2#3{%
  \def\XKV@@ifcmd##1#2##2##3\@nil##4{%
    \def##4{##2}\ifx##4\@nnil
      \def##4{##1}\expandafter\@secondoftwo
    \else
      \expandafter\@firstoftwo
    \fi
  }%
  \XKV@@ifcmd#1#2{\@nil}\@nil#3%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@getkeyname}
% \begin{macro}{\XKV@g@tkeyname}
% Utility macros to retrieve the key name from |key=value|,
% |\savevalue{key}=value| or |\gsavevalue{key}=value|. |\ifXKV@rkv|
% will record whether this particular value should be saved or not.
% |\ifXKV@sg| will record whether this value should be saved globally
% or not.
%    \begin{macrocode}
\def\XKV@getkeyname#1#2{\expandafter\XKV@g@tkeyname#1=\@nil#2}
\def\XKV@g@tkeyname#1=#2\@nil#3{%
  \XKV@ifcmd{#1}\savevalue#3{\XKV@rkvtrue\XKV@sgfalse}{%
    \XKV@ifcmd{#1}\gsavevalue#3%
      {\XKV@rkvtrue\XKV@sgtrue}{\XKV@rkvfalse\XKV@sgfalse}%
  }%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@getsg}
% Utility macro to check whether |key| or |\global{key}| has been
% specified in |\savekeys|.
%    \begin{macrocode}
\def\XKV@getsg#1#2{%
  \expandafter\XKV@ifcmd\expandafter{#1}\global#2\XKV@sgtrue\XKV@sgfalse
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\define@key}
% \changes{v1.4}{2004/08/24}{Added optional check}
% Macro to define a key in a family. Original but modified \pf{keyval}
% code. Notice the use of the |KV| prefix as default prefix. This is
% done to allow setting both \pf{keyval} and \pf{xkeyval} keys with
% a single command.
%    \begin{macrocode}
\def\define@key{\XKV@testoptb\XKV@define@key}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@define@key}
% Workhorse for |\define@key|.
%    \begin{macrocode}
\def\XKV@define@key#1{%
%    \end{macrocode}
% Define the key macro.
%    \begin{macrocode}
  \@ifnextchar[{\XKV@d@fine@k@y{#1}}{%
    \expandafter\def\csname\XKV@header#1\endcsname####1}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@d@fine@k@y}
% Defines a key macro and a default value macro.
%    \begin{macrocode}
\def\XKV@d@fine@k@y#1[#2]{%
  \expandafter\def\csname\XKV@header#1@default\expandafter\endcsname
    \expandafter{\csname\XKV@header#1\endcsname{#2}}%
  \expandafter\def\csname\XKV@header#1\endcsname##1%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\define@boolkey}
% Defines a boolean key.
%    \begin{macrocode}
\def\define@boolkey{\XKV@testoptb\XKV@define@boolkey}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@define@boolkey}
% Workhorse for |\define@boolkey|.
%    \begin{macrocode}
\def\XKV@define@boolkey#1{%
%    \end{macrocode}
% Create the conditional.
%    \begin{macrocode}
  \expandafter\newif\csname if\XKV@header#1\endcsname
%    \end{macrocode}
% Create the key function.
%    \begin{macrocode}
  \expandafter\edef\csname\XKV@header#1\endcsname##1%
    {\noexpand\XKV@setbool{\XKV@header#1}{##1}}%
  \@ifnextchar[{\XKV@d@fine@boolkey{#1}}{}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@d@fine@boolkey}
% Define the default value macro.
%    \begin{macrocode}
\def\XKV@d@fine@boolkey#1[#2]{%
  \expandafter\def\csname\XKV@header#1@default\expandafter\endcsname
    \expandafter{\csname\XKV@header#1\endcsname{#2}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@setbool}
% Set a boolean key.
%    \begin{macrocode}
\def\XKV@setbool#1#2{%
  \def\XKV@tempa{true}%
  \def\XKV@tempb{false}%
  \lowercase{\def\XKV@tempc{#2}}%
  \ifx\XKV@tempc\XKV@tempa\else
    \ifx\XKV@tempc\XKV@tempb\else
      \let\XKV@tempc\relax
    \fi
  \fi
  \ifx\XKV@tempc\relax
    \XKV@err{boolean can only be `true' or `false'}%
  \else
    \csname#1\XKV@tempc\endcsname
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\define@cmdkey}
% Defines a command key. This key will store its input in a macro
% for later use.
%    \begin{macrocode}
\def\define@cmdkey{\XKV@testoptb\XKV@define@cmdkey}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@define@cmdkey}
% Workhorse for |\define@cmdkey|.
%    \begin{macrocode}
\def\XKV@define@cmdkey#1{%
%    \end{macrocode}
% Create the key function.
%    \begin{macrocode}
  \expandafter\edef\csname\XKV@header#1\endcsname##1{\noexpand\def
    \expandafter\noexpand\csname\XKV@header#1@cmd\endcsname{##1}}%
  \@ifnextchar[{\XKV@d@fine@cmdkey{#1}}{}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@d@fine@cmdkey}
% Defines the default value macro.
%    \begin{macrocode}
\def\XKV@d@fine@cmdkey#1[#2]{%
  \expandafter\def\csname\XKV@header#1@default\expandafter\endcsname
    \expandafter{\csname\XKV@header#1\endcsname{#2}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\key@ifundefined}
% This macro allows checking if a key is defined in a family from
% a list of families.
%    \begin{macrocode}
\def\key@ifundefined{\@testopt\XKV@key@ifundefined{KV}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@key@ifundefined}
% This macro is split in two parts
% so that |\XKV@p@x| can use only the main part of the macro.
%    \begin{macrocode}
\def\XKV@key@ifundefined[#1]#2{%
%    \end{macrocode}
% Set the prefix and save the key name and family names.
%    \begin{macrocode}
  \XKV@makepf{#1}%
  \XKV@sp@deflist\XKV@fams{#2}%
  \XKV@key@if@ndefined
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@key@if@ndefined}
% Workhorse for |\key@ifundefined|.
%    \begin{macrocode}
\def\XKV@key@if@ndefined#1{%
  \XKV@knftrue
  \KV@@sp@def\XKV@tkey{#1}%
%    \end{macrocode}
% Loop over possible families.
%    \begin{macrocode}
  \XKV@whilist\XKV@tfam:=\XKV@fams\do\ifXKV@knf\fi{%
%    \end{macrocode}
% Set the header.
%    \begin{macrocode}
    \XKV@makehd\XKV@tfam
%    \end{macrocode}
% Check whether the macro for the key is defined.
%    \begin{macrocode}
    \XKV@ifundefined{\XKV@header\XKV@tkey}{}{\XKV@knffalse}%
  }%
%    \end{macrocode}
% Execute one of the final two arguments depending on state of |\XKV@knf|.
%    \begin{macrocode}
  \ifXKV@knf
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\disable@keys}
% Macro that make a key produce a warning on use.
%    \begin{macrocode}
\def\disable@keys{\XKV@testoptb\XKV@disable@keys}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@disable@keys}
% Workhorse for |\disable@keys| which redefines a key macro.
%    \begin{macrocode}
\def\XKV@disable@keys#1{%
  \XKV@checksanitizeb{#1}\XKV@tempa
  \@for\XKV@tempa:=\XKV@tempa\do{%
    \XKV@ifundefined{\XKV@header\XKV@tempa}{%
      \XKV@err{key `\XKV@tempa' undefined}%
    }{%
      \edef\XKV@tempb{\noexpand\XKV@warn{key `\XKV@tempa' has been disabled}}%
      \XKV@ifundefined{\XKV@header\XKV@tempa @default}{%
        \edef\XKV@tempc{\noexpand\XKV@define@key{\XKV@tempa}}%
      }{%
        \edef\XKV@tempc{\noexpand\XKV@define@key{\XKV@tempa}[]}%
      }%
      \expandafter\XKV@tempc\expandafter{\XKV@tempb}%
    }%
  }%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\presetkeys}
% \begin{macro}{\gpresetkeys}
% \changes{v1.7}{2004/11/25}{Made incremental}
% This provides the presetting system. The macro works incrementally:
% keys that have been preset before will overwrite the old preset
% values, new ones will be added to the end of the preset list.
%    \begin{macrocode}
\def\presetkeys{\XKV@stfalse\XKV@testoptb\XKV@presetkeys}
\def\gpresetkeys{\XKV@sttrue\XKV@testoptb\XKV@presetkeys}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@presetkeys}
% Executes the merging macro |\XKV@pr@setkeys| for both head and tail
% presets.
%    \begin{macrocode}
\def\XKV@presetkeys#1#2{%
  \XKV@pr@setkeys{#1}{preseth}%
  \XKV@pr@setkeys{#2}{presett}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@pr@setkeys}
% Check whether presets have already been defined. If not, define them
% and do not start the merging macro. Otherwise, create the control
% sequence that stores these presets and start merging.
%    \begin{macrocode}
\def\XKV@pr@setkeys#1#2{%
  \XKV@ifundefined{XKV@\XKV@header#2}{%
    \XKV@checksanitizea{#1}\XKV@tempa
    \ifXKV@st\expandafter\global\fi\expandafter\def
      \csname XKV@\XKV@header#2\expandafter\endcsname\expandafter{\XKV@tempa}%
  }{%
    \expandafter\XKV@merge\csname XKV@\XKV@header#2\endcsname{#1}\XKV@getkeyname
  }%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\delpresetkeys}
% \begin{macro}{\gdelpresetkeys}
% Macros to remove entries from presets.
%    \begin{macrocode}
\def\delpresetkeys{\XKV@stfalse\XKV@testoptb\XKV@delpresetkeys}
\def\gdelpresetkeys{\XKV@sttrue\XKV@testoptb\XKV@delpresetkeys}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@delpresetkeys}
% Run the main macro for both head tail presets.
%    \begin{macrocode}
\def\XKV@delpresetkeys#1#2{%
  \XKV@d@lpresetkeys{#1}{preseth}%
  \XKV@d@lpresetkeys{#2}{presett}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@d@lpresetkeys}
% Check whether presets have been saved and if so, start deletion
% algorithm. Supply the macro |\XKV@getkeyname| to retrieve key names
% from entries.
%    \begin{macrocode}
\def\XKV@d@lpresetkeys#1#2{%
  \XKV@ifundefined{XKV@\XKV@header#2}{%
    \XKV@err{no presets defined for `\XKV@header'}%
  }{%
    \expandafter\XKV@delete\csname XKV@\XKV@header#2\endcsname{#1}\XKV@getkeyname
  }%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\unpresetkeys}
% \begin{macro}{\gunpresetkeys}
% Removes presets for a particular family.
%    \begin{macrocode}
\def\unpresetkeys{\XKV@stfalse\XKV@testoptb\XKV@unpresetkeys}
\def\gunpresetkeys{\XKV@sttrue\XKV@testoptb\XKV@unpresetkeys}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@unpresetkeys}
% Undefine the preset macros. We make them undefined since this will
% make them appear undefined to both versions of the macro |\XKV@ifundefined|.
% Making the macros |\relax| would work in the case that no \eTeX\ is
% available (hence using |\ifx\csname|), but doesn't work when \eTeX\
% is used (and using |\ifcsname|).
%    \begin{macrocode}
\def\XKV@unpresetkeys{%
  \XKV@ifundefined{XKV@\XKV@header preseth}{%
    \XKV@err{no presets defined for `\XKV@header'}%
  }{%
    \ifXKV@st\expandafter\global\fi\expandafter\let
      \csname XKV@\XKV@header preseth\endcsname\@undefined
    \ifXKV@st\expandafter\global\fi\expandafter\let
      \csname XKV@\XKV@header presett\endcsname\@undefined
  }%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\savekeys}
% \begin{macro}{\gsavekeys}
% Store a list of keys of a family that should always be saved. The
% macro works incrementally and avoids duplicate entries in the list.
%    \begin{macrocode}
\def\savekeys{\XKV@stfalse\XKV@testoptb\XKV@savekeys}
\def\gsavekeys{\XKV@sttrue\XKV@testoptb\XKV@savekeys}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@savekeys}
% Check whether something has been saved before. If not, start merging.
%    \begin{macrocode}
\def\XKV@savekeys#1{%
  \XKV@ifundefined{XKV@\XKV@header save}{%
    \XKV@checksanitizeb{#1}\XKV@tempa
    \ifXKV@st\expandafter\global\fi\expandafter\def
      \csname XKV@\XKV@header save\expandafter\endcsname\expandafter{\XKV@tempa}%
  }{%
    \expandafter\XKV@merge\csname XKV@\XKV@header save\endcsname{#1}\XKV@getsg
  }%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\delsavekeys}
% \begin{macro}{\gdelsavekeys}
% Remove entries from the list of save keys.
%    \begin{macrocode}
\def\delsavekeys{\XKV@stfalse\XKV@testoptb\XKV@delsavekeys}
\def\gdelsavekeys{\XKV@sttrue\XKV@testoptb\XKV@delsavekeys}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@delsavekeys}
% Check whether save keys are defined and if yes, start deletion
% algorithm. Use the macro |\XKV@getsg| to retrieve key names from
% entries.
%    \begin{macrocode}
\def\XKV@delsavekeys#1{%
  \XKV@ifundefined{XKV@\XKV@header save}{%
    \XKV@err{no save keys defined for `\XKV@header'}%
  }{%
    \expandafter\XKV@delete\csname XKV@\XKV@header save\endcsname{#1}\XKV@getsg
  }%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\unsavekeys}
% \begin{macro}{\gunsavekeys}
% Similar to |\unpresetkeys|, but removes the `save keys list' for a
% particular family.
%    \begin{macrocode}
\def\unsavekeys{\XKV@stfalse\XKV@testoptb\XKV@unsavekeys}
\def\gunsavekeys{\XKV@sttrue\XKV@testoptb\XKV@unsavekeys}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@unsavekeys}
% Workhorse for |\unsavekeys|.
%    \begin{macrocode}
\def\XKV@unsavekeys{%
  \XKV@ifundefined{XKV@\XKV@header save}{%
    \XKV@err{no save keys defined for `\XKV@header'}%
  }{%
    \ifXKV@st\expandafter\global\fi\expandafter\let
      \csname XKV@\XKV@header save\endcsname\@undefined
  }%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\setkeys}
% Set keys. The starred version does not produce errors, but appends
% keys that cannot be located to the list in |\XKV@rm|. The plus
% version sets keys in all families that are supplied. Use
% |\XKV@testopta| to handle optional arguments.
%    \begin{macrocode}
\def\setkeys{\XKV@testopta\XKV@setkeys}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@setkeys}
% \changes{v1.8a}{2004/12/20}{Solved small bug}
% Workhorse for |\setkeys|.
%    \begin{macrocode}
\def\XKV@setkeys[#1]#2{%
%    \end{macrocode}
% Macros to retrieve a list of keys from the user input.
%    \begin{macrocode}
  \def\XKV@tempa##1,{%
    \def\XKV@tempb{##1}%
    \ifx\XKV@tempb\@nnil\else
      \XKV@g@tkeyname##1=\@nil\XKV@tempb
      \XKV@addtolist@x\XKV@kna\XKV@tempb
      \expandafter\XKV@tempa
    \fi
  }%
  \XKV@checksanitizea{#2}\XKV@resb
  \let\XKV@kna\@empty
  \expandafter\XKV@tempa\XKV@resb,\@nil,%
%    \end{macrocode}
% Initialize the remaining keys.
%    \begin{macrocode}
  \let\XKV@rm\@empty
%    \end{macrocode}
% Initialize the macro that should be executed.
%    \begin{macrocode}
  \let\XKV@exec\@empty
%    \end{macrocode}
% Now scan the list of families for preset keys and set user input keys.
%    \begin{macrocode}
  \XKV@usepresetkeys{#1}{preseth}%
  \expandafter\XKV@s@tkeys\expandafter{\XKV@resb}{#1}%
  \XKV@usepresetkeys{#1}{presett}%
%    \end{macrocode}
% Execute all key macros.
%    \begin{macrocode}
  \XKV@exec
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@usepresetkeys}
% Loop over the list of families and check them for preset keys. If
% present, set them right away, taking into account the keys which
% are set by the user.
%    \begin{macrocode}
\def\XKV@usepresetkeys#1#2{%
  \XKV@for\XKV@tfam:=\XKV@fams\do{%
    \XKV@makehd\XKV@tfam
    \XKV@ifundefined{XKV@\XKV@header#2}{}{%
      \XKV@toks\expandafter\expandafter\expandafter
        {\csname XKV@\XKV@header#2\endcsname}%
      \@expandtwoargs\XKV@s@tkeys{\the\XKV@toks}%
        {\XKV@kna\ifx\XKV@kna\@empty\else,\fi#1}%
    }%
  }%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@s@tkeys}
% This macro starts the loop over keys.
%    \begin{macrocode}
\def\XKV@s@tkeys#1#2{%
%    \end{macrocode}
% Define the list of key names which should be ignored.
%    \begin{macrocode}
  \XKV@sp@deflist\XKV@kn{#2}%
%    \end{macrocode}
% Start the loop over keys.
%    \begin{macrocode}
  \XKV@s@tk@ys#1,\@nil,%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@s@tk@ys}
% Workhorse for |\XKV@s@tkeys|.
%    \begin{macrocode}
\def\XKV@s@tk@ys#1,{%
  \def\XKV@tempa{#1}%
  \ifx\XKV@tempa\@nnil\else
    \XKV@knftrue
%    \end{macrocode}
% Split key and value.
%    \begin{macrocode}
    \XKV@split#1==\@nil
%    \end{macrocode}
% Check whether the key has been found.
%    \begin{macrocode}
    \ifXKV@knf
      \ifXKV@inpox
%    \end{macrocode}
% We are in the options section. Try to use the macro defined by
% |\DeclareOptionX*|.
%    \begin{macrocode}
        \ifx\XKV@doxs\relax
%    \end{macrocode}
% For classes, ignore unknown (possibly global) options. For packages,
% raise the standard \LaTeX\ error.
%    \begin{macrocode}
          \ifx\@currext\@clsextension\else
            \let\CurrentOption\XKV@tkey\@unknownoptionerror
          \fi
        \else
%    \end{macrocode}
% Pass the option through |\DeclareOptionX*|.
%    \begin{macrocode}
          \def\CurrentOption{#1}\XKV@doxs
        \fi
      \else
%    \end{macrocode}
% If not in the options section, raise an error or add the key to the
% list in |\XK@rm| when |\setkeys*| has been used.
%    \begin{macrocode}
        \ifXKV@st
          \XKV@addtolist@n\XKV@rm{#1}%
        \else
          \XKV@err{`\XKV@tkey' undefined in families `\XKV@fams'}%
        \fi
      \fi
    \else
%    \end{macrocode}
% Remove global options set by the document class from |\@unusedoptionlist|.
% Global options set by other packages or class will be removed by
% |\ProcessOptionsX*|.
%    \begin{macrocode}
      \ifXKV@inpox\ifx\XKV@testclass\XKV@documentclass
        \XKV@useoption{#1}%
      \fi\fi
    \fi
    \expandafter\XKV@s@tk@ys
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@split}
% \changes{v1.4}{2004/08/24}{Made macro more efficient}
% Macro that splits keys and values.
%    \begin{macrocode}
\def\XKV@split#1=#2=#3\@nil{%
%    \end{macrocode}
% Remove spaces from key name and check for |\savevalue| and |\gsavevalue|.
%    \begin{macrocode}
  \XKV@g@tkeyname#1=\@nil\XKV@tkey
  \expandafter\KV@@sp@def\expandafter\XKV@tkey\expandafter{\XKV@tkey}%
%    \end{macrocode}
% If the key is empty and a value has been specified, generate an error.
%    \begin{macrocode}
  \ifx\XKV@tkey\@empty
    \ifx\@empty#2\@empty\else
      \XKV@toks{#2}%
      \XKV@err{no key specified for value `\the\XKV@toks'}%
    \fi
    \XKV@knffalse
  \else
%    \end{macrocode}
% If in the |\XKV@kn| list, ignore the key.
%    \begin{macrocode}
    \@expandtwoargs\in@{,\XKV@tkey,}{,\XKV@kn,}%
    \ifin@\XKV@knffalse\else
      \KV@@sp@def\XKV@tempa{#2}%
%    \end{macrocode}
% Check global setting by |\savekeys| to know whether or not to save
% the value of the key at hand.
%    \begin{macrocode}
      \XKV@ifundefined{XKV@\XKV@header save}{}{%
        \expandafter\XKV@testsavekey\csname XKV@\XKV@header save\endcsname\XKV@tkey
      }%
%    \end{macrocode}
% Save the value of a key.
%    \begin{macrocode}
      \ifXKV@rkv
        \ifXKV@sg\expandafter\global\fi\expandafter\let
          \csname XKV@\XKV@header\XKV@tkey @value\endcsname\XKV@tempa
      \fi
%    \end{macrocode}
% Replace pointers by saved values.
%    \begin{macrocode}
      \expandafter\XKV@replacepointers\expandafter{\XKV@tempa}%
      \ifXKV@pl
%    \end{macrocode}
% If a command with a |+| is used, set keys in all families on the
% list.
%    \begin{macrocode}
        \XKV@for\XKV@tfam:=\XKV@fams\do{%
          \XKV@makehd\XKV@tfam
          \expandafter\XKV@setkey@infam\expandafter{\XKV@tempa}{#3}%
        }%
      \else
%    \end{macrocode}
% Else, scan the families on the list but stop when the key is found
% or when the list has run out.
%    \begin{macrocode}
        \XKV@whilist\XKV@tfam:=\XKV@fams\do\ifXKV@knf\fi{%
          \XKV@makehd\XKV@tfam
          \expandafter\XKV@setkey@infam\expandafter{\XKV@tempa}{#3}%
        }%
      \fi
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@testsavekey}
% This macro checks whether the key in macro |#2| appears in the
% save list in macro |#1|. Furthermore, it checks whether or not to
% save the key globally. In other words, that |\global{key}| is in
% the list.
%    \begin{macrocode}
\def\XKV@testsavekey#1#2{%
  \ifXKV@rkv\else
    \@for\XKV@resa:=#1\do{%
      \expandafter\XKV@ifcmd\expandafter{\XKV@resa}\global\XKV@resa{%
        \ifx#2\XKV@resa
          \XKV@rkvtrue\XKV@sgtrue
        \fi
      }{%
        \ifx#2\XKV@resa
          \XKV@rkvtrue\XKV@sgfalse
        \fi
      }%
    }%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@replacepointers}
% \begin{macro}{\XKV@r@placepointers}
% \changes{v1.8d}{2005/01/02}{Added \ci{XKV@afterfi} to avoid capacity problems when many pointers present}
% \changes{v1.9}{2005/01/16}{Added back linking test}
% \changes{v2.0}{2005/01/30}{Simplified}
% Replaces all pointers by their saved value. The result is stored in
% |#4|. We feed the replacement and the following tokens again to the
% macro to replace nested pointers. It stops when no pointers are
% found anymore.
%    \begin{macrocode}
\def\XKV@replacepointers#1{%
  \let\XKV@tempa\@empty
  \let\XKV@resa\@empty
  \XKV@r@placepointers#1\usevalue\@nil
}
\def\XKV@r@placepointers#1\usevalue#2{%
  \XKV@addtomacro@n\XKV@tempa{#1}%
  \ifx\@nil#2\relax\else\XKV@afterfi
    \XKV@ifundefined{XKV@\XKV@header#2@value}{%
      \XKV@err{no value recorded for key `#2'; ignored}%
      \XKV@r@placepointers
    }{%
      \@expandtwoargs\in@{,#2,}{,\XKV@resa,}%
      \ifin@\XKV@afterelsefi
        \XKV@err{back linking pointers; pointer replacement canceled}%
      \else\XKV@afterfi
        \XKV@addtolist@x\XKV@resa{#2}%
        \expandafter\expandafter\expandafter\XKV@r@placepointers
          \csname XKV@\XKV@header#2@value\endcsname
      \fi
    }%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XKV@setkey@infam}
% Sets a key in a family. Based on \pf{keyval} code.
%    \begin{macrocode}
\def\XKV@setkey@infam#1#2{%
  \XKV@ifundefined{\XKV@header\XKV@tkey}{}{%
%    \end{macrocode}
% Check whether the key macro is defined.
%    \begin{macrocode}
    \XKV@knffalse
    \ifx\@empty#2\@empty
%    \end{macrocode}
% No value given, use default.
%    \begin{macrocode}
      \XKV@ifundefined{\XKV@header\XKV@tkey @default}{%
        \XKV@err{no value specified for key `\XKV@tkey'}%
      }{%
%    \end{macrocode}
% Execute key with the default value.
%    \begin{macrocode}
        \expandafter\expandafter\expandafter\XKV@default
          \csname\XKV@header\XKV@tkey @default\endcsname\@nil
      }%
    \else
%    \end{macrocode}
% Add key macro and its value to the execution macro.
%    \begin{macrocode}
      \XKV@addtomacro@o\XKV@exec{\csname\XKV@header\XKV@tkey\endcsname{#1}\relax}%
    \fi
  }%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@default}
% \changes{v2.0}{2005/01/30}{Repaired adding extra braces when executing default value}
% This macro checks the |\prefix@fam@key@default| macro. If the macro
% has the form as defined by \pf{keyval} or \pf{xkeyval}, it is
% possible to extract the default value and safe that (if requested)
% and replace pointers. If the form is incorrect, just execute the
% macro and forget about possible pointers. The reason for this check
% is that certain packages (like \pf{fancyvrb}) abuse the `default
% value system' to execute code instead of setting keys by redefining
% default value macros. These macros do not actually contain a
% default value and trying to extract that would not work.
%    \begin{macrocode}
\def\XKV@default#1#2\@nil{%
%    \end{macrocode}
% Retrieve the name of the first token in the macro.
%    \begin{macrocode}
  \expandafter\edef\expandafter\XKV@tempa\expandafter{\expandafter\@gobble\string#1}%
%    \end{macrocode}
% Construct the name that we expect on the basis of the \pf{keyval}
% and \pf{xkeyval} syntax of default values.
%    \begin{macrocode}
  \edef\XKV@tempb{\XKV@header\XKV@tkey}%
%    \end{macrocode}
% Sanitize |\XKV@tempb| to reset catcodes for comparison with |\XKV@tempa|.
%    \begin{macrocode}
  \@onelevel@sanitize\XKV@tempb
  \ifx\XKV@tempa\XKV@tempb
%    \end{macrocode}
% If it is safe, extract the value. We temporarily redefine the key
% macro to save the default value in a macro. Saving the default value
% itself directly to a macro when defining keys would of course be easier,
% but a lot of packages rely on this system created by \pf{keyval}, so
% we have to support it here.
%    \begin{macrocode}
    \begingroup
      \expandafter\def\csname\XKV@header\XKV@tkey\endcsname##1{%
        \gdef\XKV@tempa{##1}%
      }%
      \csname\XKV@header\XKV@tkey @default\endcsname
    \endgroup
%    \end{macrocode}
% Save the default value to a value macro if either the key name has
% been entered in a |\savekeys| macro or the starred form has been used.
%    \begin{macrocode}
    \XKV@ifundefined{XKV@\XKV@header save}{}{%
      \expandafter\XKV@testsavekey\csname XKV@\XKV@header save\endcsname\XKV@tkey
    }%
    \ifXKV@rkv
      \ifXKV@sg\expandafter\global\fi\expandafter\let
        \csname XKV@\XKV@header\XKV@tkey @value\endcsname\XKV@tempa
    \fi
%    \end{macrocode}
% Replace the pointers.
%    \begin{macrocode}
    \expandafter\XKV@replacepointers\expandafter{\XKV@tempa}%
%    \end{macrocode}
% Add the key macro with the (possibly changed) default value to the
% execution macro.
%    \begin{macrocode}
    \XKV@addtomacro@o\XKV@exec{\expandafter#1\expandafter{\XKV@tempa}\relax}%
  \else
%    \end{macrocode}
% Add the default value macro without any features to the execution macro.
%    \begin{macrocode}
    \expandafter\XKV@addtomacro@o\expandafter\XKV@exec\expandafter
      {\csname\XKV@header\XKV@tkey @default\endcsname\relax}%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\setrmkeys}
% Set remaining keys stored in |\XKV@rm|. The starred version creates
% a new list in |\XKV@rm| in case there are still keys that cannot
% be located in the families specified. Care is taken again not to
% expand fragile macros. Use |\XKV@testopa| again to handle optional
% arguments.
%    \begin{macrocode}
\def\setrmkeys{\XKV@testopta\XKV@setrmkeys}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@setrmkeys}
% Submits the keys in |\XKV@rm| to |\XKV@setkeys|.
%    \begin{macrocode}
\def\XKV@setrmkeys[#1]{%
  \def\XKV@tempa{\XKV@setkeys[#1]}%
  \expandafter\XKV@tempa\expandafter{\XKV@rm}%
}
%    \end{macrocode}
% \end{macro}
% Reset catcodes.
%    \begin{macrocode}
\XKVcatcodes
%</tex>
%    \end{macrocode}
% \subsection{\LaTeX\ program}
% \changes{v1.5}{2004/09/27}{Made macros avoid expansions of options}
% Initialize the package.
%    \begin{macrocode}
%<*latex>
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{xkeyval}[2005/01/30 v2.0 package option processing (HA)]
%    \end{macrocode}
% Initializations. Load |xkeyval.tex|, adjust some catcodes to define
% internal macros and initialize the |\DeclareOptionX*| working macro.
%    \begin{macrocode}
\ifx\XKeyValLoaded\endinput\else\input xkeyval.tex \fi
\edef\XKVcatcodes{%
  \catcode`\noexpand\=\the\catcode`\=\relax
  \catcode`\noexpand\,\the\catcode`\,\relax
  \catcode`\noexpand\:\the\catcode`\:\relax
  \let\noexpand\XKVcatcodes\relax
}
\catcode`\=12\relax
\catcode`\,12\relax
\catcode`\:12\relax
\let\XKV@doxs\relax
%    \end{macrocode}
% \begin{macro}{\XKV@warn}
% \begin{macro}{\XKV@err}
% Warning and error macros.
%    \begin{macrocode}
\def\XKV@warn#1{\PackageWarning{xkeyval}{#1}}
\def\XKV@err#1{\PackageError{xkeyval}{#1}\@ehc}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% At loading, retrieve document class, copy |\@classoptionslist| to
% |\XKV@classoptionslist| and filter |key=value| pairs from the original.
%    \begin{macrocode}
\ifx\XKV@documentclass\@undefined
%    \end{macrocode}
% Retrieve the document class from |\@filelist|. This is the first
% filename in the list with a class extension. Use a while loop to
% scan the list and stop when we found the first filename which is
% a class. Also stop in case the list is scanned fully.
%    \begin{macrocode}
  \XKV@whilist\XKV@tempa:=\@filelist\do\ifx\XKV@documentclass\@undefined\fi{%
    \filename@parse\XKV@tempa
    \ifx\filename@ext\@clsextension
      \edef\XKV@documentclass{\filename@base.\filename@ext}%
    \fi
  }
  \ifx\XKV@documentclass\@undefined
    \XKV@err{xkeyval loaded before \protect\documentclass}%
    \let\XKV@documentclass\@empty
    \let\XKV@classoptionslist\@empty
  \else
    \let\XKV@classoptionslist\@classoptionslist
%    \end{macrocode}
% \changes{v1.4}{2004/08/24}{Fixed small bug in class option filtering}
% Code to filter |key=value| pairs from |\@classoptionslist| without
% expanding options.
%    \begin{macrocode}
    \def\XKV@tempa#1{%
      \let\@classoptionslist\@empty
      \XKV@tempb#1,\@nil,%
    }
    \def\XKV@tempb#1,{%
      \def\XKV@tempa{#1}%
      \ifx\XKV@tempa\@nnil\else
        \in@{=}{#1}%
        \ifin@\else\XKV@addtolist@n\@classoptionslist{#1}\fi
        \expandafter\XKV@tempb
      \fi
    }
    \expandafter\XKV@tempa\expandafter{\@classoptionslist}
  \fi
\fi
%    \end{macrocode}
% \begin{macro}{\XKV@testoptc}
% \begin{macro}{\XKV@t@stoptc}
% \begin{macro}{\XKV@t@st@ptc}
% \changes{v1.9}{2005/01/16}{Added extension to default family}
% \begin{macro}{\XKV@@t@st@ptc}
% Macros for |\ExecuteOptionsX| and |\ProcessOptionsX| for testing for
% optional arguments and inserting default values.
%    \begin{macrocode}
\def\XKV@testoptc#1{%
  \XKV@ifstar{\XKV@sttrue\XKV@t@stoptc#1}{\XKV@stfalse\XKV@t@stoptc#1}%
}
\def\XKV@t@stoptc#1{\@testopt{\XKV@t@st@ptc#1}{KV}}
\def\XKV@t@st@ptc#1[#2]{%
  \XKV@makepf{#2}%
  \@ifnextchar<{\XKV@@t@st@ptc#1}{\XKV@@t@st@ptc#1<\@currname.\@currext>}%
}
\def\XKV@@t@st@ptc#1<#2>{%
  \XKV@sp@deflist\XKV@fams{#2}%
  \@testopt#1{}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% Macros for class and package writers. These are mainly shortcuts to
% |\define@key| and |\setkeys|. The \LaTeX\ macro |\@fileswith@pti@ns|
% is set to generate an error. This is the case when a class or package
% is loaded in between |\DeclareOptionX| and |\ProcessOptionsX| commands.
% \begin{macro}{\DeclareOptionX}
% Declare an option.
%    \begin{macrocode}
\def\DeclareOptionX{%
  \let\@fileswith@pti@ns\@badrequireerror
  \XKV@ifstar\XKV@dox\XKV@d@x
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@dox}
% This macro defines |\XKV@doxs| to be used for unknown options.
%    \begin{macrocode}
\long\def\XKV@dox#1{\XKV@toks{#1}\edef\XKV@doxs{\the\XKV@toks}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@d@x}
% \begin{macro}{\XKV@@d@x}
% \changes{v1.9}{2005/01/16}{Added extension to default family}
% \begin{macro}{\XKV@@@d@x}
% \changes{v1.1}{2004/04/30}{Made to insert an empty default value
% if none present for \ci{DeclareOptionX}}
% Insert default prefix and family name (which is the filename of the
% class or package) and add empty default value if none present. Execute
% |\define@key|.
%    \begin{macrocode}
\def\XKV@d@x{\@testopt\XKV@@d@x{KV}}
\def\XKV@@d@x[#1]{\@ifnextchar<{\XKV@@@d@x[#1]}{\XKV@@@d@x[#1]<\@currname.\@currext>}}
\def\XKV@@@d@x[#1]<#2>#3{\@testopt{\define@key[#1]{#2}{#3}}{}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}{\ExecuteOptionsX}
% This macro sets keys to specified values and uses |\XKV@setkeys| to do
% the job. Insert default prefix and family name if none provided. Use
% |\XKV@t@stoptc| to handle optional arguments and reset |\ifXKV@st|
% and |\ifXKV@pl| first to avoid unexpected behavior when |\setkeys*+|
% (or a friend) has been used before |\ExecuteOptionsX|.
%    \begin{macrocode}
\def\ExecuteOptionsX{\XKV@stfalse\XKV@plfalse\XKV@t@stoptc\XKV@setkeys}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ProcessOptionsX}
% \changes{v1.4}{2004/08/24}{Fixed macro for \ci{LoadClass} case}
% Processes class or package using \pf{xkeyval}. The starred version
% copies class options submitted by the user as well, given that they
% are defined in the local families which are passed to the macro. Use
% |\XKV@testoptc| to handle optional arguments.
%    \begin{macrocode}
\def\ProcessOptionsX{\XKV@stfalse\XKV@plfalse\XKV@testoptc\XKV@pox}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@pox}
% Workhorse for |\ProcessOptionsX| and |\ProcessOptionsX*|.
%    \begin{macrocode}
\def\XKV@pox[#1]{%
  \let\XKV@tempa\@empty
%    \end{macrocode}
% Set |\XKV@inpox|: indicates that we are in |\ProcessOptionsX| to
% invoke a special routine in |\XKV@s@tkeys|.
%    \begin{macrocode}
  \XKV@inpoxtrue
%    \end{macrocode}
% Set |\@fileswith@pti@ns| again in case no |\DeclareOptionX| has been
% used. This will be used to identify a call to |\setkeys| from
% |\ProcessOptionsX|.
%    \begin{macrocode}
  \let\@fileswith@pti@ns\@badrequireerror
  \edef\XKV@testclass{\@currname.\@currext}%
%    \end{macrocode}
% If \pf{xkeyval} is loaded by the document class, initialize
% |\@unusedoptionlist|.
%    \begin{macrocode}
  \ifx\XKV@testclass\XKV@documentclass
    \let\@unusedoptionlist\XKV@classoptionslist
    \XKV@ifundefined{ver@xkvltxp.sty}{}{%
      \@onelevel@sanitize\@unusedoptionlist
    }%
  \else
%    \end{macrocode}
% Else, if the starred version is used, copy global options in case
% they are defined in local families. Do not execute this in the
% document class to avoid setting keys twice.
%    \begin{macrocode}
    \ifXKV@st
      \def\XKV@tempb##1,{%
        \def\CurrentOption{##1}%
        \ifx\CurrentOption\@nnil\else
          \XKV@g@tkeyname##1=\@nil\CurrentOption
          \XKV@key@if@ndefined{\CurrentOption}{}{%
%    \end{macrocode}
% If the option also exists in local families, add it to the list for
% later use and remove it from |\@unusedoptionlist|.
%    \begin{macrocode}
            \XKV@useoption{##1}%
            \XKV@addtolist@n\XKV@tempa{##1}%
          }%
          \expandafter\XKV@tempb
        \fi
      }%
      \expandafter\XKV@tempb\XKV@classoptionslist,\@nil,%
    \fi
  \fi
%    \end{macrocode}
% Add current package options to the list.
%    \begin{macrocode}
  \expandafter\XKV@addtolist@o\expandafter
    \XKV@tempa\csname opt@\@currname.\@currext\endcsname
%    \end{macrocode}
% Set options. We can be certain that global options can be set since
% the definitions of local options have been checked above. Note that
% |\DeclareOptionX*| will not consume global options when
% |\ProcessOptionsX*| is used.
%    \begin{macrocode}
  \def\XKV@tempb{\XKV@setkeys[#1]}%
  \expandafter\XKV@tempb\expandafter{\XKV@tempa}%
%    \end{macrocode}
% Reset the macro created by |\DeclareOptionX*| to avoid processing
% future unknown keys using |\XKV@doxs|.
%    \begin{macrocode}
  \let\XKV@doxs\relax
%    \end{macrocode}
% Reset the |\XKV@rm| macro to avoid processing remaining options
% with |\setrmkeys|.
%    \begin{macrocode}
  \let\XKV@rm\@empty
%    \end{macrocode}
% Reset |\ifXKV@inpox|: not in |\ProcessOptionsX| anymore.
%    \begin{macrocode}
  \XKV@inpoxfalse
%    \end{macrocode}
% Reset |\@fileswith@pti@ns| to allow loading of classes or packages
% again.
%    \begin{macrocode}
  \let\@fileswith@pti@ns\@@fileswith@pti@ns
  \AtEndOfPackage{\let\@unprocessedoptions\relax}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XKV@useoption}
% Removes an option from |\@unusedoptionlist|.
%    \begin{macrocode}
\def\XKV@useoption#1{%
  \def\XKV@resa{#1}%
  \XKV@ifundefined{ver@xkvltxp.sty}{}{%
    \@onelevel@sanitize\XKV@resa
  }%
  \@expandtwoargs\@removeelement{\XKV@resa}{\@unusedoptionlist}\@unusedoptionlist
}
%    \end{macrocode}
% \end{macro}
% \changes{v1.3}{2004/05/09}{Moved the options section to the end of
% the package to allow it to use \pf{xkeyval} option macros}
% The options section. Postponed to the end to allow for using
% \pf{xkeyval} options macros. All options are silently ignored.
%    \begin{macrocode}
\DeclareOptionX*{\PackageWarning{xkeyval}{Unknown option `\CurrentOption'}}
\ProcessOptionsX
%    \end{macrocode}
% Reset catcodes.
%    \begin{macrocode}
\XKVcatcodes
%</latex>
%    \end{macrocode}
% \subsection{\LaTeX\ kernel patch}\label{sec:ltxpatch}
% \changes{v1.5}{2004/09/27}{Added \LaTeX\ kernel patch}
% This section redefines some kernel macros as to avoid expansions of
% options at several places to allow for macros in key values in class
% and package options. It uses a temporary token register and some
% careful expansions. Notice that |\@unusedoptionlist| is sanitized
% after creation by \pf{xkeyval} to avoid |\@removeelement| causing
% problems with macros and braces. See for more information about the
% original versions of the macros below the kernel source documentation
% \cite{LaTeXbase}.
%    \begin{macrocode}
%<*ltxpatch>
%%
%% Based on latex.ltx.
%%
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{xkvltxp}[2004/12/13 v1.2 LaTeX2e kernel patch (HA)]
\def\@pass@ptions#1#2#3{%
  \def\reserved@a{#2}%
  \def\reserved@b{\CurrentOption}%
  \ifx\reserved@a\reserved@b
    \@ifundefined{opt@#3.#1}{\@temptokena\expandafter{#2}}{%
      \@temptokena\expandafter\expandafter\expandafter{\csname opt@#3.#1\endcsname}%
      \@temptokena\expandafter\expandafter\expandafter{%
        \expandafter\the\expandafter\@temptokena\expandafter,#2}%
    }%
  \else
    \@ifundefined{opt@#3.#1}{\@temptokena{#2}}{%
      \@temptokena\expandafter\expandafter\expandafter{\csname opt@#3.#1\endcsname}%
      \@temptokena\expandafter{\the\@temptokena,#2}%
    }%
  \fi
  \expandafter\xdef\csname opt@#3.#1\endcsname{\the\@temptokena}%
}
\def\OptionNotUsed{%
  \ifx\@currext\@clsextension
    \let\reserved@a\CurrentOption
    \@onelevel@sanitize\reserved@a
    \xdef\@unusedoptionlist{%
      \ifx\@unusedoptionlist\@empty\else\@unusedoptionlist,\fi
      \reserved@a}%
  \fi
}
\def\@use@ption{%
  \let\reserved@a\CurrentOption
  \@onelevel@sanitize\reserved@a
  \@expandtwoargs\@removeelement\reserved@a
  \@unusedoptionlist\@unusedoptionlist
  \csname ds@\CurrentOption\endcsname
}
\def\@fileswith@pti@ns#1[#2]#3[#4]{%
  \ifx#1\@clsextension
    \ifx\@classoptionslist\relax
      \@temptokena{#2}%
      \xdef\@classoptionslist{\the\@temptokena}%
      \def\reserved@a{%
        \@onefilewithoptions#3[#2][#4]#1%
        \@documentclasshook}%
    \else
      \def\reserved@a{%
        \@onefilewithoptions#3[#2][#4]#1}%
    \fi
  \else
    \@temptokena{#2}%
    \def\reserved@b##1,{%
      \ifx\@nil##1\relax\else
        \ifx\relax##1\relax\else
         \noexpand\@onefilewithoptions##1[\the\@temptokena][#4]\noexpand\@pkgextension
        \fi
        \expandafter\reserved@b
      \fi}%
      \edef\reserved@a{\zap@space#3 \@empty}%
      \edef\reserved@a{\expandafter\reserved@b\reserved@a,\@nil,}%
  \fi
  \reserved@a}
\let\@@fileswith@pti@ns\@fileswith@pti@ns
%</ltxpatch>
%    \end{macrocode}
% \subsection{\pf{keyval} primitives}
% \changes{v1.4}{2004/08/24}{Added \pf{keyval} primitives}
% Since the \pf{xkeyval} macros handle input in a very different way
% than \pf{keyval} macros, it is not wise to redefine \pf{keyval}
% primitives (like |\KV@do| and |\KV@split|) used by other packages
% as a back door into |\setkeys|. Instead, we load the original
% primitives here for compatibility to existing packages using (parts
% of) \pf{keyval}. Most of the code is original, but slightly
% adapted to \pf{xkeyval}. See the \pf{keyval} documentation for
% information about the macros below.
%    \begin{macrocode}
%<*keyval>
%%
%% Based on keyval.sty.
%%
\def\XKV@tempa#1{%
\def\KV@@sp@def##1##2{%
  \futurelet\XKV@resa\KV@@sp@d##2\@nil\@nil#1\@nil\relax##1}%
\def\KV@@sp@d{%
  \ifx\XKV@resa\@sptoken
    \expandafter\KV@@sp@b
  \else
    \expandafter\KV@@sp@b\expandafter#1%
  \fi}%
\def\KV@@sp@b#1##1 \@nil{\KV@@sp@c##1}%
  }
\XKV@tempa{ }
\def\KV@@sp@c#1\@nil#2\relax#3{\XKV@toks{#1}\edef#3{\the\XKV@toks}}
\def\KV@do#1,{%
 \ifx\relax#1\@empty\else
  \KV@split#1==\relax
  \expandafter\KV@do\fi}
\def\KV@split#1=#2=#3\relax{%
  \KV@@sp@def\XKV@tempa{#1}%
  \ifx\XKV@tempa\@empty\else
    \expandafter\let\expandafter\XKV@tempc
      \csname\KV@prefix\XKV@tempa\endcsname
    \ifx\XKV@tempc\relax
      \XKV@err{`\XKV@tempa' undefined}%
    \else
      \ifx\@empty#3\@empty
        \KV@default
      \else
        \KV@@sp@def\XKV@tempb{#2}%
        \expandafter\XKV@tempc\expandafter{\XKV@tempb}\relax
      \fi
    \fi
  \fi}
\def\KV@default{%
  \expandafter\let\expandafter\XKV@tempb
    \csname\KV@prefix\XKV@tempa @default\endcsname
  \ifx\XKV@tempb\relax
    \XKV@err{No value specified for key `\XKV@tempa'}%
  \else
    \XKV@tempb\relax
  \fi}
%</keyval>
%    \end{macrocode}
% \subsection{\TeX\ header}
% This section generates |xkvtxhdr.tex| which contains some standard
% \LaTeX\ macros taken from |latex.ltx|. This will only be loaded
% when not using |xkeyval.sty|.
%    \begin{macrocode}
%<*header>
%%
%% Taken from latex.ltx.
%%
\message{2005/01/02 v1.0 xkeyval TeX header (HA)}
\def\@nnil{\@nil}
\def\@empty{}
\def\newif#1{%
  \count@\escapechar \escapechar\m@ne
    \let#1\iffalse
    \@if#1\iftrue
    \@if#1\iffalse
  \escapechar\count@}
\def\@if#1#2{%
  \expandafter\def\csname\expandafter\@gobbletwo\string#1%
                    \expandafter\@gobbletwo\string#2\endcsname
                       {\let#1#2}}
\long\def\@ifnextchar#1#2#3{%
  \let\reserved@d=#1%
  \def\reserved@a{#2}%
  \def\reserved@b{#3}%
  \futurelet\@let@token\@ifnch}
\def\@ifnch{%
  \ifx\@let@token\@sptoken
    \let\reserved@c\@xifnch
  \else
    \ifx\@let@token\reserved@d
      \let\reserved@c\reserved@a
    \else
      \let\reserved@c\reserved@b
    \fi
  \fi
  \reserved@c}
\def\:{\let\@sptoken= } \:  % this makes \@sptoken a space token
\def\:{\@xifnch} \expandafter\def\: {\futurelet\@let@token\@ifnch}
\let\kernel@ifnextchar\@ifnextchar
\long\def\@testopt#1#2{%
  \kernel@ifnextchar[{#1}{#1[{#2}]}}
\def\@fornoop#1\@@#2#3{}
\long\def\@for#1:=#2\do#3{%
  \expandafter\def\expandafter\@fortmp\expandafter{#2}%
  \ifx\@fortmp\@empty \else
    \expandafter\@forloop#2,\@nil,\@nil\@@#1{#3}\fi}
\long\def\@forloop#1,#2,#3\@@#4#5{\def#4{#1}\ifx #4\@nnil \else
       #5\def#4{#2}\ifx #4\@nnil \else#5\@iforloop #3\@@#4{#5}\fi\fi}
\long\def\@iforloop#1,#2\@@#3#4{\def#3{#1}\ifx #3\@nnil
       \expandafter\@fornoop \else
      #4\relax\expandafter\@iforloop\fi#2\@@#3{#4}}
\long\def \@gobble #1{}
\long\def \@gobbletwo #1#2{}
\def\@expandtwoargs#1#2#3{%
\edef\reserved@a{\noexpand#1{#2}{#3}}\reserved@a}
\edef\@backslashchar{\expandafter\@gobble\string\\}
\newif\ifin@
\def\in@#1#2{%
 \def\in@@##1#1##2##3\in@@{%
  \ifx\in@##2\in@false\else\in@true\fi}%
 \in@@#2#1\in@\in@@}
\def\zap@space#1 #2{%
  #1%
  \ifx#2\@empty\else\expandafter\zap@space\fi
  #2}
\def\strip@prefix#1>{}
\def \@onelevel@sanitize #1{%
  \edef #1{\expandafter\strip@prefix
           \meaning #1}%
}
%</header>
%    \end{macrocode}
% \Finale
% \endinput
% \iffalse
%<*xkvex1>
\input xkeyval

\catcode`\@=11

%% xkeyval package: TeX example
%% Hendri Adriaens

\noindent This example demonstrates the use of the new macros for
setting keys of the `xkeyval' package. Have a look at the source for
information about this example and to understand the output.

%% Define keys.
\define@key{fami}{keyi}{key 1: #1 }
\define@key{famii}{keyii}{key 2: #1 }
\define@key{famiii}{keyiii}[test 2]{key 3: #1 }
\define@key{famiv}{keyi}{key 4: #1 }

%% Set keys from multiple families.
\setkeys{fami,famii,famiii}{keyi=test 1,keyiii}

%% Multiplely defined keys will be taken from the first family on the
%% list that defines the key. When + is used, all families
%% defining the key will set it.
\setkeys{fami,famiv}{keyi=test 3}\par
\setkeys{famiv,fami}{keyi=test 4}\par
\setkeys+{fami,famiv}{keyi=test 5}\par

%% Starred form sets keys that it can find in the families specified.
%% Keys that cannot be located will be put onto a list for later use
%% by \setrmkeys.
\setkeys*{famiv}{keyi=test 9,keyii=http://www.test10.com,
  keyiii=test 11}\par

%% Try to set the remaining keys from the previous step. The starred
%% version defines a new list in case there are still keys that cannot
%% be located in the given families. The command below does not do
%% anything since the `keyi' key has already been taken from family
%% `famiv' above.
\setrmkeys*{fami}\par

%% The normal version will complain (as \setkeys) when it cannot locate
%% a key which was previously submitted to \xsetkeys and hasn't been
%% set yet by previous commands. `xkeyval' commands can also be used to
%% set `keyval' options.
\setrmkeys{famii,famiii}\par

%% Set keys, excluding some keys.
\setkeys{fami,famii,famiii}[keyi,keyiii]{keyi=test 6,keyii=test
7,keyiii=test 8}\par

%% Test whether a key is defined in a family from the list.
\key@ifundefined{fami,famii}{keyiii}
{keyiii is not defined in family `fami' or `famii'}
{keyiii is defined in family `fami' or `famii'}

%% Define some keys and macros.
\define@cmdkey[my]{fama}{keya}[none]
\define@boolkey[my]{famb}{keyb}[true]
\def\testa{\setkeys[my]{fama}}
\def\testb{\setkeys[my]{famb}}
\def\testset{\setkeys[my]{fama,famb}}

%% Now \testa can only set keya, \testb can only
%% set keyb and \testset can set both.
\testa{keya}
  \my@fama@keya@cmd
\testb{keyb}
  \ifmy@famb@keyb true \else false \fi\par
\testset{keya=test 11,keyb=false}
  \my@fama@keya@cmd\space
  \ifmy@famb@keyb true \else false \fi

%% Pointer system

%% First we (re)define some keys.
\define@key{fam}{keyi}{key 1: #1 }
\define@key{fam}{keyii}{key 2: #1 }
\define@key{fam}{keyiii}[test 2]{key 3: #1 }

%% Pointers can be used to copy the value of other keys in the same
%% family. This can be done in two ways. The first example shows `on
%% the spot' value saving.
\setkeys{fam}{\savevalue{keyi}=test 12,\savevalue{keyii}=\usevalue{keyi}}
\setkeys{fam}{keyiii=\usevalue{keyii}}

%% However, you can also predefine the keys to be saved for the entire
%% document with a single command. Notice that in the example below, we
%% don't need the stars anymore.
\savekeys{fam}{keyi,keyii}
\setkeys{fam}{keyi=test 13,keyii=\usevalue{keyi}}
\setkeys{fam}{keyiii=\usevalue{keyii}}

%% Pointers can be used in default values as well. Remember that the
%% previous example used \savekeys for keyi and keyii.
\define@key{fam}{keyi}{keyi: #1 }
\define@key{fam}{keyii}[\usevalue{keyi}]{keyii: #1 }
\define@key{fam}{keyiii}[\usevalue{keyii}]{keyiii: #1 }
\setkeys{fam}{keyi=test 14}
\setkeys{fam}{keyii}
\setkeys{fam}{keyiii}

%% Presetting keys

%% Simple example of the preset system.
\define@key{fam}{keyi}{keyi: #1 }
\define@key{fam}{keyii}{keyii: #1 }
\presetkeys{fam}{keyi=blue}{keyii=\usevalue{keyi}}
\setkeys{fam}{}\par
\setkeys{fam}{keyi=red}\par
\setkeys{fam}{keyii=red}
\presetkeys{fam}{keyi=red}{}\par
\setkeys{fam}{keyii=\usevalue{keyi}}

\bye
%</xkvex1>
%
%<*xkvex2>
\documentclass{article}
\usepackage{xkeyval}
\usepackage{xcolor,calc}

%% Example of setting keyval options using xkeyval
\usepackage{hyperref}
\usepackage{helvet}
\setkeys{Hyp,Hel}{colorlinks,urlcolor=green,scaled=.85}

\title{\textsf{xkeyval} package\\\normalsize\emph{Example 1}}
\author{Hendri Adriaens}
\setlength{\parindent}{0cm}

\makeatletter

\begin{document}
\maketitle

This example demonstrates the use of the new macros for setting
keys of the \textsf{xkeyval} package. Below each piece of verbatim
code, you can find its output.

\section{Simple operations}

Define keys.
\begin{verbatim}
\define@key{fami}{keyi}{key 1: #1\space}
\define@key{famii}{keyii}{key 2: #1\space}
\define@key{famiii}{keyiii}[test 2]{key 3: #1\space}
\define@key{famiv}{keyi}{key 4: #1\space}
\end{verbatim}
\define@key{fami}{keyi}{key 1: #1\space}
\define@key{famii}{keyii}{key 2: #1\space}
\define@key{famiii}{keyiii}[test 2]{key 3: #1\space}
\define@key{famiv}{keyi}{key 4: #1\space}

Set keys from multiple families.
\begin{verbatim}
\setkeys{fami,famii,famiii}{keyi=test 1,keyiii}
\end{verbatim}
\begin{itemize}
\item[$\Rightarrow$]
\setkeys{fami,famii,famiii}{keyi=test 1,keyiii}
\end{itemize}

Multiplely defined keys will be taken from the first family on the
list that defines the key. When \verb|+| is used, all families
defining the key will set it.
\begin{verbatim}
\setkeys{fami,famiv}{keyi=test 3}
\setkeys{famiv,fami}{keyi=test 4}
\setkeys+{fami,famiv}{keyi=test 5}
\end{verbatim}
\begin{itemize}
\item[$\Rightarrow$]
\setkeys{fami,famiv}{keyi=test 3}\par
\setkeys{famiv,fami}{keyi=test 4}\par
\setkeys+{fami,famiv}{keyi=test 5}
\end{itemize}

Starred form sets keys that it can find in the families specified.
Keys that cannot be located will be put onto a list for later use
by \verb+\setrmkeys+.
\begin{verbatim}
\setkeys*{famiv}{keyi=test 9,keyii=\url{http://www.test10.com},
  keyiii=this key has been set on page \thepage}
\end{verbatim}
\begin{itemize}
\item[$\Rightarrow$]
\setkeys*{famiv}{keyi=test 9,keyii=\url{http://www.test10.com},
  keyiii=this key has been set on page \thepage}
\end{itemize}

Try to set the remaining keys from the previous step. The starred
version defines a new list in case there are still keys that
cannot be located in the given families. The command below does
not do anything since the \verb+keyi+ key has already been taken
from family \verb+famiv+ above.
\begin{verbatim}
\setrmkeys*{fami}
\end{verbatim}
\setrmkeys*{fami}\par

The normal version will complain (as \verb+\setkeys+) when it
cannot locate a key which was previously submitted to
\verb+\setkeys+ and hasn't been set yet by previous commands.
\textsf{xkeyval} commands can also be used to set \textsf{keyval}
options as is done to the color of the url created with the
\textsf{hyperref} package and the scaling of the font used for
`\textsf{xkeyval}' with the \textsf{helvet} package. (See the
preamble of the example for the code).
\begin{verbatim}
\setrmkeys{famii,famiii}
\end{verbatim}
\begin{itemize}
\item[$\Rightarrow$]
\setrmkeys{famii,famiii}
\end{itemize}

Set keys, excluding some keys.
\begin{verbatim}
\setkeys{fami,famii,famiii}[keyi,keyiii]{keyi=test 6,keyii=test 7,keyiii=test 8}
\end{verbatim}
\begin{itemize}
\item[$\Rightarrow$]
\setkeys{fami,famii,famiii}[keyi,keyiii]{keyi=test 6,keyii=test 7,keyiii=test 8}
\end{itemize}

Test whether a key is defined in a family from the list.
\begin{verbatim}
\key@ifundefined{fami,famii}{keyiii}
{keyiii is not defined in family `fami' or `famii'}
{keyiii is defined in family `fami' or `famii'}
\end{verbatim}
\begin{itemize}
\item[$\Rightarrow$]
\key@ifundefined{fami,famii}{keyiii}
{keyiii is not defined in family `fami' or `famii'}
{keyiii is defined in family `fami' or `famii'}
\end{itemize}

Define some keys and macros.
\begin{verbatim}
\define@cmdkey[my]{fama}{keya}[none]
\define@boolkey[my]{famb}{keyb}[true]
\def\testa{\setkeys[my]{fama}}
\def\testb{\setkeys[my]{famb}}
\def\testset{\setkeys[my]{fama,famb}}
\end{verbatim}
\define@cmdkey[my]{fama}{keya}[none]
\define@boolkey[my]{famb}{keyb}[true]
\def\testa{\setkeys[my]{fama}}
\def\testb{\setkeys[my]{famb}}
\def\testset{\setkeys[my]{fama,famb}}
Now \verb+\testa+ can only set \verb+keya+, \verb+\testb+ can only
set \verb+keyb+ and \verb+\testset+ can set both.
\begin{verbatim}
\testa{keya}
  \my@fama@keya@cmd
\testb{keyb}
  \ifmy@famb@keyb true \else false \fi
\testset{keya=test 11,keyb=false}
  \my@fama@keya@cmd\space
  \ifmy@famb@keyb true \else false \fi
\end{verbatim}
\begin{itemize}
\item[$\Rightarrow$]
\testa{keya}
  \my@fama@keya@cmd
\testb{keyb}
  \ifmy@famb@keyb true \else false \fi\par
\testset{keya=test 11,keyb=false}
  \my@fama@keya@cmd\space
  \ifmy@famb@keyb true \else false \fi
\end{itemize}

\section{Pointers and values}

First we (re)define some keys.
\begin{verbatim}
\define@key{fam}{keyi}{key 1: #1 }
\define@key{fam}{keyii}{key 2: #1 }
\define@key{fam}{keyiii}[test 2]{key 3: #1 }
\end{verbatim}
\define@key{fam}{keyi}{key 1: #1 }
\define@key{fam}{keyii}{key 2: #1 }
\define@key{fam}{keyiii}[test 2]{key 3: #1 }

Pointers can be used to copy the value of other keys in the same
family. This can be done in two ways. The first example shows `on
the spot' value saving.
\begin{verbatim}
\setkeys{fam}{\savevalue{keyi}=test 12,\savevalue{keyii}=\usevalue{keyi}}
\setkeys{fam}{keyiii=\usevalue{keyii}}
\end{verbatim}
\begin{itemize}
\item[$\Rightarrow$]
\setkeys{fam}{\savevalue{keyi}=test 12,\savevalue{keyii}=\usevalue{keyi}}
\setkeys{fam}{keyiii=\usevalue{keyii}}
\end{itemize}

However, you can also predefine the keys to be saved for the entire
document with a single command. Note that we force keys to be saved
globally. This is necessary here since the commands in the source of
this document appear in an environment.
\begin{verbatim}
\savekeys{fam}{\global{keyi},\global{keyii}}
\setkeys{fam}{keyi=test 13,keyii=\usevalue{keyi}}
\setkeys{fam}{keyiii=\usevalue{keyii}}
\end{verbatim}
\begin{itemize}
\item[$\Rightarrow$]
\savekeys{fam}{\global{keyi},\global{keyii}}
\setkeys{fam}{keyi=test 13,keyii=\usevalue{keyi}}
\setkeys{fam}{keyiii=\usevalue{keyii}}
\end{itemize}

Pointers can be used in default values as well. Remember that the
previous example used \verb+\savekeys+ for \verb+keyi+ and \verb+keyii+.
\begin{verbatim}
\define@key{fam}{keyi}{keyi: #1 }
\define@key{fam}{keyii}[\usevalue{keyi}]{keyii: #1 }
\define@key{fam}{keyiii}[\usevalue{keyii}]{keyiii: #1 }
\setkeys{fam}{keyi=test 14}
\setkeys{fam}{keyii}
\setkeys{fam}{keyiii}
\end{verbatim}
\begin{itemize}
\item[$\Rightarrow$]
\define@key{fam}{keyi}{keyi: #1 }
\define@key{fam}{keyii}[\usevalue{keyi}]{keyii: #1 }
\define@key{fam}{keyiii}[\usevalue{keyii}]{keyiii: #1 }
\setkeys{fam}{keyi=test 14}
\setkeys{fam}{keyii}
\setkeys{fam}{keyiii}
\end{itemize}

\section{Presetting keys}

Simple example of the preset system.
\begin{verbatim}
\define@key{fam}{keyi}{keyi: #1 }
\define@key{fam}{keyii}{keyii: #1 }
\presetkeys{fam}{keyi=blue}{keyii=\usevalue{keyi}}
\setkeys{fam}{}\par
\setkeys{fam}{keyi=red}\par
\setkeys{fam}{keyii=red}
\presetkeys{fam}{keyi=red}{}
\setkeys{fam}{keyii=\usevalue{keyi}}
\end{verbatim}
\begin{itemize}
\item[$\Rightarrow$]
\define@key{fam}{keyi}{keyi: #1 }
\define@key{fam}{keyii}{keyii: #1 }
\presetkeys{fam}{keyi=blue}{keyii=\usevalue{keyi}}
\setkeys{fam}{}\par
\setkeys{fam}{keyi=red}\par
\setkeys{fam}{keyii=red}
\presetkeys{fam}{keyi=red}{}\par
\setkeys{fam}{keyii=\usevalue{keyi}}
\end{itemize}

\section{Application of pointers, values and presets}

\setkeys{Hyp}{urlcolor=black}
More complicated example of the preset
system and pointer system. See for more information about this
example the
\textsf{xkeyval} article on\par
\url{http://stuwww.uvt.nl/~hendri/downloads/xkeyval.html}.
\begin{verbatim}
\newdimen\shadowsize
\define@boolkey{Fbox}{frame}[true]
\define@boolkey{Fbox}{shadow}[true]
\define@key{Fbox}{framecolor}{\def\Fboxframecolor{#1}}
\define@key{Fbox}{shadowcolor}{\def\Fboxshadowcolor{#1}}
\define@key{Fbox}{framesize}{\setlength\fboxrule{#1}}
\define@key{Fbox}{shadowsize}{\setlength\shadowsize{#1}}
\presetkeys{Fbox}{frame,framecolor=red,framesize=0.5pt}%
 {shadow=\usevalue{frame},shadowcolor=\usevalue{framecolor}!40,%
  shadowsize=\usevalue{framesize}*4}
\savekeys{Fbox}{frame,framecolor,framesize}
\newcommand*\Fbox[2][]{%
 \setkeys{Fbox}{#1}%
 {\ifKV@Fbox@frame\else\fboxrule0pt\fi
  \ifKV@Fbox@shadow\else\shadowsize0pt\fi
  \sbox0{\fcolorbox{\Fboxframecolor}{white}{#2}}%
  \hskip\shadowsize
  \color{\Fboxshadowcolor}%
  \rule[-\dp0]{\wd0}{\ht0+\dp0}%
  \llap{\raisebox{\shadowsize}%
   {\box0\hskip\shadowsize}}}%
}
\Fbox{demo1}
\Fbox[framecolor=blue]{demo2}
\Fbox[shadow=false]{demo3}
\Fbox[framesize=1pt]{demo4}
\Fbox[frame=false,shadow]{demo5}
\end{verbatim}
\newdimen\shadowsize
\define@boolkey{Fbox}{frame}[true]
\define@boolkey{Fbox}{shadow}[true]
\define@key{Fbox}{framecolor}%
 {\def\Fboxframecolor{#1}}
\define@key{Fbox}{shadowcolor}%
 {\def\Fboxshadowcolor{#1}}
\define@key{Fbox}{framesize}%
 {\setlength\fboxrule{#1}}
\define@key{Fbox}{shadowsize}%
 {\setlength\shadowsize{#1}}
\presetkeys{Fbox}%
 {frame,framecolor=red,framesize=0.5pt}%
 {shadow=\usevalue{frame},
  shadowcolor=\usevalue{framecolor}!40,
  shadowsize=\usevalue{framesize}*4}
\savekeys{Fbox}{frame,framecolor,framesize}
\newcommand*\Fbox[2][]{%
 \setkeys{Fbox}{#1}%
 {\ifKV@Fbox@frame\else\fboxrule0pt\fi
  \ifKV@Fbox@shadow\else\shadowsize0pt\fi
  \sbox0{\fcolorbox{\Fboxframecolor}{white}{#2}}%
  \hskip\shadowsize
  \color{\Fboxshadowcolor}%
  \rule[-\dp0]{\wd0}{\ht0+\dp0}%
  \llap{\raisebox{\shadowsize}%
   {\box0\hskip\shadowsize}}}%
}
\Fbox{demo1}
\Fbox[framecolor=blue]{demo2}
\Fbox[shadow=false]{demo3}
\Fbox[framesize=1pt]{demo4}
\Fbox[frame=false,shadow]{demo5}

\end{document}
%</xkvex2>
%
%<*xkvex3>
%% keyi will set \keyi in xkveca.cls
%% keyvi will set \keyvi in xkveca.cls and xkvesc.sty will pick
%% it up from \documentclass and redefine it
%% IgnoredGlobal and another=key will be considered as global
%% options to be passed on to packages. Not used anymore.
\documentclass[keyi=test1,keyvi,another=key,IgnoredGlobal,10pt]{xkveca}

%% keyii will set \keyii in xkvesa.sty
%% keyiv will be passed to xkvesb.sty as unknown option
%% the other input will be passed on to xkvesb as well.
\usepackage[keyii=test2,keyiv=test4,test5]{xkvesa}

%% keyvii will set \keyvii in xkvesc.sty
\usepackage[keyvii]{xkvesc}

\title{\textsf{xkeyval} package\\\normalsize\emph{Example 2}}
\author{Hendri Adriaens}
\setlength{\parindent}{0cm}

\makeatletter
\let\@tempa\@classoptionslist
\let\@tempb\@unusedoptionlist

\begin{document}
\maketitle

This example demonstrates the use of the new macros for declaring
class and package options of the \textsf{xkeyval} package. Please
study the files \verb+xkvex2.tex+, \verb+xkveca.cls+,
\verb+xkvecb.cls+, \verb+xkvesa.sty+, \verb+xkvesb.sty+ and
\verb+xkvesc.sty+ carefully to understand the output below.\\

Set by \verb+xkveca.cls+: \keyi\par
Set by \verb+xkvesa.sty+, copied from \verb+\documentclass+: \keyia\par
Set by \verb+xkvesa.sty+: \keyii\par
Set by \verb+xkvesb.sty+, passed on from \verb+xkvesa.sty+: \keyiii\par
Set by \verb+xkvesb.sty+, passed on as unknown key from \verb+xkvesa.sty+: \keyiv\par
Set by \verb+xkvesb.sty+, passed on as unknown from \verb+xkvesa.sty+: \keyv\par
Set by the class, copied by \verb+xkvesc.sty+: \keyvi\par
Set by \verb+xkvesc.sty+: \keyvii\par
Set by \verb+xkvecb.cls+, passed on from \verb+xkveca.cls+: \keyviii\\

The document class is: \XKV@documentclass\par
Unused global options: \@tempb\\

Notice the content of the following macros.\par
\verb+\@classoptionslist+: \@tempa\par
\verb+\XKV@classoptionslist+: \XKV@classoptionslist\\

This shows that \verb+key=value+ pairs have been deleted from the
class options list so that subsequent packages which are not using
\textsf{xkeyval} and are scanning this list will not run into
problems.\\

\end{document}
%</xkvex3>
%
%<*xkvex4>
%% Load LaTeX kernel macros patch
\RequirePackage{xkvltxp}

%% keyi will set \keyi in xkveca.cls
%% keyvi will set \keyvi in xkveca.cls and xkvesc.sty will pick
%% it up from \documentclass and redefine it
%% IgnoredGlobal and another=key will be considered as global
%% options to be passed on to packages. Not used anymore.
\documentclass[keyi=test1 on page \thepage,keyvi,another=key,IgnoredGlobal,10pt]{xkveca}

%% keyii will set \keyii in xkvesa.sty
%% keyiv will be passed to xkvesb.sty as unknown option
%% the other input will be passed on to xkvesb as well.
\usepackage[keyii=test2 on page \thepage,keyiv=test4 on page \thepage,test5]{xkvesa}

%% keyvii will set \keyvii in xkvesc.sty
\usepackage[keyvii]{xkvesc}

\title{\textsf{xkeyval} package\\\normalsize\emph{Example 3}}
\author{Hendri Adriaens}
\setlength{\parindent}{0cm}

\makeatletter
\let\@tempa\@classoptionslist
\let\@tempb\@unusedoptionlist
\let\@tempc\XKV@classoptionslist
\@onelevel@sanitize\@tempc

\begin{document}
\maketitle

This example demonstrates the use of the new macros for declaring
class and package options of the \textsf{xkeyval} package. Please
study the files \verb+xkvex2.tex+, \verb+xkveca.cls+,
\verb+xkvecb.cls+, \verb+xkvesa.sty+, \verb+xkvesb.sty+ and
\verb+xkvesc.sty+ carefully to understand the output below.\\

Set by \verb+xkveca.cls+: \keyi\par
Set by \verb+xkvesa.sty+, copied from \verb+\documentclass+: \keyia\par
Set by \verb+xkvesa.sty+: \keyii\par
Set by \verb+xkvesb.sty+, passed on from \verb+xkvesa.sty+: \keyiii\par
Set by \verb+xkvesb.sty+, passed on as unknown from \verb+xkvesa.sty+: \keyiv\par
Set by \verb+xkvesb.sty+, passed on as unknown from \verb+xkvesa.sty+: \keyv\par
Set by the class, copied by \verb+xkvesc.sty+: \keyvi\par
Set by \verb+xkvesc.sty+: \keyvii\par
Set by \verb+xkvecb.cls+, passed on from \verb+xkveca.cls+: \keyviii\\

The document class is: \XKV@documentclass\par
Unused global options: \texttt{\@tempb}\\

Notice the content of the following macros.\par
\verb+\@classoptionslist+: \texttt{\@tempa}\par
\verb+\XKV@classoptionslist+:\par\texttt{\@tempc}\\

This shows that \verb+key=value+ pairs have been deleted from the
class options list so that subsequent packages which are not using
\textsf{xkeyval} and are scanning this list will not run into
problems.\\

\newpage
This page shows that key values are only expanded when they are
actually used. They are not expanded during option processing.\\

\keyi\par
\keyii\par
\keyiv

\end{document}
%</xkvex4>
%
%<*xkveca>
\ProvidesClass{xkveca}
\usepackage{xkeyval}
\let\keyi\@empty
\let\keyvi\@empty
\DeclareOptionX{keyi}{\def\keyi{#1}}
%% this key has a default value
\DeclareOptionX{keyvi}[{test 6}]{\def\keyvi{#1}}
%% pass option to class xkvesb.cls
\PassOptionsToClass{notitlepage}{xkvecb}
\PassOptionsToClass{keyviii={test 8}}{xkvecb}
%% pass unknown options to article
\DeclareOptionX*{\PassOptionsToClass{\CurrentOption}{article}}
%% process options
\ProcessOptionsX
%% load another class
\LoadClass{xkvecb}
%</xkveca>
%
%<*xkvecb>
\ProvidesClass{xkvecb}
%% pass any unknown option to article
\DeclareOptionX{keyviii}{\def\keyviii{#1}}
\DeclareOptionX*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptionsX*
%% load article
\LoadClass{article}
%</xkvecb>
%
%<*xkvesa>
\ProvidesPackage{xkvesa}
\let\keyia\@empty
\let\keyii\@empty
%% keyi has been used in \documentclass
%% this key uses that information to set another macro
%% to do this, \ProcessOptionsX* is necessary.
%% the value from the \documentclass is copied.
\DeclareOptionX{keyi}{\def\keyia{#1}}
\DeclareOptionX{keyii}{\def\keyii{#1}}
%% pass any unknown keys to xkvesb.sty
\DeclareOptionX*{\PassOptionsToPackage{\CurrentOption}{xkvesb}}
%% pass a specific key=value to xkvesb.sty
\PassOptionsToPackage{keyiii={test 3}}{xkvesb}
%% process options. use the * to pick up options from \documentclass
\ProcessOptionsX*
\RequirePackage{xkvesb}
%</xkvesa>
%
%<*xkvesb>
\ProvidesPackage{xkvesb}
\let\keyiii\@empty
\let\keyiv\@empty
\let\keyv\@empty
%% use multiple families to define options
\DeclareOptionX[xkvesb]<famiii>{keyiii}{\def\keyiii{#1}}
\DeclareOptionX[xkvesb]<famiv>{keyiv}{\def\keyiv{#1}}
%% define \keyv as \CurrentOption
\DeclareOptionX*{\let\keyv\CurrentOption}
%% execute options in multiple families
\ExecuteOptionsX[xkvesb]<famiii>{keyiii={test 3}}
%% process options in multiple families
%% do not set keyiii
\ProcessOptionsX[xkvesb]<famiii,famiv>[keyiii]
%</xkvesb>
%
%<*xkvesc>
\ProvidesPackage{xkvesc}
%% redefine \keyvi when keyvi option is specified in \documentclass
\DeclareOption{keyvi}{\def\keyvi{test 6a}}
%% keyvii is a package specific key
\DeclareOption{keyvii}{\def\keyvii{test 7}}
%% note that this package uses original LaTeX macros for options.
%% this is possible since xkeyval filters key=value pairs from
%% the \documentclass options list.
\ProcessOptions
%</xkvesc>
%
%<*preamble>
\usepackage{url}
\usepackage{footmisc}
\usepackage{filecontents}
\usepackage{xkeyval}
\usepackage{xcolor}
\usepackage{listings}
\lstnewenvironment{command}{%
  \lstset{columns=flexible,frame=single,backgroundcolor=\color{yellow},%
    xleftmargin=\fboxsep,xrightmargin=\fboxsep,escapeinside=`',gobble=1}}{}
\lstnewenvironment{example}{%
  \lstset{basicstyle=\footnotesize\ttfamily,columns=flexible,frame=single,%
    backgroundcolor=\color{yellow!20},xleftmargin=\fboxsep,%
    xrightmargin=\fboxsep,gobble=1}}{}
\makeatletter
\def\tableofcontents{\@starttoc{toc}}
\def\changes@#1#2#3{%
  \protected@edef\@tempa{%
    \noexpand\glossary{\textbf{#1}\hfill\emph{(#2)}%
      \levelchar
      \ifx\saved@macroname\@empty
        \space\actualchar\generalname
      \else
        \expandafter\@gobble\saved@macroname
        \actualchar\string\verb\quotechar*%
        \verbatimchar\saved@macroname\verbatimchar
      \fi
      :\levelchar #3}%
    }%
  \@tempa\endgroup\@esphack
}
\def\eTeX{$\m@th\varepsilon$-\TeX}
\makeatother
\def\PrintChangesX{%
  \begingroup
  \let\efill\relax
  \setlength{\columnsep}{18pt}%
  \setlength{\columnseprule}{.4pt}%
  \PrintChanges
  \endgroup
}
\def\larg#1{{\ttfamily\char`\<}\meta{#1}{\ttfamily\char`\>}}
\def\pf#1{\textsf{#1}}
\def\bs{\symbol{'134}}
\def\ci#1{\texttt{\bs#1}}
\setlength{\parindent}{0pt}
\EnableCrossrefs
\RecordChanges
\GlossaryPrologue{\section*{Version history}}
\CodelineIndex
%</preamble>
%
%<*bib>
@MISC{LaTeXbase,
  author       = {Johannes Braams and David Carlisle and Alan Jeffrey and Leslie Lamport
                    and Frank Mittelbach and Chris Rowley and Rainer Sch\"opf},
  title        = {The {\LaTeXe} Sources},
  howpublished = {\url{CTAN:/macros/latex/base}},
  year         = 2003
}

@BOOK{companion,
  author       = {Frank Mittelbach and Michel Goossens and Johannes Braams and David Carlisle and Chris Rowley},
  title        = {The {\LaTeX} {C}ompanion, {S}econd {E}dition},
  year         = {2004},
  publisher    = {{Addison-Wesley}}
}

@MISC{keyval,
  author       = {David Carlisle},
  title        = {\pf{keyval} package, v1.13, 1999/03/16},
  howpublished = {\url{CTAN:/macros/latex/required/graphics}}
}

@MISC{pst-xkey,
  author       = {Hendri Adriaens},
  title        = {\pf{pst-xkey} package, v1.3, 2005/01/16},
  howpublished = {\url{CTAN:/macros/latex/contrib/xkeyval}}
}

@MISC{PSTricks,
  author       = {{Timothy Van} {Zandt et al.}},
  title        = {\pf{PSTricks} package, v1.04, 2004/06/22},
  howpublished = {\url{CTAN:/graphics/pstricks}}
}

@MISC{PSTricksWeb,
  author       = {Herbert Vo\ss},
  title        = {\pf{PSTricks} website},
  howpublished = {\url{http://www.pstricks.de}}
}
%</bib>
%
% \fi
