4

I'm writing a package with two options; one of the defaults should change, depending on the other option. Currently, the definitions are:

\SetupKeyvalOptions{family=gitlog,prefix=gitLog@}
\DeclareBoolOption[true]{write}
\DeclareComplementaryOption{nowrite}{write}
\DeclareStringOption{bibfile}

Now, if the user provides either the write or the nowrite option, I want to obey that rigorously. If, however, neither option is given, then I want to default to write if bibfile is empty and nowrite otherwise.

My starter attempt is this (using xstring):

\IfStrEq{}{\gitLog@bibfile}{%
    \renewcommand{\gitLog@bibfile}{\jobname}
}{
    \somehowsetoption{nowrite} if neither write or nowrite given
}%

What's the simplest way to provide the \somehowsetoption command(s)? (Please note: changing the \Declare...s is OK.

I haven't included an MWE, as I think this is more about "how-to" rather than "what's-wrong".

1 Answer 1

3

You could use \AddToKeyvalOption{gitlog}{write}{...} to define some macro (the same for the nowrite option). Then said macro will only be defined if either option has been used and you can test for its existance. Here is a complete example that defines \gitLog@writeset only if either write or nowrite has been set (I use etoolbox instead of xstring – simply because I know it better):

\documentclass{article}
\usepackage{filecontents}

\begin{filecontents*}{foo.sty}
\RequirePackage{kvoptions,etoolbox}
\SetupKeyvalOptions{family=gitlog,prefix=gitLog@}

% write:
\DeclareBoolOption[true]{write}
\AddToKeyvalOption{gitlog}{write}{\def\gitLog@writeset{true}}

% nowrite:
\DeclareComplementaryOption{nowrite}{write}
\AddToKeyvalOption{gitlog}{nowrite}{\def\gitLog@writeset{true}}

% bibfile:
\DeclareStringOption{bibfile}

% process options:
\ProcessKeyvalOptions{gitlog}

% check if write/nowrite has been set:
\ifdef\gitLog@writeset
  {}
  {% write/nowrite option has not been set,
   % set boolean depending on value of \gitLog@bibfile:
    \ifdefempty\gitLog@bibfile
      {\booltrue{gitLog@write}}
      {\boolfalse{gitLog@write}}%
  }

\end{filecontents*}

% \usepackage{foo} % false
\usepackage[bibfile=foo]{foo} % true
% \usepackage[nowrite,bibfile=foo]{foo} % false
% \usepackage[write]{foo} % true

\begin{document}

\ifbool{gitLog@write}{true}{false}

\end{document}
2
  • Thank you so much. I took the liberty of fixing one small, though important, bug. (:-)) Commented Nov 19, 2015 at 19:10
  • @Brent.Longborough you're welcome :)
    – cgnieder
    Commented Nov 19, 2015 at 23:27

You must log in to answer this question.

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