You can't use \tikz inside a tikzpicture environment like this. I have no idea what you are trying to do or what makes you think you can do this.
The \tikz command is the short version of the tikzpicture environment. Having it direct inside a tikzpicture environment makes as much sense as using a tikzpicture environment inside a tikzpicture environment!
So you second picture is basically:
\begin{tikzpicture}[show background rectangle]
\begin{tikzpicture}
\node[draw, rectangle split, rectangle split parts=2] at (0,0)
{ foo%
\nodepart{two}
bar
};
\end{tikzpicture}
\end{tikzpicture}
Still wondering why this doesn't get you good results?
The only surprise here is that both doesn't raise an error message! (This should be sent as bug report to the developers.)
You can't safely use arbitrary normal typesetting commands inside a tikzpicture. It's whole content is placed into a dummy TeX box which is later discarded while the drawing commands add their material into a second box which is typeset afterwards. Try adding some text without anything else into the picture. TikZ will just ignore it. You can however safely use \tikz or tikzpicture inside the content of a node. TikZ takes care to interrupt the current picture while processing the node content.
I could now speculate why you get exactly the output you get, but this would matter much. Apparently the size/bounding box survives but the placement is of.
If you want to be able to use drawing code inside and outside a node you can see if you are currently inside a tikzpicture by testing of e.g. \path has its normal TikZ definition. Note that TikZ reverts its own commands to the previous definition when typesetting the contents of nodes.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,shapes.multipart}
\makeatletter
% Ensures processing of content inside a `tikzpicture` (using the short version `\tikz`)
\newcommand{\ensuretikz}{%
\ifx\path\tikz@command@path
\expandafter\@firstofone
\else
\expandafter\tikz
\fi
}
\makeatother
\begin{document}
Here we have a
\begin{tikzpicture}[show background rectangle]
\node {
\ensuretikz{%
\node[draw, rectangle split, rectangle split parts=2] at (0,0)
{ foo%
\nodepart{two}
bar
};
}
};
\end{tikzpicture} picture.
Here we have a
\begin{tikzpicture}[show background rectangle]
\ensuretikz{
\node[draw, rectangle split, rectangle split parts=2] at (0,0)
{ foo%
\nodepart{two}
bar
};
}
\end{tikzpicture}
picture.
\end{document}
\tikzcommand if it is called inside the content of another node? – Nickolay Kolev May 20 '11 at 09:48\tikzcommand inside atikzpictureenvironment but, under some circumstances it can be helpful and I really liked this when I noticed that it could be done. I don't see this as a bug, but as a feature. – Gonzalo Medina May 20 '11 at 18:36scopeor\node {\tikz{...}};instead. – Martin Scharrer May 20 '11 at 18:38\tikzas the argument of\node(as I suggested in some other answer here); I didn't read carefully this question, so I initially didn't realize that Nickolay was using\tikz"directly" inside\tikzpicture, which, in fact, doesn't seem the right thing to do. Sorry for the misunderstanding. – Gonzalo Medina May 20 '11 at 18:53\ensuretikzis very interesting, you can look at this question : http://tex.stackexchange.com/questions/46596/how-can-i-check-if-the-current-code-is-inside-a-tikzpicture for another possibility. I encounter only one problem with nested tikzpicture environments but it's a next question. – Alain Matthes Mar 04 '12 at 08:27