Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 14 down vote accepted

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}
share|improve this answer
    
Nops. That will change for all nodes, whether labels or not. –  mljrg Oct 22 '12 at 16:33
    
@mljrg \tikzset{every <shape> node/.style={...}} –  mythealias Oct 22 '12 at 16:34
    
@mythealias Nops. That will change for all labels of all nodes, not only for the labels of node of style X –  mljrg Oct 22 '12 at 16:36
    
@mljrg It is the same idea ... \tikzset{every <special> node/.style={...}} will change it for every node named <special> –  mythealias Oct 22 '12 at 16:38
2  
@PeterGrill Well done! I've changed my code after your solution! Thanks –  mljrg Oct 22 '12 at 23:33
show 5 more comments

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.