I have drawn the following tree using tikz with the following code.
My question is:
How can I draw one big circle enclosing all the „A“ nodes, one for all „B“ nodes and so on (maybe with different colours)?
I hope it is no problem that the circles will intersect one another sometimes.
Here's the code:
\documentclass[landscape]{article}
\usepackage[a4paper,left=1cm,right=1cm,top=2cm,bottom=2cm,bindingoffset=5mm]{geometry}
\usepackage{tikz}
\usepackage{tikz-qtree}
\makeatletter
\let\old@@children\@@children
\def\@@children{\futurelet\my@next\my@@children}
\def\my@@children{%
\ifx\my@next\missing\else
\expandafter\@gobble
\fi
\expandafter\old@@children}
\makeatother
\newcommand{\missing}{ \edge[draw=none]; {} }
%intended usage inside tikzpicture enviornment:
%\Tree [.3 1
% \missing ]
%To producce the same result as code below but
%it gave me an error:
%! Undefined control sequence.
%\missing -> \edge
% [draw=none]; {}
\begin{document}
\begin{tikzpicture}[level distance=30pt,sibling distance=5pt]
\Tree
[.$M$
\edge node[auto=right]{1};
[.$L$
\edge node[auto=right]{1};
[.$K$
\edge[dotted];
[.$J$
\edge node[auto=right]{1};
[.$H$
\edge node[auto=right]{2};
[.$G$
\edge node[auto=right]{1};
[.$G$
\edge node[auto=right]{1};
[.$B,G$
\edge node[auto=right]{1};
[.$B$
\edge node[auto=right]{1};
[.$A,B$
\edge node[auto=right]{1};
[.$A$
\edge node[auto=right]{1};
[.$A$
\edge node[auto=right]{1};
{$...$}
\edge node[auto=left]{2};
{$...$}
]
]
\edge node[auto=left]{2};
[.$A$
\edge node[auto=left]{2};
[.$A$
\edge node[auto=right]{1};
{$...$}
\edge node[auto=left]{2};
{$...$}
]
]
]
]
\edge node[auto=left]{2};
[.$B$
\edge node[auto=left]{2};
[.$B,C$
\edge node[auto=right]{1};
[.$C$
\edge node[auto=right]{1};
[.$C$
\edge node[auto=right]{1};
{$...$}
\edge node[auto=left]{2};
{$...$}
]
]
\edge node[auto=left]{2};
[.$C$
\edge node[auto=left]{2};
[.$C$
\edge node[auto=right]{1};
{$...$}
\edge node[auto=left]{2};
{$...$}
]
]
]
]
]
]
\edge node[auto=left]{2};
[.$G$
\edge node[auto=left]{2};
[.$D,G$
\edge node[auto=right]{1};
[.$D$
\edge node[auto=right]{1};
[.$D,E$
\edge node[auto=right]{1};
[.$E$
\edge node[auto=right]{1};
[.$E$
\edge node[auto=right]{1};
{$...$}
\edge node[auto=left]{2};
{$...$}
]
]
\edge node[auto=left]{2};
[.$E$
\edge node[auto=left]{2};
[.$E$
\edge node[auto=right]{1};
{$...$}
\edge node[auto=left]{2};
{$...$}
]
]
]
]
\edge node[auto=left]{2};
[.$D$
\edge node[auto=left]{2};
[.$D,F$
\edge node[auto=right]{1};
[.$F$
\edge node[auto=right]{1};
[.$F$
\edge node[auto=right]{1};
{$...$}
\edge node[auto=left]{2};
{$...$}
]
]
\edge node[auto=left]{2};
[.$F$
\edge node[auto=left]{2};
[.$F$
\edge node[auto=right]{1};
{$...$}
\edge node[auto=left]{2};
{$...$}
]
]
]
]
]
]
]
]
]
]
]
]
\end{tikzpicture}
\end{document}
Here's the image:
Thanks for the help!
Best Answer
Answer to Edited Question
This is straightforward to do with forest. I assume that with your original code, you would need to name the nodes you want to circle and then draw a circle around them, though I'm not sure. With forest, you can automatically collect nodes with particular content into a
register
which you then use as the basis for the circle. As mentioned in comments, thefit
library is the way to go here.Note that I do not find the results aesthetically pleasing as the size of the circles makes a mess of the tree. However, you explicitly said that you wanted overlapping circles and I can't think of any non-messy result you might have in mind. So hopefully this is of some use.
I would recommend loading
shapes.geometric
along withfit
and using theellipse
shape rather thancircle
for the fitting nodes:To do this, just change the line
to
and use the following modified code in
before packing={}
:Original Answer
Originally, the question asked for 'circleS' and I understood this as wanting individual circles around the various nodes. Since the code might be useful to somebody who really does want that, I leave it below.
You can use a
node
with standard TikZ optionscircle, draw
within the tree, just as you do on the edges. For example:And you can add any other TikZ options you please - coloured border, text or filling, for example - in the usual ways.
If
forest
is an option, you could probably automatically detect all theA
nodes etc. and format them appropriately. (You probably couldn't just circle theA
inA,B
, but you could circleA,B
along with theA
s if you wished - or not. At least, circling just theA
automatically would be more trouble than it would be worth, I think.)Here's an example which draws and fills circles for all nodes with the content
A
(red),B
(green) orC
(magenta); draws and fills a box for all nodes whose content containsD
(blue/cyan), and highlights a node and all of its ancestors, including the edges drawn between them (blue). It also largely automates the content and location of the edge labels, while showing how to override this where necessary.