0

I am trying to nest tikz graph environments into the nodes of a tikz graph environment:

\begin{tikzpicture}
    \graph [layered layout]{
        ABABAB -> {
            AB -> {A, B},
            BA -> {A, B}
        };
    };
\end{tikzpicture}

enter image description here

I got this so far and I need to make each node its own graph with a similar layout.

I also want to be able to connect nodes from the inner graphs with nodes of the outer graph.

I could create all nodes individually and connect them, with nested nodes like this:

\node [draw] at (0,0) (container1) {
    \begin{tikzpicture}
        \node at (0,0) (container1label) {Node 1};
        \node [draw,below=0.4 of container1label] (child1) {Node 1's Child};
    \end{tikzpicture}
};

But I would like to have the layout automatically taken care of.

Ideally I would create the outer graph with invisible edges, so the layout is appropriate, and then connect the inner nodes of the graphs with each other manually.

Is there some way to nest a tikzpicture in a node created in a \graph environment?

Edit:

enter image description here

To make it more clear, this is what I am trying to model. A formal grammar composed of rules. I need an outer node for each left side of a rule and inside these outer nodes I would like to be able to create new nodes for each symbol and connect them with their corresponding outer nodes.

For example, I would like the outer node of abcdef to contain nodes for ab, cde, f, a, bc and def (preferrably easily laid out as shown in the picture, in two lines) and then I need an edge from each of these inner nodes to their corresponding outer node, which in turn contains its rule's symbols.

A simpler solution might be to only connect the outer nodes with each other if they have rules pointing to each other, but I wouldn't even know how to define a math text content for each node in the graph environment. Preferrably I would like to connect each inner symbol node with each outer rule node, though.

So similar to this:

enter image description here

13
  • Welcome to tex.sx. I'm not really practiced at this, but I think that tikzpictures can be nested, and if so, each node could be one, with a "big" one wrapped around all the little ones. Sep 12, 2023 at 23:14
  • 1
    You can nest nodes @stimulate, but you shouldn't nest tikzpictures. The results of trying to do so are (almost) always unpredictable and (almost) always not what you want. The settings of the outside picture will change the output of the inner one, while the stuff inside the inner one won't be accessible in the outer one. You can use things such as tikzmark to create subnodes you can refer to outside the current tikzpicture (or outside one) and you can use remember picture, for example, but nesting them is a can of worms.
    – cfr
    Sep 12, 2023 at 23:26
  • 1
    If you provide a complete minimal example and explain what you want to be able to do, there is a high probability you'll get one or more responses explaining alternative approaches to achieve what you want.
    – cfr
    Sep 12, 2023 at 23:27
  • 1
    @cfr -- I retract the suggestion! (but not the welcome) Sep 13, 2023 at 0:22
  • 1
    As @cfr said, please provide an example on how your graph looks like. The graphdrawing supports sublayouts and if that doesn't do what you need and as long as you don't need a transformation you might be able to use a PGF/TikZ matrix which is very close to putting a TikZ picture inside a node but actually supported. Whether graphdrawing can deal with it is another story, though. Sep 13, 2023 at 7:39

1 Answer 1

5

The sublayouts certainly provide one solution for this.

Code

% !TeX TS-program = lualatex
\documentclass[tikz]{standalone}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{layered}
\begin{document}
\tikz[
  text depth=+0pt, text height=+.7em, inner sep=+.2em,
  subgraph text none, anchor=base,
  subgraph nodes={
    draw=lightgray, rectangle, rounded corners, inner sep=+.333em,
    text depth=, text height=}]
\graph [layered layout, fresh nodes, nodes behind edges] {
  XYABCDEFXY//[layered layout]{ xyabcdefxy -- { xy, abcdef, xy } },
  XY//[layered layout]{ xy -- {x, y} },
  ABCDEF//[layered layout]{
    abcdef -- {
      ab  -- { a,    bc  },
      cde -- { (bc), def }, % (bc) to connect to the previous bc node
      f   -- (def)          % and not create a new one
    }
  },
  AB //[layered layout]{ ab  -- { a,  b  }},
  CDE//[layered layout]{ cde -- { c,  de }},
  BC //[layered layout]{ bc  -- { b,  c  }},
  DEF//[layered layout]{ def -- { de, f  }},
  DE //[layered layout]{ de  -- { d,  e  }},
  % all nodes in () because we want to use the existing ones
  {(xy), (xy')} -- (XY),
  (abcdef) --               (ABCDEF),
  (ab)     --               (AB),
  (cde)    --[bend left=15] (CDE),
  (bc)     --               (BC),
  (def)    --               (DEF),
  (de)     --               (DE)
};
\end{document}

Output

enter image description here

3
  • 1
    The graphing stuff is so impressive. Too bad we only got a picture when the OP had run out of time.
    – cfr
    Sep 15, 2023 at 1:54
  • @cfr I am not actually out of time yet! This is great :) Thank you so much Qrrbrbirlbel , its exactly what I need!
    – stimulate
    Sep 15, 2023 at 12:42
  • To be clear, I meant a picture in the question. (@stimulate)
    – cfr
    Sep 15, 2023 at 16:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .