# How to give a label to nodes which already contain a tikzpicture with nodes.

I am a beginner at TikZ and LaTeX. This is what I currently have:

I would like to label each of these rectangles (i.e. graphs) with a single letter, i.e. G.

This is the code for a single graph in this picture:

  \node[outer] (L) {
\begin{tikzpicture}
\node [inner,label=below:1] (a) at (0,0) {1};
\node [inner] (ai) at (1,0) {1};
\node [inner,label=below:2] (aii) at (2,0) {1};
\draw[->] (a) edge (ai);
\draw[->] (ai) edge (aii);
\end{tikzpicture}
};


Specifically, this is the code for the top leftmost graph.

I would like to give it a label L. I read online that to give a caption (label) to a tikzpicture you have to put it inside the tikz figure. I've done it for the big graph, hence you can see "Figure 3: A double pushout." below the whole picture.

But when I put the contains of this node L inside a tikzfigure, i.e.

  \node[outer] (L) {
\begin{figure}
\begin{tikzpicture}
\node [inner,label=below:1] (a) at (0,0) {1};
\node [inner] (ai) at (1,0) {1};
\node [inner,label=below:2] (aii) at (2,0) {1};
\draw[->] (a) edge (ai);
\draw[->] (ai) edge (aii);
\end{tikzpicture}
\end{figure}
};


It throws an error.

! LaTeX Error: Not in outer par mode.


Also, it's really a lot "figures", which seems unnecessary for me?

I also tried this solution (which works for small nodes), so instead

  \node[outer] (L) {


I wrote

  \node[outer, label=above:L] (L) {


But it throws this error:

! Undefined control sequence.
<argument> pgf@sh@ns@\tikzlastnode
l.15 \draw[->] (a) edge (ai)


My code may seem silly to you, experts. So feel free to point out any mistakes! I have a feeling I'm too "wordy" for what I'm trying to achieve.

For completeness, I will say that the big tikzpicture has the following settings:

\begin{figure}[htb!]
\centering
\begin{tikzpicture}[remember picture,
inner/.style={circle,draw,inner sep=4},
outer/.style={draw,inner sep=4, outer sep=2} % deleted thick here after draw


Thanks for help everyone!

-

Another alternative, moving the draw out of {external label}.

Code

\documentclass[border=10pt]{standalone}

\usepackage[demo]{graphicx}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}

\begin{document}
%
\begin{tikzpicture}[remember picture,
inner/.style={circle,draw,inner sep=4},
outer/.style={draw,inner sep=4, outer sep=2}]

\node[outer,label=above:L]  (L) {
\begin{tikzpicture}
\node [inner,label=below:1] (a) at (0,0) {1};
\node [inner] (ai) at (1,0) {1};
\node [inner,label=below:2] (aii) at (2,0) {1};
\end{tikzpicture}
};
\draw[->] (a) edge (ai)  (ai) edge (aii);

\begin{scope}[xshift=4cm]
\node[outer,label=above:M]  (M) {
\begin{tikzpicture}
\node [inner,label=below:1] (a) at (0,0) {1};
%    \node [inner] (ai) at (1,0) {1};
\node [inner,label=below:2] (aii) at (2,0) {1};
\end{tikzpicture}
};
\draw[->] (a) edge  (aii);
\end{scope}

\draw[->] (L) -- (M);

\end{tikzpicture}
\end{document}

-
Thanks for that, I went with your idea as I haven't got much to modify in my code. However, I had to resort to Gonzalo's method when I encountered an example with loops above the nodes - couldn't get the rectangle to be drawn to include the loops, as they ended up beyond the rectangle. But for all other cases, i.e. when edges are between the nodes, this is great. –  Filip Feb 6 '14 at 2:40
My answer tends to use the OP's method for ease of communiation and gets info across. Gonzalo is one of the best gurus here, full of ideas. Thank you very much. –  Jesse Feb 6 '14 at 3:03

1. You don't need nested tikzpictures; you can do all your six "subfigures" inside a single environment without nesting.

2. Using the fit library you can produce the rectangular frames for each subfigure: they are simply \nodes, so you can name them and use a label for them to place the required strings.

3. You can place your \nodes more easily using the positioning library and the =of syntax.

The code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit,positioning}

\begin{document}

\begin{figure}
\begin{tikzpicture}[
remember picture,
inner/.style={circle,draw,inner sep=4,minimum size=20pt},
outer/.style={draw,inner sep=4, outer sep=2},
node distance=0.5cm
]
% top left
\node [inner,label=below:1] (a) {1};
\node [inner,right=of a] (ai) {1};
\node [inner,label=below:2,right=of ai] (aii) {1};
\draw[->] (a) edge (ai);
\draw[->] (ai) edge (aii);
\coordinate[below=0.5cm of a] (aux1);
\node[draw,fit={(a) (aux1) (aii)},inner ysep=10pt,label={above:L}] (boxtl) {};

% top middle
\begin{scope}[xshift=4.5cm]
\node [inner,label=below:1] (b) {};
\node [inner,right=of b,draw=none] (bi) {};
\node [inner,label=below:2,right=of bi] (bii) {};
\coordinate[below=0.5cm of b] (aux2);
\node[draw,fit={(b) (aux2) (bii)},inner ysep=10pt,label={above:M}] (boxtm) {};
\end{scope}

% top middle
\begin{scope}[xshift=9cm]
\node [inner,label=below:1] (c) {2};
\node [inner,right=of c,draw=none] (ci) {};
\node [inner,label=below:2,right=of ci] (cii) {3};
\coordinate[below=0.5cm of c] (aux3);
\node[draw,fit={(c) (aux3) (cii)},inner ysep=10pt,label={above:N}] (boxtr) {};
\end{scope}

% the arrows
\draw[->] (boxtr) -- (boxtm);
\draw[->] (boxtm) -- (boxtl);
\end{tikzpicture}
\caption{a figure with six subfigures}
\end{figure}

\end{document}


-
Thank you very much! This is great, the only reason I took the other one is because it was easier to understand, apologies. I don't completely understand the purpose of coordinate, i.e. why does it have to be 0.5cm below the first node within each graph? Cheers. –  Filip Feb 6 '14 at 2:43
@Filip those are auxiliary coordinates whose only purpose is to "enlarge" the nodes downwards a little bit, so all six figures would have a more similat height; of course, you can remove them, since they are not essential. –  Gonzalo Medina Feb 6 '14 at 2:53