2

The text of my terminal nodes for my tree is overlapping. Does anyone know how to place the labels on multiple lines? I checked stackoverflow but did not find a solution. I can space out the tree but I prefer to add a second line to the node because eventually, the spacing can only go so far.

I tried adding a double backslash to the text but it created an error message. Does anyone have suggestions? Below is my code and images of what I am trying to do (before and ideally after)

Thank you.

\begin{tikzpicture} [scale=1.5] 
\tikzstyle{solid node} =[circle,draw,inner sep=1.5,fill=black]
\tikzstyle{hollow node}=[circle,draw,inner sep=1.5]
\tikzstyle{level 1}=[level distance=15mm,sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=15mm,sibling distance=2.5cm]
\tikzstyle{level 3}=[level distance=15mm,sibling distance=1cm]

\node(0)[hollow node,label=above:{},align=left]{} child{node[solid node]{} child{node[hollow node,label=below,align=left:{appleJacks are great \ appleJacks are great}]{} edge from parent node[left]{a}} child{node[hollow node,label=below:{appleJacks are great, \ appleJacks are great}]{} edge from parent node[right]{b}} edge from parent node[left,xshift=-5]{action1} } child{node[solid node]{} child{node[hollow node,label=below:{appleJacks are great, appleJacks are great}]{} edge from parent node[left]{a}} child{node[hollow node,label=below:{appleJacks are great, appleJacks are great}]{} edge from parent node[right]{b}} edge from parent node[right,xshift=5]{action2} }; \drawdashed, label=above right:{my label}to(0-2);

\end{tikzpicture}

enter image description here enter image description here

1 Answers1

1

You need to increase sibling distance at level 1:

enter image description here

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{quotes}

\begin{document} \begin{tikzpicture}[scale=1.5, every label/.append style = {align=center}, solid node/.style = {circle,draw,fill=black,inner sep=1.5pt}, hollow node/.style = {circle,draw,inner sep=1.5pt}, level distance = 15mm, level 1/.style = {sibling distance=5cm}, level 2/.style = {sibling distance=2.5cm}, lbl/.style = {pos=0.55, anchor=south #1, font=\footnotesize}, ] \node(0)[hollow node]{} child{node[solid node]{} child{node[hollow node,label=below:{appleJacks are great \ appleJacks are great}] {} edge from parent node[lbl=east]{a}} child{node[hollow node,label=below:{appleJacks are great, \ appleJacks are great}]{} edge from parent node[lbl=west]{b}} edge from parent node[lbl=east]{action1} } child{node[solid node]{} child{node[hollow node,label=below:{appleJacks are great,\ appleJacks are great}]{} edge from parent node[lbl=east]{a}} child{node[hollow node,label=below:{appleJacks are great,\ appleJacks are great}]{} edge from parent node[lbl=west]{b}} edge from parent node[lbl=west]{action2} }; \drawdashed to["my label"] (0-2); \end{tikzpicture} \end{document}

Addendum:
For cases when you like to draw tres with more levels, the use of the forest package can be handy. Code for above tree cna be writen asČ

\documentclass[border=3.141592]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta, quotes}

\begin{document} \begin{forest} for tree = { % nodes circle, draw, minimum size=6pt, inner sep=0pt, /tikz/N/.style = {rectangle, draw=none, font=\small\linespread{0.84}\selectfont, inner ysep=4pt, inner xsep=0pt, text width=8.8em, align=center }, % tree if level = 2{N, child anchor=north, edge=-{Circle[open, length=6pt]} } {-}, l sep = 12mm, s sep = 2mm, %tier/.option = level, %% edge labels /tikz/ELS/.style = {% Edge Label Style pos=0.5, node font=\scriptsize, inner sep=2pt, anchor=south #1}, EL/.style = {% Edge Label if n=1{edge label={node[ELS=east]{#1}}} {edge label={node[ELS=west]{#1}}}}, } [
[ , fill, name=A, EL=action 1 [appleJacks are great appleJacks are great, EL=$a$ ] [appleJacks are great appleJacks are great, EL=$b$, ] ] [ , fill, name=B, EL=action 2 [appleJacks are great appleJacks are great, EL=$a$, ] [appleJacks are great appleJacks are great, EL=$b$, ] ] ] \draw[dashed] (A) to [ELS, "my label"] (B); \end{forest} \end{document}

You can observe, that now nodes at bottom level are rectangles (which replace label in above MWE) and the style of edges to this nodes is changes to edge=-{Circle[open, length=6pt]}.

enter image description here

Zarko
  • 296,517
  • Thank you Zarko, that works! I have one more question, - Currently it is 2 layers, how can I make it three layers (8 terminal nodes)? – ithoughtso Nov 20 '22 at 17:45
  • I finally figured out how to add a third layer. Thank you very much for taking the time to assist me with this problem that I spent hours on even after looking at tikz documentation. – ithoughtso Nov 20 '22 at 18:01
  • If you have more layers, I would look to forest package, which is dedicated for drawing/writing of trees. In your case its main advantage is, that it can define minimal distance between nodes and not need any care about sibling distance. At more layers you should be aware that tree should be sufficient small that can be placed on page (problem can happen even at landscape page orientation). For an example of forest six layer tree see https://tex.stackexchange.com/questions/586227/tree-diagram-with-nodes-information/586253#586253. – Zarko Nov 20 '22 at 20:38