具有良好标签和边缘的树

具有良好标签和边缘的树

我想做一棵这样的树:

好树

但我只能做到这一点:

坏树

使用以下代码:

\begin{forest} for tree={grow=north, parent anchor=north,child anchor=south} [a[b[c]]] \path[fill=red] (.parent anchor) circle[radius=2pt]; \path[fill=blue](!1.child anchor) circle[radius=2pt]; \end{forest}

答案1

看来使用纯 TikZ 是最简单的方法:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{positioning}
\tikzset{
    node distance= 4mm and 2mm,
    arr/.style = {thick, draw=#1},
    arr/.default = red,
    dot/.style = {circle, draw, very thin, fill, inner sep=0pt, outer sep=0pt, 
                  minimum size=3pt,
                  label=right:$#1$,
                  node contents={}},
    every label/.append style = {font=\scriptsize, inner sep=1pt}
        }
\begin{document}
    \begin{tikzpicture}
\node (a) [dot=a];
\node (b) [dot=b,above=of a];
\node (c) [dot=c,above=of b];
\draw[arr] (a) -- (b);
\draw[arr=green] (b) -- (c);
   \end{tikzpicture}
   
    \begin{tikzpicture}
\node (a) [dot=a];
\node (b) [dot=b,above right=of a];
\node (c) [dot=c,below right=of b];
\draw[arr] (a) -- (b);
\draw[arr=green] (b) -- (c);
   \end{tikzpicture}
\end{document}

在此处输入图片描述在此处输入图片描述

相关内容