# How can I define a key-value command like \hypersetup?

I am quite familiar with switch (\if...\else...\fi) now and read many questions on key-value topic, but I cannot find an example to create the options listing command like \hypersetup, to make

\documentclass[key1=...,key2=...,key3=...]{myclass}


equivalent to

\documentclass{myclass}
\myclssetup{%
key1 = ...,
key2 = ...,
key3 = ...,
.............
}


None of these keys are boolean, and all these values helps me to do switch into different layout schemes. For instance, key1 is to set one type of cover page and key2 is to set one type of section formats. I do not need to draw something or to set some geometry properties (I don't need values of numerical parameters). I also want some default options pre-set, if the user didn't specify their options.

If I use the kvoptions package, all I know is that I probably need \newif and \DeclareVoidOption (kvoptions package) to create options (values), \define@key (keyval package) and \kvsetkeys command (keyval package) to link values and keys. However, I have been struggling to find similar and simple examples to achieve the function I need.

I think it's important to stress that I am writing a class (not package), and it will be based on another class (like book).

• Do you stick to kvoptions? – user31729 May 13 '16 at 17:15
• @Christian Hupfer No, if it's xkeyval or something else, is all right. But I need to consider some compatibility for users who use old version of TeX distribution (maybe TeX Live 2011/12) – Spaceship May 13 '16 at 17:19
• xkeyval has a newer version, but the usage I apply in my answer does rely on that. – user31729 May 13 '16 at 17:30
• l3keys is nice too, but since you're referring to an older TL version, it's perhaps not the correct tool then – user31729 May 13 '16 at 17:38
• @Christian Hupfer I know that one. Maybe later I would upgrade my class by programming in LaTeX3, with expl3 and others. But since I have a lot to learn even in basic TeX programming and macro manipulation, right now it's probably better to stick to LaTeX2e.... – Spaceship May 13 '16 at 17:45

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}

• Thank you! I think you provide one that is closer to my need, and great MWE demonstration. – Spaceship May 13 '16 at 17:38
• @Spaceship: You're welcome. Happy TeXing – user31729 May 13 '16 at 17:39
• Hi Christian, You got legendary when I was away? Congrats! :) – user11232 May 15 '16 at 2:45
• @HarishKumar: Thank you very much! Yes, last week. You're back??? – user31729 May 15 '16 at 6:24
• @ChristianHupfer: Some what :) . What is the next badge? ^^ – user11232 May 16 '16 at 2:07

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}

[...]