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.

My use case: I am drawing chemical schemes. Each molecule contains several nodes. However, I also want to put each molecule into a "wrapper" node, so that I can use the usual anchors in combining the molecules into a reaction scheme. How can I achieve that, or something similar?

share|improve this question
add comment

4 Answers

up vote 7 down vote accepted

Just some initial ideas:

Depending how complicated the "inner" part needs to be, one way to do this would be to make the molecules a "matrix" node (see the pgfmanual for details on how to use it). Another possibility to look in to is the "chain" library.

Another option would be to draw an invisible node at the centre of the molecule of the requisite size (draw it visibly first to get it the right size). The disadvantage of that is that you have to work out that size whereas it would be nice to get TikZ to do it by hand.

share|improve this answer
    
You could use the fit library to draw the "background" node. –  Christian Wolf Nov 13 '12 at 7:52
add comment

You can abuse matrix nodes. Normal nodes do not let you put TikZ commands inside them (as they revert to text mode), but matrix nodes do. So you could just create a 1x1-matrix for every part. For example:

\begin{tikzpicture}
    \node[matrix] (A) {
        \draw (0,0) rectangle (1,1); 
        \node at (0.5,0.5) {A}; \\
    };
    \node[matrix,left of=A] (B) 
    {
        \draw (0.5,0.5) circle (0.5);
        \fill (0.5,0.5) circle (0.1); \\
    };
\end{tikzpicture}
share|improve this answer
add comment

I did it by including a whole \begin{tikzpicture} ... \end{tikzpicture} in the content of each node.

Maybe not the most elegant way of doing it... but it works.

share|improve this answer
    
Thanks! I underestimated the nesting ability of the environment. I now define the molecules as separate commands (using the tikzpicture environment internally), which are then referred to from the node content. –  Svante Aug 4 '10 at 13:03
2  
One reason not to use this method is that referring to nodes within the inner picture is problematic. A quick test showed that tikz does know about the inner nodes, but thinks that they're in different places to where they actually are. –  Andrew Stacey Aug 4 '10 at 13:08
    
Yes, naturally. I do not need to refer to the inner nodes from the outside, though. Having an inner environment should however prevent name clashes. If you need to refer to inner nodes, the matrix solution seems to be the way to go. –  Svante Aug 4 '10 at 13:17
6  
@Svante: Actually, my experiments show that there is not a decent separation between "inner" and "outer" names. TikZ does not expect this nesting so there is no protection for node names. You can refer to inner nodes from the outer and outer nodes from the inner but it does not complain if you use the same nodenames inside and out and the positioning of inner nodes is not right when viewed from the outside. –  Andrew Stacey Aug 4 '10 at 13:39
    
Oh, OK, then. :) –  Svante Aug 4 '10 at 22:54
add comment

(This should be a comment to André's answer but sadly I am not allowed to do that.)

While the original question was asked earlier, there is a very similar question or even duplicate here that has an answer that uses remember picture to make a nested tikzpicture work properly. For me this worked while I had problems with using matrix.

share|improve this answer
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.