# How do I use scopes to apply a style to nodes inside a path?

I want to use nodes inside a single path, in order that I might position one relative to another using the ++(x,y) notation.

However, a scope that I want to use to style the nodes only styles the path (and therefore appears to do nothing, as the path is 'invisible').

Adding the scope inside the path (around the nodes) doesn't work.

So, is there anyway to apply a scope to nodes inside a path?

Many thanks for any help.

Example:

\documentclass{article}
\usepackage{tikz}

\tikzset{
every node/.style = {
shape = rectangle,
minimum height = 20mm,
minimum width = 20mm,
line width = .5mm},
mystyle/.style = {draw = red!50!black!50}
}

\begin{document}

\begin{tikzpicture}

% These two nodes have the correct style,
% but I have to specify the style for every node
% (I'll have around 100 nodes in total, hence the
% desire to use a scope for the style).
\path (0,6)
node(nodeOne) [mystyle] {Node One}
++(5,0) node(nodeTwo) [mystyle] {Node Two};

% These two nodes are in the mystyle'' scope,
% but the style is not applied to the nodes
% as they reside inside a path.
\begin{scope}[mystyle]
\path (0,0)
node(nodeThree) {Node Three}
++(5,0) node(nodeFour) {Node Four};
\end{scope}

\end{tikzpicture}

\end{document}

-
It's not applied because your custom style is not a node style yet so the color option is ignored. Add draw option to any of node 3 or 4 and you can see that it is adopted. – percusse Oct 27 '12 at 23:47

A path's or scope's [mystyle] only applies to the paths (if they are drawn), and as you used only \path and not some kind of line (--, to, …) there's not much to draw.

For the first path you could add every node/.append style=mystyle to its options so that the path's nodes get the style mystyle additional to the globally set every node/.style from your \tikzset in the preamble.

\path[every node/.append style=mystyle] (0,6) node(nodeOne) {Node One}
++(5,0) node(nodeTwo) {Node Two};


This also applies to scopes:

\begin{scope}[every node/.append style=mystyle]
\path (0,0) node(nodeThree) {Node Three}
++(5,0) node(nodeFour) {Node Four};
\end{scope}


If you only want to place nodes relatively to each other, you might consider (using the positioning library of TikZ (e.g. \usetikzlibary{positioning}).

\begin{scope}[every node/.append style=mystyle, on grid]
\node (nodeOne) at (0,6) {node One};
\node[right=5cm of nodeOne] (nodeTwo) {node Two};
\end{scope}


Note the key on grid that makes sure the the relative positioning is from center to center, not between the outer border of the nodes. The “Advanced Placement Options” are explained in detail in subsection 16.5.3 of the PGF/TikZ manual.

For simple cases of placement, it can suffice to set a node distance:

\begin{scope}[every node/.append style=mystyle, node distance=5cm, on grid]
\node (nodeOne) at (0,6) {node One};
\node[right=of nodeOne] (nodeTwo) {node Two};
\end{scope}

-
Wow, many thanks for all the very quick responses! Thank you to everyone---this is exactly what I needed. I was unaware of the ".append" modifier too. – Graham Oct 28 '12 at 0:32

I've had something like this before... I think the initial every node/.style = { options } is in effect regardless of any subsequent \begin{scope} .... \end{scope} environment.

Try using another every node/.style = { options } in the relevant scope to apply locally to where you want to change how the nodes are produced.

-