考虑以下 MWE。我已定义了一种样式mynodestyle
来设置 中选定节点的样式mypic
。我想在放置一些 时自定义样式mypics
。
\documentclass{standalone}
\usepackage{tikz,relsize}
\tikzset{
mynodestyle/.style={text=red,font=\relsize{1}},
%
% Would following alternative definition of 'mynodestyle' with the given default value be sensible?
mynodestyle2/.style={#1},
mynodestyle2/.default={text=red,font=\relsize{1}},
%
pics/mypic/.style={code={%
\node[mynodestyle] at (0,0) {Hello}; % How can I 'style' these nodes with text from outside of the pic code?
\node[mynodestyle] at (2,0) {World};
\node at (2,-1) {I should not be in style.}; % non styled node
}}
}
\begin{document}
\begin{tikzpicture}
\path (0,0) pic {mypic};
\path (0,-3) pic [mynodestyle={text=orange,font=\relsize{2}}] {mypic};
\path (0,-6) pic [mynodestyle/.append style={text=blue,font=\relsize{4}}] {mypic}; % If I have to use it this way, does a default make sense?
\path (0,-9) pic {mypic};
\end{tikzpicture}
\end{document}
有人能解释一下 mypic 第二次放置时会发生什么吗?[mynodestyle={text=orange,font=\relsize{2}}]
这里做了什么(没有效果)以及为什么节点文本没有mynodestyle
红色?
总的来说,我的印象是我不知道如何正确地做到这一点。
这种表示法/.style n args
太死板,因为所有参数都是强制性的。不过,我希望为选定的节点设置默认值,这样我可以单独覆盖并在需要时进行扩展。
相关问题:
答案1
您可以定义一个键
my node style/.code={\tikzset{mynodestyle/.style={#1}}},
这允许您在不使用 的情况下设置样式/.style
。在第二张图片中,您“执行” mynodestyle
,这就是所有内容变为红色的原因。(我同意 Ti钾Z 应该因为额外的内容而给出错误=...
但是这并没有发生。)
\documentclass{standalone}
\usepackage{tikz,relsize}
\tikzset{
mynodestyle/.style={text=red,font=\relsize{1}},
my node style/.code={\tikzset{mynodestyle/.style={#1}}},
%
% Would following alternative definition of 'mynodestyle' with the given default value be sensible?
mynodestyle2/.style={#1},
mynodestyle2/.default={text=red,font=\relsize{1}},
%
pics/mypic/.style={code={%
\node[mynodestyle] at (0,0) {Hello}; % How can I 'style' these nodes with text from outside of the pic code?
\node[mynodestyle] at (2,0) {World};
\node at (2,-1) {I should not be in style.}; % non styled node
}}
}
\begin{document}
\begin{tikzpicture}
\path (0,0) pic {mypic};
\path (0,-3) pic [mynodestyle={text=orange,font=\relsize{2}}] {mypic};
\path (0,-6) pic [mynodestyle/.style={text=blue,font=\relsize{4}}] {mypic}; % If I have to use it this way, does a default make sense?
\path (0,-9) pic {mypic};
\path (0,-12) pic [my node style={text=orange,font=\relsize{2}}] {mypic};
\end{tikzpicture}
\end{document}