3

I need to draw something like this:

My problem are the black dots position i.e. nodes S and M and the labels position, also the number labels does not look good too.

The X node is beside Coke node not in the edge between Cabonated_Drinks and Coke.

enter image description here

this is what i got:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}

\tikzset{
  solid node/.style={circle,draw,inner sep=1.2,fill=black},
  hollow node/.style={circle,draw,inner sep=1.2},
}

\begin{document}
    \begin{tikzpicture}[font=\footnotesize]
    \tikzset{
        level 1/.style={level distance=15mm,sibling distance=45mm},
        level 2/.style={level distance=15mm,sibling distance=30mm},
        level 3/.style={level distance=15mm,sibling distance=15mm},
        level 4/.style={level distance=15mm,sibling distance=10mm},
    }

    \node(A)[hollow node,label=above:{ALL}]{}
    child {
        %node(B)[hollow node,label=left:{Carbonated Drinks}]{}
        node(B)[hollow node,label={[align=left]left:Carbonated\\Drinks}]{}
        child {node(D)[hollow node, label=below:{Coke}]{}
            edge from parent{
                node[solid node, left, label=left:{X}, at end]{}
                node[right]{1}
            }
        }
        child {node(E)[hollow node, label=below:{Pepsi}]{}
            edge from parent{
                node[left]{1}
                node[solid node, right, near start, label=right:{T}]{}
            }
        }
        edge from parent{
            node[solid node, left, near start, label=left:{S}]{}
            node[solid node, right, near start, label=right:{M}]{}
        }
        edge from parent{
            node[right]{1}
        }
    }
    child {
        node(C)[hollow node,label=right:{Coffee}]{}
        child {
            node(F)[hollow node, label=below:{Mocca}]{}
            edge from parent{
                node[right]{1}
            }
        }
        child {
            node(G)[hollow node, label=below:{Nescaffe}]{}
            edge from parent{
                node[left]{1}
            }
        }
        edge from parent{
            node[left]{1}
        }
    };
    \end{tikzpicture}
\end{document}

What gives me an image like this: enter image description here

Thanks in advance.

  • 2
    Welcome to TeX.SE! And the proble you have are the positions of the black points? Please do not only show a image and some code, explain your problem in words too! – Mensch Mar 10 at 21:32
  • [edited] My problem are the black dots position and the labels position, also the number labels does not look good too. – thecharliex Mar 10 at 21:42
  • You can add above or below or xshift=...mm or yshift=...mm into node definitions for black dots positions. – ferahfeza Mar 10 at 22:31
  • It's not customary to add “SOLVED” to the title. – egreg Mar 10 at 23:23
3

There are two keys which might be useful here: pos and shift. Using these, I got

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{amsmath}

\tikzset{
  solid node/.style={circle,draw,inner sep=1.2,fill=black},
  hollow node/.style={circle,draw,inner sep=1.2},
}

\begin{document}
    \begin{tikzpicture}[font=\footnotesize]
    \tikzset{
        level 1/.style={level distance=15mm,sibling distance=45mm},
        level 2/.style={level distance=15mm,sibling distance=30mm},
        level 3/.style={level distance=15mm,sibling distance=15mm},
        level 4/.style={level distance=15mm,sibling distance=10mm},
    }

    \node(A)[hollow node,label=above:{ALL}]{}
    child {
        %node(B)[hollow node,label=left:{Carbonated Drinks}]{}
        node(B)[hollow node,label={[align=left,xshift=1.5em]135:Carbonated\\Drinks}]{}
        child {node(D)[hollow node, label=below:{Coke}]{}
            edge from parent{
                node[solid node,pos=0.95,xshift=-0.5em,left, label=left:{X}]{}
                node[pos=0.6,above left]{1}
            }
        }
        child {node(E)[hollow node, label=below:{Pepsi}]{}
            edge from parent{
                node[pos=0.6,below left]{1}
                node[solid node, right, pos=0.4, label=right:{T}]{}
            }
        }
        edge from parent{
            node[solid node, left, pos=0.35,shift={(120:2pt)}, label=120:{S}]{}
            node[solid node, right, pos=0.35,shift={(-60:2pt)}, label=-60:{M}]{}
            node[pos=0.6,above left]{1}
            }
    }
    child {
        node(C)[hollow node,label=right:{Coffee}]{}
        child {
            node(F)[hollow node, label=below:{Mocca}]{}
            edge from parent{
                node[pos=0.6,above left]{1}
            }
        }
        child {
            node(G)[hollow node, label=below:{Nescaffe}]{}
            edge from parent{
                node[pos=0.6,above right]{1}
            }
        }
        edge from parent{
            node[pos=0.6,above right]{1}
        }
    };
    \end{tikzpicture}
\end{document}

enter image description here

On the long run you may benefit from switching to forest.`

1

SOLVED using properly xshift and yshift with labels and nodes as @marmot says...

Here is the improved code:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}

\tikzset{
  solid node/.style={circle, draw, inner sep=2, fill=black, line width=1pt},
  hollow node/.style={circle, draw, inner sep=2, line width=1pt },
}

%child{node(l1)[hollow node, label=below:{D}]{}

\begin{document}
    \begin{tikzpicture}[font=\footnotesize]
    \tikzset{
        level 1/.style={level distance=15mm,sibling distance=45mm, line width=1pt},
        level 2/.style={level distance=15mm,sibling distance=30mm},
        level 3/.style={level distance=15mm,sibling distance=15mm},
        level 4/.style={level distance=15mm,sibling distance=10mm},
    }

    \node(A)[hollow node,label=above:{ALL}]{}
    child {
        %node(B)[hollow node,label=left:{Carbonated Drinks}]{}
        node(B)[hollow node,label={[align=left]left:Carbonated\\Drinks}]{}
        child {node(D)[hollow node, label=below:{Coke}]{}
            edge from parent{
                node[solid node, xshift=-1pt, yshift=+3pt, left, label=left:{X}, at end]{}
                node[right]{1}
            }
        }
        child {node(E)[hollow node, label=below:{Pepsi}]{}
            edge from parent{
                node[left]{1}
                node[solid node, rotate=+45pt, right, near start, label={[xshift=-1.0pt, yshift=-9.0pt]right:T}]{}
            }
        }
        edge from parent{
            node[solid node, rotate=-45, left, near start,label={[xshift=2.0pt, yshift=-7.0pt]left:S}]{}
            node[solid node, rotate=-45, right, near start, label={[xshift=-2.0pt, yshift=7.0pt]right:M}]{}
        }
        edge from parent{
            node[right]{1}
        }
    }
    child {
        node(C)[hollow node,label=right:{Coffee}]{}
        child {
            node(F)[hollow node, label=below:{Mocca}]{}
            edge from parent{
                node[right]{1}
            }
        }
        child {
            node(G)[hollow node, label=below:{Nescaffe}]{}
            edge from parent{
                node[left]{1}
            }
        }
        edge from parent{
            node[left]{1}
        }
    };
    \end{tikzpicture}
\end{document}

I got:

enter image description here

thanks all @marmot, @ferahfeza

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.