0

I'm writing my first LaTeX class. After some research I've found that kvoptions seems to be the most recommended for key value class options, and etoolbox is recommended over ifthen for conditionals.

What's mystifying me is how one conditionally defines commands/passes options to packages based on the value of a kvoptions string option. Unfortunately I dont really have examples of what I've tried as I don't know where to start combining these two.

If there's another more ergonomic combination that can achieve this goal I'm open to it. I'm not particularly attached to either package. Any pointers would be hugely appreciated.

5
  • Have you given a glance on texdoc kvoptions's manual, in "sec.3 Example"? You may find example such as \DeclareBoolOption{print} and \ifMCS@print, which seemed highly related to your question statement. Commented Aug 31 at 10:03
  • 1
    Could you provide a bit of starting code to work with, a simple class with one or two options, and a command that you want to define conditionally? Commented Aug 31 at 10:03
  • 2
    Don't use kvoptions in this day and age. Commented Aug 31 at 11:36
  • 1
    It would be easier to help you if you provided a bit more context (a small MWE, showing what you try to do, even if it doesn't fully work). Commented Aug 31 at 11:37
  • do not use kvoptions for a new class. see texdoc clsguide for the built in latex option handler. similarly you do not need etoolbox, latex has better conditionals built in. Commented Aug 31 at 15:42

2 Answers 2

3

Provided that you need both, evaluation whether some special value is set, and at the same time store the value, you can use expkv-opt and the defining front end in expkv-def (disclaimer: I'm the author of those packages).

We set up the key to store every value inside \myopt, additionally we specify what to do for specific values (and to ignore unknown choices for the choice-code behaviour).

I use expkv-def for its ability to overload keys with several types using the also prefix.

\begin{filecontents*}[overwrite]{\jobname.cls}
\RequirePackage{expkv-def,expkv-opt}
\ProvidesClass{\@currname}[2025-08-31 v0.0 an adhoc class for an MWE]

\newcommand\mysimplebool[2]{#2} % this is basically 'false'

% forward all options to our base class
\LoadClassWithOptions{article}

\ekvdefinekeys{\@currname}
  {
    % general behaviour of storing inside a variable
             store  myopt = \myopt
    % handling of a select few special values
    ,also    choice myopt =
      {
         special-value = \let\mysimplebool\@firstoftwo
        ,other-value   = \let\mysimplebool\@secondoftwo
      }
    % ignore unknown choices
    ,unknown-choice myopt = {}
  }
\ekvoProcessGlobalOptions{\@currname}

% additional setup macro to set keys outside of the optional argument
\protected\ekvsetdef\myclssetup{\@currname}

\end{filecontents*}

\documentclass[12pt,myopt=special-value]{\jobname}

\begin{document}
\mysimplebool{true}{false}

We also stored the value: \myopt.
\end{document}

enter image description here


If you don't need to actually store the value but only want to accept specific ones, simply drop the store and the also prefix (and if you want errors to be thrown drop unknown-choice as well).

Or, if you don't need the overloading functionality, you might want to consider the kernel's key=value interface. Search for ltkeys or l3keys, there should be questions on the site about those.

2

If you prefer a no-package solution you can use the built in key=value and options handler. Output looks as in my other answer.

\begin{filecontents*}[overwrite]{\jobname.cls}
\ProvidesExplClass{\@currname}
  {2025-08-31} {0.0} {an adhoc class for an MWE}

\DeclareKeys
  {
     myopt .tl_set:N = \myopt
  }

% forward all unknown options to our base class
\DeclareUnknownKeyHandler{\PassOptionsToClass{\CurrentOption}{article}}

\ProcessKeyOptions

\LoadClass{article}

\NewExpandableDocumentCommand \mysimplebool { m m }
  { \str_if_eq:VnTF \myopt { special-value } {#1} {#2} }
\end{filecontents*}

\documentclass[12pt,myopt=special-value]{\jobname}

\begin{document}
\mysimplebool{true}{false}

We also stored the value: \myopt.
\end{document}
2
  • Why this works with no \ExplSyntaxOn/Off? Commented Sep 2 at 18:42
  • 1
    @yannisl because \ProvidesExplClass is an implicit \ExplSyntaxOn that automatically also issues \ExplSyntaxOff at the end of that file. Commented Sep 2 at 21:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.