7

Im setting up a command with some options via l3keys. One of the options is tikz-options and it should take a list of TikZ options and pass them to a {tikzpicture}.

\documentclass{article}

\usepackage{tikz,xparse}

\ExplSyntaxOn

\keys_define:nn { mymodule } {
   tikz-options .tl_set:N = \l_mymodule_tikz_options_tl,
   tikz-options .initial:n = {},
}

\tikzset{
   every~my~image/.style = {
      fill = blue,
   }
}

\NewDocumentCommand{ \myimage }{ o }{
   \IfValueT { #1 } {
      \keys_set:nn { mymodule } { #1 }
   }
   \begin{tikzpicture}[every~my~image, \l_mymodule_tikz_options_tl]
      \filldraw (0,0) rectangle (2,2);
   \end{tikzpicture}
}

\ExplSyntaxOff

\begin{document}
\myimage

\myimage[tikz-options={fill=red,draw=black}]
\end{document}

The above example gives this error

! Package pgfkeys Error: I do not know the key '/tikz/fill=red,draw=black' and 
I am going to ignore it. Perhaps you misspelled it.

I guess there’s a problem with expansion, since TikZ considers the whole content of the token list as one option. How can I manually expand it?

6
  • Good question! I suspect something with .code=\pgfkeysalsofrom{...} -- I've done something similar with tcolorbox (which is tikz) and a \l_tmpa_tl. Perhaps, I'll find that!
    – user31729
    Commented Feb 5, 2016 at 20:32
  • TikZ has pgfkeys. That's a done deal.
    – percusse
    Commented Feb 5, 2016 at 20:41
  • What's the point of using l3keys here? You can just say \begin{tikzpicture}[every-my-image, #1] .... Even aside from the difficulties of combining l3 syntax with TikZ, why would you want to bring in an additional key-value system to no purpose?
    – cfr
    Commented Feb 5, 2016 at 22:19
  • 1
    \l_mymodule_tikz_options_tl is not a set of options, but it contains a set of options. No key-value based package does expansion when absorbing options.
    – egreg
    Commented Feb 5, 2016 at 22:40
  • @cfr: Yes in this example #1 would work but tikz-options is only one among other l3 options …
    – Tobi
    Commented Feb 6, 2016 at 0:01

1 Answer 1

8

You're not passing a set of options but something that contains a set of options. The error message is a bit misleading, because the expansion takes place when issuing the error, so it appears you have passed the right options.

No key-value based package performs expansion when absorbing key-value pairs, because it could be disastrous. So the solution is to expand the token list before passing it in the optional argument to \begin{tikzpicture}:

\documentclass{article}

\usepackage{tikz,xparse}

\tikzset{
   every my image/.style = {
      fill = blue,
   }
}

\ExplSyntaxOn

\keys_define:nn { mymodule }
 {
   tikz-options .tl_set:N = \l_mymodule_tikz_options_tl,
   tikz-options .initial:n = {},
 }

\NewDocumentCommand{ \myimage }{ O{} }
 {
   \group_begin:
   \keys_set:nn { mymodule } { #1 }
   \__tobi_start_tikzpicture:V \l_mymodule_tikz_options_tl
      \filldraw (0,0) rectangle (2,2);
   \end{tikzpicture}
   \group_end:
 }
\cs_new_protected:Nn \__tobi_start_tikzpicture:n
 {
   \begin{tikzpicture}[every~my~image,#1]
 }
\cs_generate_variant:Nn \__tobi_start_tikzpicture:n { V }

\ExplSyntaxOff

\begin{document}

\myimage

\myimage[tikz-options={fill=red,draw=black}]

\end{document}

enter image description here

3
  • I knew that it was an expansion problem ;-) but didn’t know how to expand the token list before passing it … thanks for your help!
    – Tobi
    Commented Feb 6, 2016 at 0:03
  • Out of curiosity does L3keys have something similar to <key>/.expanded = <value> handler?
    – percusse
    Commented Feb 6, 2016 at 6:49
  • 1
    @percusse One can use .code:n=\tl_set:Nx <tl-var> { #1 } or similar.
    – egreg
    Commented Feb 6, 2016 at 10:30

You must log in to answer this question.

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