我有一个节点和一些辅助节点(标签或不太重要的节点)。现在,如果我想允许用户通过样式自定义主节点和子节点,则用户需要编写类似 的内容main/.style={style main node}, subnode1/.style={style subnode}
,但我更希望将其编写为style main node, subnode1/.style={style subnode}
。我很好奇:这可能吗?我想我需要以某种方式从主样式中提取一些键,或者以某种方式将全局定义subnode1
为全局样式(似乎每个节点都创建一个组?)。
梅威瑟:
\documentclass[options]{article}
\usepackage{tikz}
\usetikzlibrary{quotes}
\begin{document}
\pgfkeys{
/first group of styles/.cd,
cool fill color/.style={fill=pink},
.search also={/tikz},% order is important
}
\pgfkeys{
/second group of styles/.cd,
cool shape/.style={draw,rounded corners},
label style/.style={},
label text/.store in=\labelText,
load defaults/.append style={
label text={Default label},
},
.search also={/tikz},
}
\NewExpandableDocumentCommand{\myNode}{O{}m}{
\node[
inner sep=3mm,
.search also={/second group of styles,/first group of styles,/tikz},
/second group of styles/load defaults,
#1,
% What is below is both ugly and failing
/utils/exec={
%%% Why is this needed?? Can I avoid it?
\xdef\globalLabelText{\labelText}
\message{OK}
},
"\globalLabelText" {%
.search also={/first group of styles, /second group of styles,/tizk},
% Whyy can't I set the style? Again a scope problem?
label style
},
alias=base
]
{#2};
\node[anchor=north] at (base.south){
\globalLabelText
};
}
\begin{tikzpicture}
\myNode[
cool fill color,
cool shape,
label text=My label,
my style/.style={scale=3},
label style/.style={red, my style},
]{A}
\end{tikzpicture}
\end{document}
编辑 似乎 pgf 过滤库为此提供了工具,但我的答案被删除了。