2

I'm trying to draw a graph inside a tikz tree. When I draw one node, it's ok, but when I draw several nodes... it becomes ugly :

ugly result

And the code:

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{float}

\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\usepackage{tikz-qtree}
\usepackage{tikz-qtree-compat}

\begin{document}

\begin{figure}[H]
  \centering
  \begin{tikzpicture}[]
    \Tree [.$B$ [.$A$ {
      \tikz{
        \node[draw,circle](a){$\delta_1$};
        \node[draw,circle,right of=a](b){$\delta_2$};
        \node[draw,circle,above of=a](c){$\delta_3$};
        \draw[->] (a) -- (b);
      }
    } C ] [.$D$ E F ] ]
  \end{tikzpicture}
\end{figure}

\end{document}

What would be the good way to proceed ?

Thank you in advance.

EDIT

After reading the comments, I got good results for leafs, but it doesn't work with the heads of the tree. For example, can you set the A point into a graph ? (I tried to set it as a matrix node but I've an error "ERROR: Package pgf Error: No shape named r is known.")

enter image description here

(from left to right, I used baseline=(c), then I created a matrix node and set it as baseline, and lastly I created a scope and set it as baseline)

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{float}

\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\usepackage{tikz-qtree}
\usepackage{tikz-qtree-compat}

\begin{document}

\begin{figure}[H]
  \centering
  \begin{tikzpicture}[]
    \Tree [.$B$ [.$A$
    {
      \tikz[baseline=(c),every node/.append style={inner sep=1pt}]{
        \node[draw,circle](a){$\delta_1$};
        \node[draw,circle,right of=a](b){$\delta_2$};
        \node[draw,circle,above of=a](c){$\delta_3$};
        \draw[->] (a) -- (b);
      }}
    {\tikz[baseline=(m.north),every node/.append style={inner sep=1pt}]{
        \node(m)[matrix]{
            \node[draw,circle](a){$\delta_1$};
            \node[draw,circle,right of=a](b){$\delta_2$};
            \node[draw,circle,above of=a](c){$\delta_3$};
            \draw[->] (a) -- (b);\\
        };
      }}
    {\tikz[baseline=(scope1.north),every node/.append style={inner sep=1pt}]{
      \begin{scope}[local bounding box=scope1]
        \node[draw,circle](a){$\delta_1$};
        \node[draw,circle,right of=a](b){$\delta_2$};
        \node[draw,circle,above of=a](c){$\delta_3$};
        \draw[->] (a) -- (b);
      \end{scope}
    }}
    C ] [.$D$ E F ] ]
  \end{tikzpicture}
\end{figure}

\end{document}

Any idea ?

EDIT 2

As asked, here is a picture of what I would like to have:

enter image description here

  • 1
    You can define c node as baseline of the left picture containing the three nodes to align with the others node on the same level – Salim Bou Dec 15 '16 at 13:51
  • 1
    How should it look like? As a first step, define a different baseline for the subgraph and reduce the white space inside the nodes; use e.g. \tikz[baseline=(c),every node/.append style={inner sep=1pt}]{.... – gernot Dec 15 '16 at 13:52
  • Thank you. Indeed, baseline gives very good results for the leafs, especially coupled with a matrix/scope. However if it works for the leafs, it does not works for the parents/heads... (see my edit) – tobiasBora Dec 15 '16 at 14:38
  • Can you add an image showing exactly what you want – Salim Bou Dec 15 '16 at 14:44
  • Sure, you can find it in my edit 2. – tobiasBora Dec 15 '16 at 15:02
1

Responding to original unedited question.

You can possibly scale it, e.g., \tikz[scale=0.6, every node/.style={transform shape}]. Or you can make the tree wider using distance between siblings, e.g., \begin{tikzpicture}[sibling distance=3em].

You could also use \sbox and then some shifting around. E.g.:

\documentclass[twocolumn]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{float}

\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\usepackage{tikz-qtree}
\usepackage{tikz-qtree-compat}

\begin{document}

\begin{figure}[H]
  \centering
  \caption{Starting point}
  \begin{tikzpicture}
    \Tree [.$B$ [.$A$ {
      \tikz{
        \node[draw,circle](a){$\delta_1$};
        \node[draw,circle,right of=a](b){$\delta_2$};
        \node[draw,circle,above of=a](c){$\delta_3$};
        \node[draw,circle,below of=a](d){$\delta_4$};
        \draw[->] (a) -- (b);
      }
    } C ] [.$D$ E F ] ] 
  \end{tikzpicture}
\end{figure}

\begin{figure}[H]
  \centering
  \caption{Shifted}
  \newsavebox\mysubpicA
  \sbox{\mysubpicA}{%
    \begin{tikzpicture}[remember picture, scale=1.0, every node/.style={transform shape}] %sub-picture
        \node[draw,circle](a){$\delta_1$};
        \node[draw,circle,right of=a](b){$\delta_2$};
        \node[draw,circle,above of=a](c){$\delta_3$};
        \node[draw,circle,below of=a](d){$\delta_4$};
        \draw[->] (a) -- (b);
    \end{tikzpicture}% needed, otherwise anchors are wrong!
  }
  \begin{tikzpicture}
    \Tree [.$B$ [.$A$ \node[shift={(0,-\ht\mysubpicA)}](mysubpic){\usebox{\mysubpicA}};
     C ] [.$D$ E F ] ]
  \end{tikzpicture}
\end{figure}

\begin{figure}[H]
  \centering
  \caption{Wider and shifted}
  \newsavebox\mysubpicB
  \sbox{\mysubpicB}{%
    \begin{tikzpicture}[remember picture, scale=1.0, every node/.style={transform shape}] %sub-picture
        \node[draw,circle](a){$\delta_1$};
        \node[draw,circle,right of=a](b){$\delta_2$};
        \node[draw,circle,above of=a](c){$\delta_3$};
        \node[draw,circle,below of=a](d){$\delta_4$};
        \draw[->] (a) -- (b);
    \end{tikzpicture}% needed, otherwise anchors are wrong!
  }
  \begin{tikzpicture}[sibling distance=3em]
    \Tree [.$B$ [.$A$ \node[shift={(0,-\ht\mysubpicB)}](mysubpic){\usebox{\mysubpicB}};
     C ] [.$D$ E F ] ]
  \end{tikzpicture}
\end{figure}

\begin{figure}[H]
  \centering
  \caption{Wider, scaled and shifted}
  \newsavebox\mysubpicC
  \sbox{\mysubpicC}{%
    \begin{tikzpicture}[remember picture, scale=.6, every node/.style={transform shape}] %sub-picture
        \node[draw,circle](a){$\delta_1$};
        \node[draw,circle,right of=a](b){$\delta_2$};
        \node[draw,circle,above of=a](c){$\delta_3$};
        \node[draw,circle,below of=a](d){$\delta_4$};
        \draw[->] (a) -- (b);
    \end{tikzpicture}% needed, otherwise anchors are wrong!
  }
  \begin{tikzpicture}[sibling distance=3em]
    \Tree [.$B$ [.$A$ \node[shift={(0,-\ht\mysubpicC)}](mysubpic){\usebox{\mysubpicC}};
     C ] [.$D$ E F ] ]
  \end{tikzpicture}
\end{figure}

\end{document}

enter image description here


UPDATE 1

Here is an updated version (Figure 5) without shifting but with changed anchor. To get better layout inside the tree you have to change level distance. You can do it either globally as in the example or locally for one level (see 3.3 Node placement styles in tikz-qtree documentation).

\documentclass[twocolumn]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{float}

\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\usepackage{tikz-qtree}
\usepackage{tikz-qtree-compat}

\begin{document}

\begin{figure}[H]
  \centering
  \caption{Starting point}
  \begin{tikzpicture}
    \Tree [.$B$ [.$A$ {
      \tikz{
        \node[draw,circle](a){$\delta_1$};
        \node[draw,circle,right of=a](b){$\delta_2$};
        \node[draw,circle,above of=a](c){$\delta_3$};
        \node[draw,circle,below of=a](d){$\delta_4$};
        \draw[->] (a) -- (b);
      }
    } C ] [.$D$ E F ] ] 
  \end{tikzpicture}
\end{figure}

\begin{figure}[H]
  \centering
  \caption{Shifted}
  \newsavebox\mysubpicA
  \sbox{\mysubpicA}{%
    \begin{tikzpicture}[remember picture, scale=1.0, every node/.style={transform shape}] %sub-picture
        \node[draw,circle](a){$\delta_1$};
        \node[draw,circle,right of=a](b){$\delta_2$};
        \node[draw,circle,above of=a](c){$\delta_3$};
        \node[draw,circle,below of=a](d){$\delta_4$};
        \draw[->] (a) -- (b);
    \end{tikzpicture}% needed, otherwise anchors are wrong!
  }
  \begin{tikzpicture}
    \Tree [.$B$ [.$A$ \node[shift={(0,-\ht\mysubpicA)}](mysubpic){\usebox{\mysubpicA}};
     C ] [.$D$ E F ] ]
  \end{tikzpicture}
\end{figure}

\begin{figure}[H]
  \centering
  \caption{Wider and shifted}
  \newsavebox\mysubpicB
  \sbox{\mysubpicB}{%
    \begin{tikzpicture}[remember picture, scale=1.0, every node/.style={transform shape}] %sub-picture
        \node[draw,circle](a){$\delta_1$};
        \node[draw,circle,right of=a](b){$\delta_2$};
        \node[draw,circle,above of=a](c){$\delta_3$};
        \node[draw,circle,below of=a](d){$\delta_4$};
        \draw[->] (a) -- (b);
    \end{tikzpicture}% needed, otherwise anchors are wrong!
  }
  \begin{tikzpicture}[sibling distance=3em]
    \Tree [.$B$ [.$A$ \node[shift={(0,-\ht\mysubpicB)}](mysubpic){\usebox{\mysubpicB}};
     C ] [.$D$ E F ] ]
  \end{tikzpicture}
\end{figure}

\begin{figure}[H]
  \centering
  \caption{Wider, scaled and shifted}
  \newsavebox\mysubpicC
  \sbox{\mysubpicC}{%
    \begin{tikzpicture}[remember picture, scale=.6, every node/.style={transform shape}] %sub-picture
        \node[draw,circle](a){$\delta_1$};
        \node[draw,circle,right of=a](b){$\delta_2$};
        \node[draw,circle,above of=a](c){$\delta_3$};
        \node[draw,circle,below of=a](d){$\delta_4$};
        \draw[->] (a) -- (b);
    \end{tikzpicture}% needed, otherwise anchors are wrong!
  }
  \begin{tikzpicture}[sibling distance=3em]
    \Tree [.$B$ [.\node(a){$A$}; \node[shift={(0,-\ht\mysubpicC)}](mysubpic){\usebox{\mysubpicC}};
     C ] [.$D$ E F ] ]
  \end{tikzpicture}
\end{figure}

\begin{figure}[H]
  \centering
  \caption{Anchor north, scaled, wider with changed level distance}
  \newsavebox\mysubpicD
  \sbox{\mysubpicD}{%
    \begin{tikzpicture}[remember picture, scale=.6, every node/.style={transform shape}] %sub-picture
        \node[draw,circle](a){$\delta_1$};
        \node[draw,circle,right of=a](b){$\delta_2$};
        \node[draw,circle,above of=a](c){$\delta_3$};
        \node[draw,circle,below of=a](d){$\delta_4$};
        \draw[->] (a) -- (b);
    \end{tikzpicture}% needed, otherwise anchors are wrong!
  }
  \begin{tikzpicture}[sibling distance=3em, level distance=1.5\ht\mysubpicD]
    \tikzset{execute at begin node=\strut}
    \tikzset{every tree node/.style={align=center,anchor=north}}
    \Tree [.\(B\) [.\(A\) \node(subpic){\usebox{\mysubpicD}};
     \(C\) ] [.\node(subpic)[]{\usebox{\mysubpicD}}; \(E\) \(F\) ] ]
  \end{tikzpicture}
\end{figure}

\end{document}

updated rendering

  • Thank you, I didn't know sbox, it's pretty interesting. However, it's not working when the node is inside the tree because the shift only shift the current node, but not all the subtrees... Any idea to correct it ? – tobiasBora Dec 15 '16 at 15:39
  • @tobiasBora: See my updated answer. I think you will have to do some amount of manual tweaking of the distance between the nodes both vertically and horizontally to get nice output. I do not think that completely automatic layout is possible with TikZ-Qtree, though I am not an expert in this and I could be wrong. – wilx Dec 16 '16 at 10:12
  • tikzpictures should not be nested. It is a Bad Idea. :( @tobiasBora – cfr Dec 17 '16 at 1:59
  • @cfr: Does the \sbox not do any kind of magic there that would make it not a bad idea? – wilx Dec 17 '16 at 17:42
  • @wilx The \sbox version should be fine, as far as I know, but the first solution doesn't use that and nests them directly. – cfr Dec 17 '16 at 23:01
1

This answer is inspired by wilx answers, and by Tikzpicture inside forest node label. Please, note that I do not really answer to the question, since my answer is for forest and not for tikz-tree, but I think it may be interesting to have a version for both systems.

The picture: result with forest

And the code:

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{float}
\usepackage{forest}

\usepackage{tikz}
\usetikzlibrary{positioning,arrows}

\begin{document}

\newsavebox\mysubpicC
\sbox{\mysubpicC}{%
  \begin{tikzpicture}[remember picture, scale=.6, every node/.style={transform shape}] %sub-picture
    \node[draw,circle](a){$\delta_1$};
    \node[draw,circle,right of=a](b){$\delta_2$};
    \node[draw,circle,above of=a](c){$\delta_3$};
    \node[draw,circle,below of=a](d){$\delta_4$};
    \draw[->] (a) -- (b);
  \end{tikzpicture}% needed, otherwise anchors are wrong!
}

\begin{forest}
  [\usebox{\mysubpicC}
  [\usebox{\mysubpicC}]
  [V' [\usebox{\mysubpicC}] [\usebox{\mysubpicC}] [\usebox{\mysubpicC}]]]
\end{forest}

\end{document}
  • This is safer. But why remember picture? – cfr Dec 17 '16 at 2:04
0

Here's a Forest version which simply copies the repeated graph 3 times and adds the C after the initial tree is parsed. This is possible because Forest works in a cyclical fashion, so no infinite regress is initiated. At least, I think that is why it works since you would prima facie expect copying the parent of the current node and its subtree as a child of that node would lead to an infinite loop.

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage{forest}
\usetikzlibrary{fit}
\begin{document}
\begin{forest}
  before typesetting nodes={
    delay={
      for tree={
        math content,
        circle,
        inner sep=3pt,
        parent anchor=children,
        child anchor=parent,
      },
    },
  },
  delta/.style={
    delay={
      content/.wrap value={\delta_{#1}},
      tikz+={\node [circle, fit=(), draw, inner sep=-5.5pt] {};},
    }
  },
  [B
    [, delta=3, l sep'-=5.5pt,
      [, delta=1,
        no edge,
        calign with current edge,
        before typesetting nodes={
          append'=!u,
          append'=!u,
          append'=!u,
          append={[C]}
        }
      ]
      [, delta=2, edge path'={(!s.east) -- (.west)}, edge+={->}]
    ]
    [D
      [E]
      [F]
    ]
  ]
\end{forest}
\end{document}

repeated graph copying

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.