This is a general and documented feature of latex3, but I shall still provide an MWE in order to illustrate the problem, see below.
Suppose I have a function which uses the l3keys key-value interface, and I want it to have reasonable defaults for when the user does not provide the key. l3keys provides the default
and initial
options, where default
is used when the key is given without a value and initial
is used to initialise the key. However, this initialisation happens exactly once, at the time the key is defined.
\documentclass{standalone}
\begin{document}
\ExplSyntaxOn
\keys_define:nn { mykeys } {
myvalue .int_set:N = \l_myvalue_int,
myvalue .initial:n = 0
}
\cs_new:Nn \show_zero_or_myvalue:n {
\keys_set:nn { mykeys } { #1 }
\int_use:N \l_myvalue_int
}
show_zero_or_myvalue:n {}
show_zero_or_myvalue:n { myvalue=5 }
show_zero_or_myvalue:n {}
\ExplSyntaxOff
\end{document}
As a user, with the function definition hidden in some package, I would expect this to result in the output 050
. Of course, once you read the l3keys documentation, you know that it actually prints 055
, because the key myvalue
is not set in the third call, so it retains its previous value.
However, there are situations where this is undesirable. Imagine you were drawing a picture with TikZ and the code
\tikz[color=black]{
% define some nodes
\draw[color=red] (a) -- (b);
\draw (b) -- (c);
}
resulted in two red lines.
Obviously, I can reset the key in the backend by hand, but since I am working with a large collection of keys that are read in multiple places, my current workaround is that before every call to \keys_set:nn { mykeys } { #1 }
, I issue a manual \keys_set:nn { mykeys } { ... }
, where ...
is a long, long list of keys.
Is there a better way to do this, either built into l3keys or by hooking into the set_keys
mechanism?
There have been questions on this topic a while ago, but none with a definitive answer. Since latex3 is still being developed, I think asking again is reasonable.
Related: if there is no built-in way, what is the standard mechanism to propose the inclusion of this feature? Evidently I am not the only person who would like to see something like this.
\path
begins and;
ends a group.