1

Consider follwong MWE. I have defined a style mynodestyle to style selected nodes in mypic. I want to customize the styling when placing some 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}

enter image description here

Can someone explain to me what happens in the case of the second placement of mypic? What does [mynodestyle={text=orange,font=\relsize{2}}] here do (no effect) and why becomes the node text without mynodestyle red?

And in general I have the impression that I do not know how to do this properly.

The notation /.style n args is too rigid because all arguments become mandatory. However, I would like to have my defaults for the selected nodes which I can overwrite individually and extend when needed.

Related question:

1 Answer 1

1

You can define a key

my node style/.code={\tikzset{mynodestyle/.style={#1}}},

which allows you to set the style without using /.style. In the second pic, you "execute" mynodestyle, which is why everything gets red. (I would agree that TikZ should give an error because of the additional =... stuff but this does not happen.)

\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}

enter image description here

2
  • Thank you for your answer. I updated my question by using .append style to keep defaults which are not changed for a specific instance of mypic. Can you explain what your solution offers as an advantage in contrast to using .style=... or .append style=...?
    – Hotschke
    Jun 4, 2020 at 15:51
  • 1
    @Hotschke The point is that you can make things somewhat shorter. Of course, you could also use my node style/.code={\tikzset{mynodestyle/.append style={#1}}}. Note that this is not my suggestion, but this way of dealing with styles gets used all over, as e.g. in nodes={draw}, which is basically a short cut for every node/.append style={draw}.
    – user194703
    Jun 4, 2020 at 15:57

You must log in to answer this question.

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