如何将一组 pgfkey 放入... pgfkey

如何将一组 pgfkey 放入... pgfkey

我想定义一个可以包含一些其他 pgfkey 的 pgfkey,我可以将其作为输入提供给另一个节点。

这是 MWE。第一个函数myRectangle可以工作,但不太实用。第二个函数更容易阅读……但不起作用:

% \RequirePackage{atbegshi}\AtBeginShipoutInit
\documentclass{article}
\usepackage{tikz}

\usepackage{pgfkeys}
% Define the used keys in the following document
\pgfkeys{
  /myKeys/.is family, /myKeys,
  default/.style =
  {
    width  = 1cm,
    my tikz commands={},
  },
  width/.estore in = \kWidth,
  my tikz commands/.estore in = \kTikzCommands
}

% This work, but is dirty
\newcommand{\myRectangle}[1][]{%
  \pgfkeys{/myKeys, default, #1}%
  \edef\test{\noexpand\node [draw, minimum width=\kWidth, inner sep=0pt, shape=rectangle, very thick, color=red, minimum height=1cm, \kTikzCommands] at (current page.center) {};}
  \begin{tikzpicture}[remember picture,overlay]%
    \test %
  \end{tikzpicture}%
}

% This does not work, and I'd like to make it work
\newcommand{\myRectangleB}[1][]{%
  \pgfkeys{/myKeys, default, #1}%
  \begin{tikzpicture}[remember picture,overlay]%
    \node [draw, minimum width=\kWidth, inner sep=0pt, shape=rectangle, very thick, color=red, minimum height=1cm, \kTikzCommands] at (current page.center) {};
  \end{tikzpicture}%
}


\begin{document}

% Ok
\myRectangle[width=10cm,my tikz commands={fill=blue, dashed}]
% No ok
\myRectangleB[width=10cm,my tikz commands={fill=blue, dashed}]

\end{document}

错误如下:

ERROR: Package pgfkeys Error: I do not know the key '/tikz/fill=blue, dashed' and I am going to ignore it. Perhaps you misspelled it.

有什么想法可以使其更具可读性吗?

谢谢。

答案1

使用样式而不是宏怎么样?

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfkeys}
\pgfkeys{
  /myKeys/.is family, /myKeys/.cd,
  default/.style={
    width= 1cm,
    my tikz commands={},
  },
  width/.estore in=\kWidth,
  kTikzCommands/.style={},
  my tikz commands/.style={
    kTikzCommands/.style={#1}
  }
}
\newcommand{\myRectangleB}[1][]{%
  \pgfkeys{/myKeys, default, #1}%
  \begin{tikzpicture}[remember picture,overlay]%
    \node [draw, minimum width=\kWidth, inner sep=0pt, 
      shape=rectangle, very thick, color=red, minimum height=1cm, 
        /myKeys/kTikzCommands/.try] at (current page.center) {};
  \end{tikzpicture}%
}
\begin{document}
\myRectangleB[width=10cm,my tikz commands={fill=blue, dashed}]
\end{document}

相关内容