通过家族继承 PGF 密钥/设置多个值

通过家族继承 PGF 密钥/设置多个值

我正在尝试创建一个用于绘制各种事物的库,并且我希望能够设置 PGF 键,以便同时存在顶级设置系列和模块级设置。如果和中都存在键名/my library/my library/module我希望模块键自动继承库键中包含的值。

因此,例如,/my library/包含一个名为 的键label properties,用于设置所有标签的默认样式。 的值/my library/module/label properties应以 的(扩展?)值作为前缀/my library/label properties

下面的 MWE 已经完成部分了,但是因为我无法让模块键继承库键的值,所以它实际上什么也没做。

    % arara: lualatex: { shell: true }
\documentclass{article}
\usepackage{tikz}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{etoolbox}

% Sets various configuration parameters
% \strandlibset[module]{options as pgfkeys}
\NewDocumentCommand{\mylibraryset}{o m}{
    \IfNoValueTF{#1}{
        \pgfqkeys{/my library}{#2}
    }
    {
        \pgfqkeys{/my library/#1}{#2}
    }
}
\pgfkeys{
    /my library/.is family,
    my library,
    label properties/.style={node font=\bfseries},
    % ... Other top-level keys, some of which should set TikZ options
}

\pgfkeys{
    /my library/module/.is family,
    /my library/module,
    label properties/.initial=, % Inherits the value of /my library/label style/ and prepends that to the key-value list
    % ... Other keys specific to <module>
}

\begin{document}
\begin{tikzpicture}
    \path (0, 0) node[\pgfkeysvalueof{/my library/module/label properties}] {Test node with default options};
    \mylibraryset[module]{label properties={blue, draw=black, fill=black!10!white}}
    \path (0, -1) node[\pgfkeysvalueof{/my library/module/label properties}] {Test node with extra options};
\end{tikzpicture}
\end{document}

如果我在密钥中设置多个选项,这个相当做作的 MWE 根本无法编译,label style因为密钥在使用时不会自动扩展。手册详细介绍了各种密钥处理程序,但提供的示例太基础,无法清楚地表明到底发生了什么。我想我正在寻找的是.style,.add code处理程序,但我并不完全确定。

按照薛定谔的猫的回答,改变键的定义似乎可以解决问题:

% arara: lualatex: { shell: true }
\documentclass{article}
\usepackage{tikz}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{etoolbox}

% Sets various configuration parameters
% \strandlibset[module]{options as pgfkeys}
\NewDocumentCommand{\mylibraryset}{o m}{
    \IfNoValueTF{#1}{
        \pgfqkeys{/my library}{#2}
    }
    {
        \pgfqkeys{/my library/#1}{#2}
    }
}
\pgfkeys{
    /my library/.is family,
    my library,
    label properties/.style={red, node font=\bfseries},
    % ... Other top-level keys, some of which should set TikZ options
}

\pgfkeys{
    /my library/module/.is family,
    /my library/module,
    label properties/.style=/my library/label properties, % Inherits the value of /my library/label properties/ and prepends that to the key-value list
    % ... Other keys specific to <module>
}

\begin{document}
\begin{tikzpicture}
    \path (0, 0) node[/my library/module/label properties] {Test node with default options};
    \mylibraryset[module]{label properties/.append style={blue, draw=black, fill=black!25!white}}
    \path (0, -1) node[/my library/module/label properties] {Test node with extra options};
\end{tikzpicture}
\end{document}

解决方法似乎是使一些键需要.style.append style处理程序。

答案1

您的代码无法运行的原因是您在需要时没有扩展值。但是,通过style/.expanded在相关位置插入,您的代码可以相当轻松地运行。

\documentclass{article}
\usepackage{tikz}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{etoolbox}

% Sets various configuration parameters
% \strandlibset[module]{options as pgfkeys}
\NewDocumentCommand{\mylibraryset}{o m}{
    \IfNoValueTF{#1}{
        \pgfqkeys{/my library}{#2}
    }
    {
        \pgfqkeys{/my library/#1}{#2}
    }
}
\pgfkeys{
    /my library/.is family,
    my library,
    label style/.style={node font=\bfseries},
    % ... Other top-level keys, some of which should set TikZ options
}

\pgfkeys{
    /my library/module/.is family,
    /my library/module,
    label style/.initial=, % Inherits the value of /my library/label style/ and prepends that to the key-value list
    % ... Other keys specific to <module>
}

\begin{document}
\begin{tikzpicture}
    \path (0, 0) node[style/.expanded=\pgfkeysvalueof{/my library/module/label style}] {Test node with default options};
    \mylibraryset[module]{label style={blue, draw=black, fill=black!10!white}}
    \path (0, -1) node[style/.expanded=\pgfkeysvalueof{/my library/module/label style}] {Test node with extra options};
\end{tikzpicture}
\end{document}

在此处输入图片描述

这种方法是否比使用密钥更好/.forward to是我们需要单独讨论的问题。

显然,您只需使用一种风格即可。

\documentclass{article}
\usepackage{tikz}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{etoolbox}

% Sets various configuration parameters
% \strandlibset[module]{options as pgfkeys}
\NewDocumentCommand{\mylibraryset}{o m}{
    \IfNoValueTF{#1}{
        \pgfqkeys{/my library}{#2}
    }
    {
        \pgfqkeys{/my library/#1}{#2}
    }
}
\pgfkeys{
    /my library/.is family,
    my library,
    label style/.style={node font=\bfseries},
    % ... Other top-level keys, some of which should set TikZ options
}

\pgfkeys{
    /my library/module/.is family,
    /my library/module,
    label style/.style=, % Inherits the value of /my library/label style/ and prepends that to the key-value list
    % ... Other keys specific to <module>
}

\begin{document}
\begin{tikzpicture}
    \path (0, 0) node[/my library/module/label style] {Test node with default options};
    \mylibraryset[module]{label style/.style={blue, draw=black, fill=black!10!white}}
    \path (0, -1) node[/my library/module/label style] {Test node with extra options};
\end{tikzpicture}
\end{document}

相关内容