Nested scope shift in nested tikzpictures?

\documentclass{book}

\usepackage[a4paper,twoside,hmargin=3cm]{geometry}

\usepackage{tikz,tikzpagenodes}
\usetikzlibrary{calc,positioning}

\newif\ifoutertikznest
\outertikznesttrue
% \outertikznestfalse

\begin{document}

\begin{tikzpicture}[overlay,remember picture,anchor=north west,inner sep=0pt, outer sep=0pt,line width=2pt]

\node[draw=red,minimum size=2cm,anchor=north west,inner sep=0pt,outer sep=0pt] at (0,0) (A1) {%
\ifoutertikznest %
\begin{tikzpicture}[overlay,remember picture,anchor=north west,inner sep=0pt, outer sep=0pt,line width=2pt] % ***
\else
}; % ***
\fi

\node[draw=blue,minimum size=2cm,anchor=center,inner sep=0pt,outer sep=0pt] at (current page.center) (A2) {};

\node[draw=green,minimum size=2cm,anchor=north west,inner sep=0pt,outer sep=0pt] at (current page.north west) (A3) {};

\begin{scope}[shift={(0.5,1.0)}] %
\node[draw=magenta,minimum size=2cm,anchor=north west,inner sep=0pt,outer sep=0pt] at (0,0) (A4) {};
\begin{scope}[shift={(0.5,1.0)}] %
\node[draw=yellow,minimum size=2cm,anchor=north west,inner sep=0pt,outer sep=0pt] at (0,0) (A5) {};
\end{scope}
\end{scope}

\ifoutertikznest %
\end{tikzpicture} % ***
}; % ***
\fi

\end{tikzpicture}

\end{document}


If you enable the \outertikznestfalse line, then there is no additional wrapping in a tikzpicture, and the nested scope shifts look as expected (image left):

However, if the rest of the image is "wrapped" under the first node (which requires an extra tikzpicture) - and this is the state, if outertikznesttrue line is enabled instead - then something is not quite right with the relative shift of the nested scopes (image right).

While I did expect the extra tikzpicture to influence the absolute placement in some way - I didn't expect that it would disturb the relative shifts between the scopes. Is this expected behavior? And how could I have the same relative position of the nested scopes (as on image left), even if I have an extra tikzpicture wrapper?

-
I see what I expect to see: the yellow and magenta squares are in the same relative relationship because they are always in the same tikzpicture. But they move relative to the red because they go from being in the same tikzpicture to being in different ones. Nesting tikz pictures should always be regarded as a last resort and if there is a way to put everything in one picture, that solution should be preferred. –  Loop Space Jun 18 '12 at 7:34
Thanks for that, @AndrewStacey : "the yellow and magenta squares are in the same relative relationship" - didn't notice this initially; half of the answer is there! "...as a last resort" - point taken; however, I have a tikz composition which is "upper-left-corner" aligned to a small page (A5) - and now I'd like to center those contents to a bigger page (A4); which is why I'd hope for a possible solution, for having (also) the relative position between the magenta and red kept (even if in different tikzpictures). Many thanks again - cheers! –  sdaau Jun 18 '12 at 7:41

The result is normal. You can add \fill (0,0) circle (2pt);

  \fill (0,0) circle (2pt);
\begin{scope}[shift={(0.5,1.0)}] %
\node[draw=magenta,minimum size=2cm,anchor=north west,inner sep=0pt,outer sep=0pt] at (0,0) (A4) {};
\begin{scope}[shift={(0.5,1.0)}] %
\node[draw=yellow,minimum size=2cm,anchor=north west,inner sep=0pt,outer sep=0pt]  at (0,0) (A5) {};
\end{scope}
\end{scope}


You can see that you change the origin point with the nested tikzpicture. In this case the orign point in the center of the red node, in the other case it's the north west point (normal with your code).

-
Many thanks for that, @Altermundus - I can now see that I "change the origin point with the nested tikzpicture"; I otherwise thought all those "anchor=north west" would somehow "force" the tikzpicture to align there; but apparently not - great tip with the circle :) Many thanks again - cheers! –  sdaau Jun 18 '12 at 7:59