4

I'm not sure if I'm taking it too far, but I think I've seen something like this around. This gives an error:

\documentclass{scrartcl}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand \RULE {}
  {
    \rule{\l_tmpa_dim}{\l_tmpb_dim}
  }
\keys_define:nn { foo / bar }
  {
    tmpa .dim_set:N = \l_tmpa_dim,
    tmpb .dim_set:N = \l_tmpb_dim,
    tmpa .initial:n = 1em,
    tmpb .initial:n = 4em,
  }
\NewDocumentCommand \dimsetup { m } { \keys_set:nn { foo } { #1 } }
\ExplSyntaxOff
\dimsetup{bar={tmpa=3em,tmpb=1em}}
\begin{document}
  \RULE
\end{document}

leads to

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! LaTeX error: "kernel/key-unknown"
! 
! The key 'foo/bar' is unknown and is being ignored.
! 
! See the LaTeX3 documentation for further information.
! 
! For immediate help type H <return>.
!...............................................

Is that how it's expected to be? How can I easily use this key-value inside a key (do I need to define bar so it passes the options?). I think I've seen this around, and I thought that this worked.

This is the “manual” workaround of what I want to achieve:

\dimsetup{bar/tmpa=3em,bar/tmpb=1em}
  • You're mistaking l3keys for pgfkeys. The / you use has no syntactical value; the class of the keys is foo/bar. – egreg Jul 17 '14 at 20:44
  • Right now I don't remember a question (I will look now) but I think I've seen something like that with l3keys. However, the documentation states that the whole name (if I understood correctly) in this case are foo/bar/tmpa and foo/bar/tmpb… why would it matter if I pass the / in \keys_set:nn or later? EDIT Here is one link, although I haven't looked thoroughly if it's the same case as mine. – Manuel Jul 17 '14 at 20:48
  • The link I shared gave me an idea… isn't this the same as when you pass true or false to a boolean operation in a key? – Manuel Jul 17 '14 at 20:53
  • That's the syntax for choice keys. – egreg Jul 17 '14 at 20:56
  • True, I thought it was general. – Manuel Jul 17 '14 at 21:03
3

You seem to want to use bar both as a subpath and as a key in itself. Something like

\documentclass{scrartcl}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand \RULE {}
  {
    \rule { \l_tmpa_dim } { \l_tmpb_dim }
  }
\keys_define:nn { foo }
  {
    bar .code:n = { \keys_set:nn { foo / bar } {#1} },
    bar / tmpa .dim_set:N = \l_tmpa_dim,
    bar / tmpb .dim_set:N = \l_tmpb_dim,
    bar / tmpa .initial:n = 1em,
    bar / tmpb .initial:n = 4em,
  }
\NewDocumentCommand \dimsetup { m } { \keys_set:nn { foo } { #1 } }
\ExplSyntaxOff
\dimsetup{bar={tmpa=3em,tmpb=1em}}
\begin{document}
  \RULE
\end{document}

will do that. You might want to look at key grouping in the l3keys docs, too.

Note that you could also do

\keys_define:nn { foo }
  {
    bar .code:n = { \keys_set:nn { foo / bar } {#1} },
  }
\keys_define:nn { foo / bar }
  {
    tmpa .dim_set:N = \l_tmpa_dim,
    tmpb .dim_set:N = \l_tmpb_dim,
    tmpa .initial:n = 1em,
    tmpb .initial:n = 4em,
  }

if you like. (In l3keys, the main power of / is in creating choice keys, etc.)

  • Your last edit was what I was trying (and it works), and that was what I thought was “automatic”, but it seems not :) However, / is not only to create choice keys, but also to structure keys, isn't it? I mean, whay I want is logical, without needing a choice key but setting some dimensions related to bar inside my “package” foo. – Manuel Jul 17 '14 at 21:00
  • @Manuel Just putting keys into a path doesn't make (each) part of that path into a key: you still have to define it. (Behind the scenes, there's a macro that contains the code for each key, and that is not the same as what is done to set up a path.) – Joseph Wright Jul 17 '14 at 21:03
  • @Manuel On the 'structure' part, that's the idea, yes. What I'm trying to get at, though, is that this is something that is expected for the programmer but doesn't actually have a relationship to how particular things are implemented, while for choices the fact that they are stored as key/choice-a is 'built-in'. – Joseph Wright Jul 17 '14 at 21:04
  • Nice. By the way, is there a clear reason to choose this behaviour vs., say, the pgfkeys one? (I don't know how pgfkeys work, but egreg noted that it works this way.) This was intended to come before your last comment. – Manuel Jul 17 '14 at 21:06
  • @Manuel pgfkeys is object-oriented, which l3keys isn't (see the fact we have the traditional define/set split). However, that doesn't prevent us (me) from implementing the approach you've asked about. That's more about the fact that we've added stuff to l3keys based on need/feature requests from users. Feel free to raise on LaTeX-L (mailing list for discussion about LaTeX): I'll certainly consider it if you can give some good use case examples :-) – Joseph Wright Jul 17 '14 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.