Fist, note that /testclass/
is not the same as testclass/
. So you at least need
\pgfkeys{%
/testclass/testkey/.initial=testkeyinitial,
/testclass/.is family,
}
Second, I would recommend defining and handling all options before \LoadClass
. This is because in a real parasitic class, you generally want to pass all unknown options to the base class. pgfopts
doesn't seem to care two hoots about this, which surprised me, both other methods of handling options do care and it is probably good housekeeping to stick to the usual pattern unless there is compelling reason to do otherwise. (That is, it makes me nervous!)
With the path suitably corrected, the code you are using would work if you were dealing with package options, but does not work when used for class options.
Using .store in
instead resolves the problem.
\begin{filecontents}{\jobname.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{\jobname}[2016/12/04 A Test Class]
\RequirePackage{pgfkeys}
\RequirePackage{pgfopts}
\pgfkeys{%
/testclass/.is family,
/testclass,
testkey/.store in=\optiontest,
testkey=testkeyinitial,
}
\ProcessOptions
\ProcessPgfOptions{/testclass}
\LoadClass{article}
\end{filecontents}
\documentclass[testkey=works]{\jobname}
\begin{document}
Test: \optiontest
\end{document}
Why does the original method not work?
I am not certain about the following. However, I think that what happens is that pgfopts
relies on testing for <key path>/<key name>/.@cmd
in the case of class options (see p. 4 of the manual). It does this so that it can ignore options which are not relevant and which should only be processed by the class itself in the normal way. Otherwise, 12pt
say or a4paper
would be passed to pgfkeys
for processing.
However, .initial
doesn't define a key as such. Rather it defines a key handler with the relevant name. Hence, there is no <key path>/<key name>/.@cmd
and the option is assumed to be one which should be passed to the class rather than processed by pgfopts
.
Note that even when the key is processed correctly because it is a regular key rather than a handler, the class will still complain that the option is unknown. pgfopts
shares this feature with similar key-handling packages such as l3keys2e
which process the options correctly but do not prevent LaTeX warning that they are unknown and unprocessed. As far as LaTeX is concerned, it appears that here is processing and then there is being-seen-to-be-processing ....