This is a followup to my previous question Why does pgfkeys .initial not work in these cases?. In that question I created a macro with a key-value interface using pgfkeys
and it works perfectly. Now, I'm implementing the same options with a package such that when the package is loaded with the appropriate option, the functionality of the macro in the previous question is replicated.
With a macro, if I invoke that macro without providing any options, I can invoke the key's .initial
handler, fetch the key's value with \pgfkeysvalueof{...}
and do what needs to be done with no problems. In other words, the macro "knows" what to do if no options are provided. My problem is that I can't seem to replicate that functionality with the package example below.
If I load the package without any keys using \usepackage{selectunits}
I expect the package to behave as though [sunits=alternate]
had been given at load time, which I cannot get to work. Every other possible key-value combination seems to work perfectly. I can almost get everything to work perfectly by adding sunits=alternate,
inside the \pgfkeys{...}
blocks (presently commented out in the MWE), but this causes that key and value to always be used regardless of any other keys and values provided when the package is loaded.
So ultimately the question is: Is there a way to make \usepackage{selectunits}
, with no keys used, work as though sunits=alternate
had been given at load time, preferably without more than one key-value combination being invoked? It should also work with either of the two \pgfkeys{...}
blocks.
Here is the MWE:
% !TEX TS-program = lualatexmk
% !TEX encoding = UTF-8 Unicode
\documentclass{article}
\begin{filecontents}[overwrite,noheader]{selectunits.sty}
% !TEX encoding = UTF-8 Unicode
\ProvidesPackage{selectunits}[2021-01-22 v1.0 Example package]
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{pgfopts}
\newcommand*{\perpusebaseunits}{\typeout{You'll get base units.}}
\newcommand*{\perpusederivedunits}{\typeout{You'll get derived units.}}
\newcommand*{\perpusealternateunits}{\typeout{You'll get alternate units.}}
% Either one of the two following blocks should work.
% This block does not use .is choice
\pgfkeys{%
/selectunits/.is family, /selectunits,%
sunits/.initial=alternate,%
sunits/.default=derived,%
sunits/.code={%
\ifcsname perpuse#1units\endcsname
\csname perpuse#1units\endcsname
\else
\GenericError{}
{\MessageBreak settheunits: Illegal key value}
{Key 'sunits' can only base, derived, or alternate.}
{Read the documentation for help.}
\fi
},%
%sunits=alternate, % almost works, key used even in the presence of other keys
}%
% This block uses .is choice
%\pgfkeys{%
% /selectunits/.is family, /selectunits,%
% sunits/.is choice,%
% sunits/.initial=alternate,%
% sunits/.default=derived,%
% sunits/base/.code={\perpusebaseunits},%
% sunits/derived/.code={\perpusederivedunits},%
% sunits/alternate/.code={\perpusealternateunits},%
% %sunits=alternate, % almost works, key used even in the presence of other keys
%}%
\ProcessPgfPackageOptions{/selectunits}
\NewDocumentCommand{\selectunitssetup}{ o }{%
\IfValueTF {#1}
{ \pgfqkeys{/selectunits}{#1} }
{ \csname perpuse\pgfkeysvalueof{/selectunits/sunits}units\endcsname }
}%
\end{filecontents}
\usepackage{selectunits} % should set alternate units, doesn't work
%\usepackage[sunits]{selectunits} % should set derived units, works
%\usepackage[sunits=base]{selectunits} % should set base units, works
%\usepackage[sunits=derived]{selectunits} % should set derived units, works
%\usepackage[sunits=alternate]{selectunits} % should set alternate units, works
%\usepackage[sunits]{selectunits} % should set derived units, works
%\usepackage{selectunits} % should set alternate units, doesn't works
%\usepackage[sunits=blubb]{selectunits} % should throw an error and stop, works
\begin{document}
Hello.
\selectunitssetup\par % should set alternate units, works
\selectunitssetup[sunits]\par % should set derived units, works
\selectunitssetup[sunits=base]\par % should set base units, works
\selectunitssetup[sunits=derived]\par % should set derived units, works
\selectunitssetup[sunits=alternate]\par % should set alternate units, works
\selectunitssetup[sunits]\par % should set derived units, works
\selectunitssetup\par % should set alternate units, works
%\selectunitssetup[sunits=blubb]\par % should throw an error and stop, works
\end{document}