我需要有关 tikz 树节点和标签、偏移和倾斜度的帮助

我需要有关 tikz 树节点和标签、偏移和倾斜度的帮助

我需要画一些类似的东西:

我的问题是黑点的位置,即节点 S 和 M 和标签的位置,而且数字标签看起来也不太好。

X 节点位于 Coke 节点旁边,不在 Cabonated_Drinks 和 Coke 之间的边缘上。

在此处输入图片描述

这是我得到的:

\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}

是什么给了我这样的图像: 在此处输入图片描述

提前致谢。

答案1

这里有两个可能有用的键:posshift。使用这些,我得到了

\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}

在此处输入图片描述

从长远来看,转向森林可能会对你有益。`

答案2

正如@marmot 所说,使用带有标签和节点的 xshift 和 yshift 已解决...

改进后的代码如下:

\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}

我有:

在此处输入图片描述

感谢所有人@marmot、@ferahfeza

相关内容