2

I am trying to but a cloud around two nodes in a graph like this:

graph with a cloud shape around two node

However the cloud never ends up around the nodes but just to the right of the graph. Is there maybe a different syntax to style a group of nodes or to name them inside a graph. I used the name= option though previously I could always use a name in parenthesis.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing, fit, shapes.callouts}
\usegdlibrary{layered}

\begin{document}
\begin{tikzpicture}[
layered layout,
grow'=right,
level sep=.5cm,
sibling sep=.75cm,
nodes={draw, circle},
]
\graph {
  S[rectangle, inner sep=3mm]
  -- A
  -- { B[name=B, nudge down=4mm] -- C[name=C, nudge up=4mm] }
  -- D
  -- { E -- F, G -- H }
  -- I
  -- J
  -- K
  -- Z[rectangle, inner sep=3mm]
};
\node[draw, cloud, fit=(B)(C)] {uvw};
\end{tikzpicture}
\end{document}

2 Answers 2

4

According to the manual on p. 282, the name key gives the name to a (sub)graph, not to a node. However, the methods described there work fine.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing, fit, shapes.callouts}
\usegdlibrary{layered}

\begin{document}
\begin{tikzpicture}
\begin{scope}[
layered layout,
grow'=right,
level sep=.5cm,
sibling sep=.75cm,
nodes={draw, circle},
]
\graph[]  {
  S[rectangle, inner sep=3mm]
  -- A
  -- {[name separator=-,name=grph] B[nudge down=4mm] -- C[nudge up=4mm] }
  -- D
  -- { E -- F, G -- H }
  -- I
  -- J
  -- K
  -- Z[rectangle, inner sep=3mm]
};
\end{scope}
\node[draw, cloud,fit=(grph-B)(grph-C)] {};
\end{tikzpicture}
\end{document}

enter image description here

As long as your nodes have unique names, i.e. you do not use B, say, twice, you can just drop the name key altogether.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing, fit, shapes.callouts}
\usegdlibrary{layered}

\begin{document}
\begin{tikzpicture}
\begin{scope}[
layered layout,
grow'=right,
level sep=.5cm,
sibling sep=.75cm,
nodes={draw, circle},
]
\graph[]  {
  S[rectangle, inner sep=3mm]
  -- A
  -- { B[nudge down=4mm] -- C[nudge up=4mm] }
  -- D
  -- { E -- F, G -- H }
  -- I
  -- J
  -- K
  -- Z[rectangle, inner sep=3mm]
};
\end{scope}
\node[draw, cloud,fit=(B)(C)] {};
\end{tikzpicture}
\end{document}
1
  • Your answer changes more than necessary though it helped me spot my mistake. Also thanks for referencing out the manual - it helped me with a problem I encountered after fixing this.
    – Septatrix
    Commented Jun 18, 2020 at 16:13
1

I actually found the problem after seeing @Schrödinger's cat's answer. The problem was actually setting the layered layout option on the whole environment instead of just the graph. This somehow overwrites the normal positioning und puts the cloud to the right. Moving it solves the problem:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing, fit, shapes.callouts}
\usegdlibrary{layered}

\begin{document}
\begin{tikzpicture}[
  grow'=right,
  level sep=.5cm,
  sibling sep=.75cm,
  nodes={draw, circle},
]
\graph[layered layout] {
  S[rectangle, inner sep=3mm]
  -- A
  -- B[nudge down=4mm]
  -- C[nudge up=4mm]
  -- D
  -- { E -- F, G -- H }
  -- I
  -- J
  -- K
  -- Z[rectangle, inner sep=3mm]
};
\node[draw, cloud, fit=(B)(C)] {};
\end{tikzpicture}
\end{document}
2
  • 2
    Unfortunately, the name key does not name the node here in the way suggested by the code. Try ... B[name=B1, nudge down=4mm] ... first without and then with \node[draw, cloud, fit=(B1)(C)] {};. However, \node[draw, cloud, fit=(B)(C)] {}; works because the node will get the name B.
    – user194703
    Commented Jun 18, 2020 at 16:42
  • Yes the subgraph is also not needed. I just forgot to remove that from the MWE and now corrected this to not confuse others.
    – Septatrix
    Commented Jun 18, 2020 at 17:18

You must log in to answer this question.

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