1

The source document below first writes two packages, each using keys and kvoptions; the first package passes a boolean option to the second and calls the second.

Despite the preamble line

\usepackage[whether=true]{pkg}

in the source document below, the output is always It's false, and I see this error in the log:

Package more Warning: Value `PKG@whether' is not supported by
(more)                option `whether' on input line 9.

What's wrong?

\RequirePackage{filecontents}
\begin{filecontents}{pkg.sty}
\RequirePackage[debugshow]{kvoptions}
\SetupKeyvalOptions{family=PKG,prefix=PKG@}
\RequirePackage{ifthen}
\DeclareBoolOption{whether}
\ProcessKeyvalOptions*
\PassOptionsToPackage{whether=PKG@whether}{more}
\RequirePackage{more}
\end{filecontents}
%%%%
\begin{filecontents}{more.sty}
\RequirePackage[debugshow]{kvoptions}
\SetupKeyvalOptions{family=MORE,prefix=MORE@}
\RequirePackage{ifthen}
\DeclareBoolOption{whether}
\ProcessKeyvalOptions*
\ifthenelse{\boolean{MORE@whether}}
  {\newcommand{\sayit}{it's true}}
  {\newcommand{\sayit}{it's false}}
\end{filecontents}
%%%%

\documentclass{article}

\usepackage[whether=true]{pkg}

\begin{document}
\sayit
\end{document}

Note: Of course, the preceding is a MWE of my real situation, where the packages do useful things. More importantly, both real packages take additional boolean keys as well as several string keys.

One workaround: The following cures the problem but seems overly complicated to me. Replace the line

\PassOptionsToPackage{whether=PKG@whether}{more}

with...

\ifthenelse{\boolean{PKG@whether}}{\newcommand{\newwhether}{true}}{\newcommand{\newwhether}{false}}
\PassOptionsToPackage{whether=\newwhether}{more}

... or, in the first of these two lines, a corresponding \if ... \fi construction.

Is there a simpler, more direct, way, avoiding the need to make a new conditional command — given still that in the actual situation each package will have multiple boolean and string key options?

4
  • My guess is that \PKG@whether is not a “string” containing true or false, but rather a boolean flag. Jul 10, 2019 at 19:36
  • Can’t you just do \ifPKG@whether \PassOptionsToPackage{whether=true}{more} \fi (note the default boolean is false)? Jul 10, 2019 at 19:42
  • @RuixiZhang:i I don't want to use the \ifPKG@whether...\fi work-around is that it would be quite awkward in my actual situation, since both packages will have additional Boolean options as well as several string options.
    – murray
    Jul 10, 2019 at 20:21
  • @RuixiZhang: Since multiple \PassOptionsToPackage commands may be used, so may multiple \ifPKG@boolOptName \PassOptionsToPackage ... \fi ` commands be used. So please make your 2nd comment above into an answer! It seems to most easily suit my needs.
    – murray
    Jul 11, 2019 at 0:54

2 Answers 2

1

Converting comments into an answer as requested.

When you \DeclareBoolOption{whether} in pkg.sty with prefix PKG@, it creates a boolean flag \ifPKG@whether and sets \PKG@whetherfalse by default. There are no “strings” such as true or false created.

I assumed the reasons you used the \PassOptionsToPackage{...}{more} \RequirePackage{more} construct was that you had multiple options to be passed to more.sty. We want to avoid \RequirePackage[option]{more} \RequirePackage[otheroption]{more} because this will produce “conflicting options” error.

It is perfectly fine to use the already created flags and to call \PassOptionsToPackage multiple times. Say, you have booleans whether and other in more.sty:

\begin{filecontents}{more.sty}
\PassOptionsToPackage{debugshow}{kvoptions}% changed from \RequirePackage[debugshow]{kvoptions}
\RequirePackage{kvoptions}
\SetupKeyvalOptions{family=MORE,prefix=MORE@}
\DeclareBoolOption{whether}
\DeclareBoolOption[true]{other}% default true
\ProcessKeyvalOptions*
...
\end{filecontents}

You can structure your pkg.sty as follows:

\begin{filecontents}{pkg.sty}
\PassOptionsToPackage{debugshow}{kvoptions}% changed from \RequirePackage[debugshow]{kvoptions}
\RequirePackage{kvoptions}
\SetupKeyvalOptions{family=PKG,prefix=PKG@}
\DeclareBoolOption{whether}
\DeclareBoolOption{other}% default false
\ProcessKeyvalOptions*

\ifPKG@whether
  \PassOptionsToPackage{whether=true}{more}
\fi

\ifPKG@other
\else
  \PassOptionsToPackage{other=false}{more}
\fi

\RequirePackage{more}
\end{filecontents}

You may use \csname [email protected]\endcsname in the body to check the option list of more.sty.

2

A bool key defines a boolean. For a command you would need something like the choice keys of l3keys.

But you can use the boolean directly in the \RequirePackage:

\RequirePackage[whether=\ifPKG@whether true\else false \fi]{more}

In \PassOptionsToPackage the argument is not expanded, so you would need the standard \next method:

\edef\next{%
 \noexpand\PassOptionsToPackage{whether=
         \ifPKG@whether true\else false \fi }{more}}

\next
7
  • The latter does work in my situation (which will involve intermediate packages, so that the first package pkg will not directly call the last one more). Is that inherently simpler than the "One workaround" I found and added to the end of my post?
    – murray
    Jul 10, 2019 at 21:25
  • Is this OK instead? \ifPKG@whether \def\newwhether{true} \else \def\newwhether{false}\fi and then \PassOptionsToPackage{whether=\newwhether}{more} That is, am I overlooking something problematic with this method?
    – murray
    Jul 10, 2019 at 21:38
  • @murray But this would waste token space for the temporary \newwhether and is no different from \ifPKG@whether \PassOptionsToPackage{whether=true}{more} \fi Jul 10, 2019 at 21:40
  • well you should better not use \def without a \newcommand first to check if it is already defined unless you know exactly that the command is specific to your package - which is normally not the case with command containing only letters. Jul 10, 2019 at 21:45
  • @RuixiZhang: But in my actual situation I cannot easily use the \ifPKG@whether\PassOptionsToPackage{whether=true}{more} \fi method when I'll have multiple boolean keys. Or can I use multiple \PassOptionsToPackage commands, one for each such key?
    – murray
    Jul 10, 2019 at 22:00

You must log in to answer this question.

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