TikZ-PGF TikZ Styles – TikZpicture in Node of Another TikZpicture: How to Screen Off from Inheriting Style

tikz-pgftikz-styles

I wish to create tikzpicture environments inside a node of another tikzpicture.
The problem I encouter is that styles are inherited by the inner tikzpicture.
How can I screen of the inner tikzpicture form the outer one? Or is this impossible?

The following minimal example illustrates the problem:

\documentclass{minimal}
\usepackage{tikz}
  \usetikzlibrary{positioning}
\begin{document}

% wanted: square centered on line
\begin{tikzpicture}
  \draw (0,0) -- (0,1);
  \node[fill] at (0,.5) {};
\end{tikzpicture}

% got: square to the right of line
\begin{tikzpicture}[red]
  \node[fill] (O) {};
  \node[right=of O] {% <- this 'right of' is inherited; how to avoid?
  \begin{tikzpicture}
    \draw (0,0) -- (0,1);
    \node[fill] at (0,.5) {};
  \end{tikzpicture}
  };
\end{tikzpicture}

\end{document}

Best Answer

In this specific case you can use anchor=center to restore the default positioning. The right etc. settings modify the used anchor.

\documentclass{minimal}
\usepackage{tikz}
  \usetikzlibrary{positioning}
\begin{document}

% wanted: square centered on line
\begin{tikzpicture}
  \draw (0,0) -- (0,1);
  \node[fill] at (0,.5) {};
\end{tikzpicture}

% got: square to the right of line
\begin{tikzpicture}[red]
  \node[fill] (O) {};
  \node[right=of O] {% <- this 'right of' is inherited; how to avoid?
  \begin{tikzpicture}[anchor=center]
    \draw (0,0) -- (0,1);
    \node[fill] at (0,.5) {};
  \end{tikzpicture}
  };
\end{tikzpicture}

\end{document}

In the general case if you want to avoid any settings to affect the sub-tikzpicture I would store it into a savebox before the main tikzpicture and insert that box:

\documentclass{minimal}
\usepackage{tikz}
  \usetikzlibrary{positioning}
\begin{document}

% wanted: square centered on line
\begin{tikzpicture}
  \draw (0,0) -- (0,1);
  \node[fill] at (0,.5) {};
\end{tikzpicture}


\newsavebox\mybox

\begin{lrbox}{\mybox}
  \begin{tikzpicture}
    \draw (0,0) -- (0,1);
    \node[fill] at (0,.5) {};
  \end{tikzpicture}
\end{lrbox}

% got: square to the right of line
\begin{tikzpicture}[red]
  \node[fill] (O) {};
  \node[right=of O] {% <- this 'right of' is inherited; how to avoid?
        \usebox\mybox
  };
\end{tikzpicture}

\end{document}