Since Joseph has posted a "Not exactly an answer", I'm going to post my "not exactly an answer" answer as well. The reason that this is "not exactly an answer" is because I don't think that the question is the right way around. The question asks how to protect an inner tikzpicture from an outer one. My "answer" to that is:
- You don't want to.
- If you do, use a box.
The reason for the first is that in all likelihood you want some things to be inherited, most likely due to size. You might also want to inherit a colour. For this sort of partial inheritance, the best approach is to ensure that the inner tikzpicture explicitly sets those values that it doesn't want to inherit and then leaves the rest alone. This is the same as the normal scoping behaviour of TeX groups and pgfscope
s.
The real problem with inner and outer tikzpictures is not protecting the inner from the outer, but protecting the outer from the inner. There are certain parts of a tikzpicture that are globally set because there are certain pieces of information that need to be shifted out of certain internal groupings and it is just so much easier to do this via setting global macros. The ones that I've encountered are:
- The bounding box of a picture.
- The coordinates of a node.
There may be others. The question What are most important variables set at the beginning of a tikzpicture? of a scope? was asked to gather this information. With bounding boxes, see https://tex.stackexchange.com/a/46792/86 and for nodes, see tikz : how to refer to a node, in nested nodes (there are plenty of other examples).
There is some code that fixes both of these problems. It's called the subpicture
package and is part of the (excellent) tikz-qtree
package (which is probably why not many people have heard of it - I only know because I was digging through that code to answer other questions on this site). This is also why this is "not quite an answer": I've never tried to use that code by itself so don't know how it would work outside of the tikz-qtree
package. But then, I'm one of the staunchest "Don't nest tikzpictures" advocates around. But if I ever did have to nest tikzpictures, that would be the way that I would do it.
What I do know about it is that internally, it does use boxes. But it saves the bounding box and node information so that these are still accessible to the outer tikzpicture. So it is the best of both worlds in that sense.
right of =
is not inherited but it'sright= of
.{lrbox}
part of Martin’s codepositioning
library between groups of tikz objects ; To build a tree within each node is a group of tikz objects ; etc.