How can I define a key-value command like \hypersetup?
Examples for kvoptions
among my packages: accsupp
, attachfile2
, bookmark
, enparen
, epstopdf-base
, grffile
, pagegrid
, pmboxdraw
, rerunfilecheck
, resizegather
, selinput
, zref-xr
, ...
A grep -r kvoptions TDS:tex/latex
will reveal much more packages.
Example rerunfilecheck
:
% Package start
[...]
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{rerunfilecheck}%
[2011/04/15 v1.7 Rerun checks for auxiliary files (HO)]
% Option setup
\RequirePackage{kvoptions}[2010/02/22]
\SetupKeyvalOptions{%
family=rerunfilecheck,%
prefix=ReFiCh@%
}
% The setup command (a convenience wrapper for `\setkeys`:
\newcommand*{\RerunFileCheckSetup}{%
\setkeys{rerunfilecheck}%
}
% The options
\DeclareBoolOption{mainaux}
\DeclareBoolOption{partaux}
\DeclareBoolOption{starttoc}
\DeclareBoolOption{index}
\DeclareBoolOption{glossary}
\define@key{rerunfilecheck}{aux}[true]{%
\RerunFileCheckSetup{%
mainaux={#1},%
partaux={#1},%
starttoc={#1},%
index={#1},%
glossary={#1}%
}%
}
% Option processing: first configuration file, then package options
\InputIfFileExists{rerunfilecheck.cfg}{}{}
\ProcessLocalKeyvalOptions*
% Helper macro to disable options
\def\ReFiCh@DisableOption{%
\DisableKeyvalOption[%
action=warning,%
package=rerunfilecheck%
]{rerunfilecheck}%
}
[...]
% The options are disabled after they have been used the last time:
\ReFiCh@DisableOption{mainaux}
\ReFiCh@DisableOption{partaux}
\ReFiCh@DisableOption{starttoc}
\ReFiCh@DisableOption{index}
\ReFiCh@DisableOption{glossary}
\ReFiCh@DisableOption{aux}
[...]
Here's a xkeyval
version, with automatic definition of keys and it's keyval
macro and a corresponding \if...
whether the option is used.
The precise implementation depends on the internal options to be specified what they should do if specified. I assumed, that the class options are to be stored to a macro, say \classoption@myclass@#2
where #2
has the relevant meaning, say paper
or width
etc.
\documentclass{article}
\usepackage{xkeyval}
\makeatletter
\newcommand{\definemykey}[2]{%
\expandafter\newif\csname ifmyclassname@#1\endcsname
\define@key{myclass}{#1}{%
\expandafter\gdef\csname classoption@myclass@#2\endcsname{##1}
\global\expandafter\csname myclassname@#1true\endcsname
}
}
\definemykey{keyA}{KeyA}
\definemykey{keyB}{KeyB}
\definemykey{keyC}{KeyC}
\newcommand{\myclasssetup}[1]{%
\setkeys{myclass}{#1}%
}
\makeatother
\myclasssetup{keyB={upvote good answers!}, keyC={provide a MWE}}
\begin{document}
\makeatletter
\ifmyclassname@keyA
Yes, it was specified
\else
No, it is missing
\fi
\ifmyclassname@keyB
Yes, it was specified and has the value \textbf{\classoption@myclass@KeyB}
\else
No, it is missing
\fi
\makeatother
\end{document}