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?
5 Answers
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}
- 158,329
- 89,023
- 26
- 255
- 291
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.
- 153,724
- 43
- 389
- 751
(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.
I don't think I saw this mentioned already - you can use \tikz\node to nest a node inside another one. Though, I'm not sure what outside variables/macros would be referable from the inside. Still, I use it to left-pad numbers with spaces ( given how we don't have a \printf{% 3d}{4} in Latex :) ):

The code:
\documentclass[varwidth]{standalone}
\usepackage{tikz}
\usepackage{calc}
\pagecolor{yellow!15}
\begin{document}
\begin{tikzpicture}
\newlength{\wtmp}
\setlength{\wtmp}{\widthof{589}}
% style: node left pad (for) number (padded with space)
\tikzstyle{nlpn} = [draw=none, align=right, inner sep=0pt, outer sep=0pt, minimum size=0pt, minimum width=\wtmp,text width=\wtmp]
% style for function:
\tikzstyle{nlpnA} = [nlpn,draw]
% function to insert node:
\def\lpn#1{\tikz\node[nlpnA]{#1};}
% place node - use function to place node inside
\node[draw] at (0,4) {[\lpn{64}/512/\lpn{0}]};
\tikzstyle{nlpnA} = [nlpn]
\node[draw] at (0,3) {[\lpn{64}/512/\lpn{0}]};
\end{tikzpicture}
\end{document}
- 17,079
-
1You can use \phantom to create empty space the size of some text and precede it with \rlap to make that overlap the empty space. E.g. \rlap{text}\phantom{longer text}. Use \llap for overlapping from the other side, e.g. \phantom{longer text}\llap{text}. This works anywhere because these are TeX primitives. See also https://www.tug.org/TUGboat/tb22-4/tb72perlS.pdf – James C. Jan 17 '15 at 21:51
tikzpictureenvironment internally), which are then referred to from the node content. – Svante Aug 04 '10 at 13:03remember pictureto both, inner and outer picture. – Twonky Nov 07 '22 at 10:22