42

How can all labels of a given type of node be restyled in Tikz?

Imagine that there are nodes of different styles, and that only the nodes of style X should have red labels. Is this possible without having to change all labels one by one of nodes of style X?

2 Answers 2

52

To change it for all nodes:

\tikzset{every node/.style={<style_specs>}}

If you have a custom style, you can set this only for that specific node style:

\tikzset{My Style/.style={red, draw=blue, fill=yellow!20, minimum size=0.5cm}}

If you want to change an existing node style, you can use .append style to add to the nodes style specs:

\tikzset{rectangle/.append style={draw=brown, ultra thick, fill=red!30}}

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}

\tikzset{My Style/.style={red, draw=blue, fill=yellow!20, minimum size=0.5cm}}
\tikzset{rectangle/.append style={draw=brown, ultra thick, fill=red!30}}

\begin{document}
\begin{tikzpicture}
    \node [My Style] at (0,0) {$x$};
    \node [My Style] at (1,0) {$y$};

    \node [rectangle] at (2,0) {$z$};
\end{tikzpicture}
\end{document}

This actual intent here is more complex that what I thought since a label is actually a node. One solution would be to define a style for each particluar shape. So, for instance, for a rectangle you could define My Rectangle as:

\tikzset{My Rectangle/.style={
    rectangle, draw=brown, fill=yellow, thick,
    prefix after command= {\pgfextra{\tikzset{every label/.style={blue}}}}
    }
}

which alters the label style (Again, thanks to percusse):

enter image description here

You could also just change the rectangle style as follows:

\tikzset{rectangle/.append style={
    prefix after command= {\pgfextra{\tikzset{every label/.style={blue}}}}
    }
}

Code:

\documentclass{article}
\usepackage{tikz}

\tikzset{My Rectangle/.style={
    rectangle, draw=brown, fill=yellow, thick,
    prefix after command= {\pgfextra{\tikzset{every label/.style={blue}}}}
    }
}

\begin{document}

\begin{tikzpicture}
    \node [rectangle,    label=black text] at (1,0) {$z$};
    \node [My Rectangle, label={blue text} ] at (3,0) {$z$};
    \node [rectangle, label={blue text} ] at (5,0) {$z$};
\end{tikzpicture}
\end{document}
4
  • I am looking for something like \tikzset{X/.style={label={[red]:...}}}` for example, to change the color of the labels of node X to red.
    – mljrg
    Oct 22, 2012 at 16:39
  • @mljrg see tex.stackexchange.com/a/49113/11241
    – mythealias
    Oct 22, 2012 at 16:40
  • Can I set a text to this new style?
    – hola
    May 16, 2021 at 6:35
  • @hola: Not sure I understand what you are asking as the \tikzset defines a new style. If you want to have it applied ALL the time, then you can use \tikzset{every node/.style={My Rectangle}}. If that doesn't solve your question, perhaps you need to ask a new question. May 17, 2021 at 17:26
24

Prolog: After Peter Grill undeleted his answer my answer becomes kind of null.
But maybe someone else can profit from it in some way …

What does not work

Surprisingly (to me anyway) the following will turn the node's text itself green.

\tikzset{X/.style={label/.style={green}}}
\node[X,label=hello] {X};

The existing every label/.style definition apparently works only in a scope-like usage.

\begin{scope}[every label/.style={green}]
    \node[X,label=hello] {X};
    % and a lot of other X nodes
\end{scope}

Adding the every label/.style to the definition of X fails (with no change to the output):

\tikzset{X/.style={every label/.style={green}}}
\node[label=hello] {X};

What does work

The /.style can have one argument:

\tikzset{X/.style={
    % your styles here, e.g.
    draw,
    circle,
    % and …
    label={[green]#1}
    }
}
\node[X=hello] {X};
\node[X={[draw]hello}] {Y}; % this fails, you can't add an extra optional argument

My proposal

I'd use the .style args key handler, where one can set the argument pattern to [#1]#2. Note, that the [] are not mandatory!

The key handler .style 2 args has also two arguments where the second one is optional, but it requires you to write X={hello} or X={hello}{draw}.

Code

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\tikzset{X/.style args={[#1]#2}{
    % your styles here, e.g.
    draw,
    circle,
    % and …
    label={[green,#1]#2}
    }
}
\node[X={[]hello}] {X};
\node[X={[draw]hello}] at (1,0) {Y};
\node[X={[red]hello}] at (2,0) {Z};
\end{tikzpicture}
\end{document}

Output

Output

Furthermore

I believe there is a solution hidden in the answers of Optional arguments in pgfkeys?. But they all appear to require to write [draw]{hello}.

You must log in to answer this question.

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