Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This is a problem that's come up from time to time as an aspect of a question, and it's something that I think would be an extremely useful addition to the TikZ/PGF system.

The basic set-up is to be able to designate a part of a tikzpicture as a "sub picture". The ideal would be that it act a bit like a node with a tikzpicture as its contents, but without actually nesting tikzpictures (which, in my experience, causes a few issues with inheritance). In particular, it should be possible to position the sub-picture according to its bounding box (just like with a node), and there should be a surrounding (rectangular) node with all its anchors (that's probably the easy part).

I can think of a few ways to do this, but others may have already solved this or have better ideas than me.

share|improve this question
1  
I had basically the same idea a while back. I also needed a sub-picture where I can still access the anchors from within the parent picture. Kind of turning the sub picture into a shape. Unfortunately, I never figured out how to do it. –  Martin Scharrer Nov 13 '11 at 21:14
    
Any reason why you can't just have two separate TikZ pictures with remember picture and overlay where applicable? You can get the bounding box by setting coordinates in places where you want the anchors in the sub-picture using the current bounding box node. These can then be referenced from the containing picture. –  Roelof Spijker Nov 14 '11 at 9:07
add comment

1 Answer 1

Using just an external TikZ picture is not quite enough. If you want the position of the sub-picture to be influenced by elements from the containing picture and yet other elements from the containing picture be influenced by the sub-picture in turn, you need something additional. We could put our sub-picture in a savebox and use it inside of the main picture. We then use it inside of the main picture, by putting it inside of a node with inner sep=0pt. This allows us to position the node like any other node, while it can contain a TikZ picture that does not suffer from inheritance issues. If we additionally set the remember picture option on both pictures, we can references nodes within the first picture as well. We won't need the overlay option here, since it is basically one picture that happens to have a box in it. The following is a very basic example that demonstrates the concept:

\documentclass{article}
\usepackage{tikz}
\begin{document}
  \newsavebox\mysubpic
  \sbox{\mysubpic}{%
    \begin{tikzpicture}[remember picture] %sub-picture
      \path[draw, fill=blue!30] (0,0) -- (2,2) -- (0,2) -- cycle;
      \path[draw, fill=green!30] (0,0) -- (2,2) -- (2,0) -- cycle;
      \coordinate (orig) at (0,0);
      \coordinate (sw) at (current bounding box.south west);
      \coordinate (nw) at (current bounding box.north west);
      \coordinate (se) at (current bounding box.south east);
      \coordinate (ne) at (current bounding box.north east);
    \end{tikzpicture}% needed, otherwise anchors are wrong!
  }
  \begin{tikzpicture}[remember picture]
    \path[draw] node (a) {before} ++(2,0) node[inner sep=0pt] (subpic) {\usebox{\mysubpic}} ++(\wd\mysubpic,0) node {after};
    \foreach \anch in {south west, north west, south east, north east}{
      \fill[blue] (subpic.\anch) circle[radius=2pt];
    }
    \foreach \anch in {sw,nw,se,ne}{
      \fill (\anch) circle[radius=1pt];
    }
  \end{tikzpicture}
\end{document}

And the resulting image:

sub-picture in TikZ

share|improve this answer
    
I've reimplemented the code we were having difficulty with using this new savebox approach, but unfortunately exactly the same issue arises. I've posted the problematic code in the chat room. –  Jamie Vicary Nov 14 '11 at 12:30
    
The context of the above comment is the discussion Andrew Stacey and I were having here, about his new knots package. –  Jamie Vicary Nov 14 '11 at 12:39
    
The issues is not with the approach, it was with the engine used. This should work fine, as long as its used with pdflatex. –  Roelof Spijker Nov 14 '11 at 13:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.