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


equivalent to

    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, 2016 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, 2016 at 17:19
  • xkeyval has a newer version, but the usage I apply in my answer does rely on that.
    – user31729
    May 13, 2016 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, 2016 at 17:38
  • 1
    @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, 2016 at 17:45

3 Answers 3


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.




  \expandafter\newif\csname ifmyclassname@#1\endcsname
    \expandafter\gdef\csname classoption@myclass@#2\endcsname{##1}
    \global\expandafter\csname myclassname@#1true\endcsname




\myclasssetup{keyB={upvote good answers!}, keyC={provide a MWE}}


Yes, it was specified
No, it is missing

Yes, it was specified and has the value \textbf{\classoption@myclass@KeyB}
No, it is missing


  • 1
    Thank you! I think you provide one that is closer to my need, and great MWE demonstration.
    – Spaceship
    May 13, 2016 at 17:38
  • @Spaceship: You're welcome. Happy TeXing
    – user31729
    May 13, 2016 at 17:39
  • Hi Christian, You got legendary when I was away? Congrats! :)
    – user11232
    May 15, 2016 at 2:45
  • @HarishKumar: Thank you very much! Yes, last week. You're back???
    – user31729
    May 15, 2016 at 6:24
  • @ChristianHupfer: Some what :) . What is the next badge? ^^
    – user11232
    May 16, 2016 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
  [2011/04/15 v1.7 Rerun checks for auxiliary files (HO)]

% Option setup

% The setup command (a convenience wrapper for `\setkeys`:

% The options

% Option processing: first configuration file, then package options

% Helper macro to disable options


% The options are disabled after they have been used the last time:


Using expkv-opt you can process package/class options with keys defined for expkv (possibly via the helper package expkv-def).

An example could be the following class-file stored as myclass.cls:



% define the keys (see expkv-def's documentation for an overview on key types)
     store key1 = \myclassA
    ,store key2 = \myclassB
    ,store key3 = \myclassC
% define the setup macro (will be defined protected here)

% process the options given to \documentclass

% lets base on article


If used together with this document:



You'll get

enter image description here

Please note that in any case (and with any package) the usage of \documentclass[<options>]{<class>} and \documentclass{<class>}\clssetup{<options>} is not the same. Options given to \documentclass are (normally) picked up by every package you use (and classes loaded with \LoadClass) whereas options given to \clssetup don't propagate that way. Also the options given to \documentclass will be expanded using \edef once (expkv-opt doesn't use the expanded option list, but the raw one provided by recent versions of LaTeX, still the keys will be expanded inside \edef for backwards compatibility), whereas \clssetup receives its options as-is.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .