2

What the TikZ/pgf manual says:

In section 27.10, on page 391, the manual begins by giving an example in "plain" syntax example of how scopes might be set up, and what use they might be:

\tikz [spring layout] 
{
    \begin{scope}[tree layout]
        \node (a) {a};
        \node (b) {b};
        \node (c) {c};
        \draw (a) edge (b) edge (c);
    \end{scope}

    \begin{scope}[tree layout]
        \node (1) {1};
        \node (2) {2};
        \draw (1) edge (2);
    \end{scope}

\draw (a) edge (1);
}

One can use different algorithms to draw different "sublayouts" of the entire graph. Great. I then tried to use the syntax provided on pg. 391 itself to specify some sublayout options in order to draw a circle around the sublayout.

However, nothing happens. Everything compiles the same as usual, and everything looks the same as usual.

The MWE:

\documentclass{standalone}

%======================================
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{arrows}
\usetikzlibrary{graphs}
\usetikzlibrary{decorations.pathmorphing}
\usegdlibrary{force, layered, trees}
%======================================

\begin{document}

\tikz [rounded corners, spring layout, node distance=20mm]
    {
        \begin{scope}
            \node (a) {a};
        \end{scope}

        //  [draw, circle]
        {
            \begin{scope}[tree layout, node distance=5mm]
                \node (b) {b};
                \node (c) {c};
                \node (d) {d};

                \draw
                    (b) edge[->] (c)
                    (b) edge[->] (d)
                    (c) edge[->] (d);
            \end{scope}
        }

        \draw
        (a) edge[->] (b);
    }

\end{document}

So, what is the correct way to go about drawing a circle around the "node" that consists of the sublayout?

3
  • Scopes are mostly useful for rotating parts of a picture. The best way to draw a circle around a picture is to use \tikz{} inside the node. May 20, 2014 at 13:41
  • @JohnKormylo That's really not the best way to do it. Nesting should be discouraged.
    – percusse
    May 20, 2014 at 17:28
  • I stand corrected. May 20, 2014 at 22:03

1 Answer 1

6

You can use the fit library to draw a circle around the nodes of the sublayout. But in that case you have to use a scope for the layout.

\documentclass[tikz,margin=5pt]{standalone}
\usetikzlibrary{graphs,graphdrawing,arrows}
\usetikzlibrary{fit}
\usegdlibrary{force, trees}

\begin{document}

\tikz [>=latex']{
  \begin{scope}[spring layout, node distance=20mm]
    \node (a) {a};
    //{
      \begin{scope}[tree layout, node distance=5mm]
        \node (b) {b};
        \node (c) {c};
        \node (d) {d};
        \draw
          (b) edge[->] (c)
          (b) edge[->] (d)
          (c) edge[->] (d);
      \end{scope}
    \draw(a) edge[->] (b);
    }
  \end{scope}
  \node[circle,draw,fit=(b) (c) (d)]{};
}
\end{document}

enter image description here

Or with the same result:

\documentclass[tikz,margin=5pt]{standalone}
\usetikzlibrary{graphs,graphdrawing,arrows}
\usegdlibrary{force,trees}

\begin{document}
\tikz[spring layout, node distance=20mm,>=latex']
  \graph{
    a,
    mytree[subgraph text none,draw,circle]//[tree layout]{b->{c,d}, c->d},
    a->b
  };
\end{document}

You must log in to answer this question.

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