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

stimulate
  • 107
  • 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. – barbara beeton Sep 12 '23 at 23:14
  • @barbarabeeton tikzpictures can be nested, but I don't know of a way to define the contents of a node declared in a graph environment. There is the typeset key, which allows one to map the text content of all nodes to something new, but I can't put a tikzpicture in there and I would like the contents to be individual for each node. – stimulate Sep 12 '23 at 23:19
  • @barbarabeeton tikzpictures shouldn't be nested. As with most rules, there are cases where you may get away with it, but it is (almost?) always the wrong thing to do. See, for example, https://tex.stackexchange.com/questions/46598/problem-with-overlay-when-a-tikzpicture-is-inside-another-tikzpicture. – cfr Sep 12 '23 at 23:19
  • 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 '23 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 '23 at 23:27
  • If your graph has a tree structure (or close), you could even use forest if you don't need the automatic layout facilities of LuaTeX. But it isn't clear from your current question really what you're dealing with. – cfr Sep 12 '23 at 23:31
  • @cfr I will look into forest. I am trying to render a DAG structure where each node contains inner nodes. These inner nodes point to nodes in the outer graph.

    I am currently looking at defining all the inner nodes in a single graph and then surrounding them with nodes using fit. But here the problem is I can't reference the wrapper nodes in the original graph. There is also declare for tikz graph macros.. but it doesn't create a node for the entire macro. I just need to define a graph within a node..

    – stimulate Sep 12 '23 at 23:47
  • 1
    @cfr -- I retract the suggestion! (but not the welcome) – barbara beeton Sep 13 '23 at 00:22
  • @stimulate I don't know what a DAG structure is (or even which discipline it belongs to). By the 'original graph' do you mean the inner one? So the concern is you can't reference the outer nodes (the 'fit' nodes) from the inner nodes? If they are all in the same tikzpicture, you can. There are even ways of referencing nodes which you'll only draw later in the picture. (You get nonsense output on the first run, but that's just like the ?? for cross-references. Just more colourful.) – cfr Sep 13 '23 at 01:47
  • Can you just draw a graph within a node? That is, don't nest it in a tikzpicture. Just put it in a node? (I haven't used the graph stuff much, so the answer could easily be a flat 'No'.) – cfr Sep 13 '23 at 01:48
  • How complicated is the overall picture? Are you sure you need the graphing facilities? (Assuming you can't put them in nodes. If you can, that ought to work? Unless I've misunderstood ....) – cfr Sep 13 '23 at 01:51
  • 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. – Qrrbrbirlbel Sep 13 '23 at 07:39
  • @Qrrbrbirlbel Unfortunately I am on a deadline, and I have decided to not invest more time in this for now, but if it makes it more clear and helps resolve this question, what I am trying to render is a formal grammar structure sort of like in the new picture I added. Each node should be a rule with an "outer node" for the left hand side of the rule and "inner nodes" for the right hand side. Inside and outer node I would like to have nodes for each rule symbol which I can connect to their corresponding outer nodes. – stimulate Sep 14 '23 at 20:28

1 Answers1

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

Qrrbrbirlbel
  • 119,821
  • 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 '23 at 01: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 '23 at 12:42
  • To be clear, I meant a picture in the question. (@stimulate) – cfr Sep 15 '23 at 16:05