27

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}
equaeghe
  • 5,976
  • 4
  • 30
  • 36
  • right of is not inherited but it's right= of Make a try with right of= O – Alain Matthes Mar 09 '12 at 11:19
  • A while after asking this question, I put the tikzpictures that I want to put in nodes in TeX boxes; cf. http://tex.stackexchange.com/a/66028/87. An approach which I feel is more convenient. – equaeghe Jul 16 '13 at 14:18
  • Yes it's a fine possibility. I use it in some cases but you can imagine the possibility to pass some arguments between the two tikzpictures. I think it's not possible with a TeX box but I'm not sure. In a general case, the TeX box is a good approach. – Alain Matthes Jul 16 '13 at 20:05

1 Answers1

25

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}
Martin Scharrer
  • 262,582
  • So there's no command to reset all TikZ attributes to default in this case? – krlmlr Feb 08 '12 at 08:49
  • 2
    @user946850: No, I don't think so. Such a command would be very difficult to maintain, because all TikZ attributes would need need to be set with it explicitly. You can't reset things in TeX only overwrite them (with the exception of grouping of course). – Martin Scharrer Feb 08 '12 at 10:45