In TikZ we can use every node/.style=... and every label/.style=... but is there a way to say something like every non label/.style=... so that I can define styles for non-label and label nodes without interference?
4 Answers
No tricky no hackish solution because I think it's bad problem. It's a bad use of every node.
This situation appears in some tutorials of the pgfmanual. First Andrew makes this remark : The key every label is called after the every node and It's necessary to complete with a text from the pgfmanual :
/tikz/label=[⟨options⟩]⟨angle⟩:⟨text⟩ (no default)When this option is given to a node operation, it causes another node to be added to the path after the current node has been finished. This extra node will have the text ⟨text⟩.
In other terms, the label is another node, so it's normal that you get some interferences.
What you need to do ? You can work with a ticky or hackish method or you can avoid the problem with a more natural code.
I tink the better way is to define a style not for every node but for a specific node
\documentclass[11pt]{scrartcl} \usepackage{tikz} \usetikzlibrary{shadows} \begin{document} \begin{tikzpicture} [every node/.style = {circle}, every special node/.style = {}, special node/.style={draw = blue!50, fill = blue!20, thick, every special node}] \tikzset{every special node/.style={circular drop shadow=red!20}} \tikzset{every label/.style={red}} % by default draw=none except if every node uses "draw" \node[special node,label={0:A}] {B}; \end{tikzpicture} \end{document}
- It might be the case that you don't want to type a key for each node. So I agree it's a misuse however there can be cases where you need to find a convenient solution.percusse– percusse2012-03-23 18:56:19 +00:00Commented Mar 23, 2012 at 18:56
- @percusse Don't want to type a key for each node is not a valid argument if you get a code easier to read and process but it is a matter of taste.Alain Matthes– Alain Matthes2012-03-23 20:31:42 +00:00Commented Mar 23, 2012 at 20:31
- Oh come on, I only wrote 3 lines of code. :) Joking aside, I agree with you about the natural way of coding. Lastly, I'm jealous about your nice color choice but I don't want to edit once again.percusse– percusse2012-03-24 00:53:17 +00:00Commented Mar 24, 2012 at 0:53
Tricky. The key every node is tried on every node. The key every label is called after the every node so it is possible to undo anything that every node does in the every label, but then you'd have to remember exactly what was set and know how to undo it.
Here's one way to achieve this. It works by using the fact that labels are always subordinate to nodes, and that (so far as I'm aware), no other nodes are subordinate to them (this might be a bit funny with matrices or chains - not tested). So we want a key, say every non label, which is executed on the parent node but not on any children (labels). One way to do that is for the key every non label to clear itself upon being executed. That ensures that it doesn't propagate downwards. As we do this in a group (the node group), this clearing only affects children and not siblings (ie other proper nodes), even on the same path.
Depending on exactly what you were doing, it might be necessary to be a bit careful with how this interacts with other keys. For example, if you put a label key in an every node one then you have to ensure that the blanking key occurs first.
\documentclass{article} %\url{http://tex.stackexchange.com/q/49096/86} \usepackage{tikz} \usetikzlibrary{shadows} \tikzset{ clear label/.style={ append after command={ [every non label/.style={}] } }, every node/.style={ every non label, clear label }, every non label/.style={ circular drop shadow, } } \begin{document} \begin{tikzpicture} \node[label=hello] {world}; \node[label=hello] at (0,-2) {world}; \path (0,-4) node[label=hello] {world} (1,-4) node[label=hello] {world}; \end{tikzpicture} \end{document} Result:

Another hackish solution for this particular example is to remove the shadow by emptying the preaction key. This is something I learned from this question.
\documentclass{article} \usepackage{tikz} \usetikzlibrary{shadows,shapes.geometric} \makeatletter \tikzset{every node/.style ={draw,shape=ellipse,circular drop shadow,blue}, every label/.style={yellow,draw=none,fill=red,shape=trapezium,fill opacity=0.5}, every label/.add code={}{\let\tikz@preactions\pgfutil@empty} } \makeatother \begin{document} \begin{tikzpicture} \node[label={0:Trapezium}] {Ellipse}; \end{tikzpicture} \end{document} 
Since the regular options can be overridden, I guess that, only pre- and post- actions remain persistent and as seen from this simple example we simply squeeze in an innocent "You know what, forget about what I said earlier about the preactions" line. Seemingly it does the job. You can add also a line for the postactions too. Still, I can imagine that there might be some persistent options left out.
Tackling the problem a little bit different (and comming from Reduce white space above TikZ label), I’d re-define labels and pins so that they don’t actually have a every node style to use by setting this style empty:
\patchcmd\tikz@@parse@label@nonactive{\bgroup[}{\bgroup[every node/.style=,}{}{} \patchcmd\tikz@@parse@pin@nonactive{\bgroup[}{\bgroup[every node/.style=,}{}{} The same happens for …@active with active : (can be tested by using french with the babel package).
The real every node style is now every Node which is included in every node, every label and every pin manually.
Furthermore, with .style I also removed the initial draw=none, fill=none of every label and every pin which now need to be given explicitly.
If this should still be present in those styles, instead of .style use .prefix style or .append style (whatever is needed).
The style every node and pin is the same as every non label.
Code
\documentclass[tikz]{standalone} %\usepackage[french]{babel} \usepackage{etoolbox} \makeatletter \c@pgf@counta=\catcode`\: \catcode`\:=\active\relax \patchcmd\tikz@@parse@label@active{\bgroup[}{\bgroup[every node/.style=,}{}{} \patchcmd\tikz@@parse@pin@active{\bgroup[}{\bgroup[every node/.style=,}{}{} \catcode`\:=\c@pgf@counta \patchcmd\tikz@@parse@label@nonactive{\bgroup[}{\bgroup[every node/.style=,}{}{} \patchcmd\tikz@@parse@pin@nonactive{\bgroup[}{\bgroup[every node/.style=,}{}{} \makeatother \tikzset{ every node/.style ={every Node/.try, every node and label/.try, every node and pin/.try}, every label/.style={every Node/.try, every node and label/.try, every label and pin/.try}, every pin/.style ={every Node/.try, every node and pin/.try, every label and pin/.try}, Nodes/.style ={every Node/.append style={#1}}} \begin{document} \tikzset{Nodes=draw} \tikz\node[label=bar, pin=below:pin] {foo}; \tikz[nodes=circle]\node[label=bar, pin=below:pin] {foo}; \tikz[Nodes=circle]\node[label=bar, pin=below:pin] {foo}; \end{document} Output



\tikzset{every node/.style ={draw,red},every label/.style={draw=none,blue}} \begin{tikzpicture} \node[label={0:A}] {B}; \end{tikzpicture}every node/.style={circular drop shadow}and this is afecting my labels too, and I don't know how to disable the shadow for labels ...circular drop shadowtocirclenodes, right? In that case, you could pass that option toevery circle nodeinstead ofevery nodeto keep it from applying to labels. Unless they're circular, of course. Hm, I'd really like to see a clever approach for theevery non labelstyle problem. Maybe Ryan Reich can come up with something.node[circle ...but I am definingevery node/.style={ellipse, circular drop shadow, ...}, every label/.style={...}. Then I have\node[label=thelabel]{...}and the node is draw as an ellipse with a shadow, but its label is also an ellipse with a shadow ...