刪除圓圈並為樹的元素添加顏色

刪除圓圈並為樹的元素添加顏色

問題:我正在尋找下面添加的圖像。我想為每個節點添加不同的顏色。

這是我到目前為止所做的:

\documentclass{article} 
\usepackage{forest}

\begin{document}
    \large
    \begin{forest}
        for tree={circle,draw, l sep=10pt}
        [36,black 
        [2]
        [18
        [2] 
        [9
        [3]
        [3]
        ]       
        ]
        ]
        \end{forest}
\end{document}

在此輸入影像描述

答案1

你的意思是這樣嗎?只需tikz在節點文字後面新增繪圖參數(例如draw=redfill=green、 和text=blue)。您可以透過新增 來刪除給定節點的圓圈draw=none

\documentclass{article} 
\usepackage{forest}

\begin{document}
    \large
    \begin{forest}
        for tree={circle,draw, l sep=10pt}
        [36,draw=none 
        [2,fill=red]
        [18,draw=none
        [2,fill=yellow] 
        [9,draw=none
        [3,fill=green]
        [3,fill=cyan]
        ]       
        ]
        ]
        \end{forest}
\end{document}

輸出

或者

\documentclass{article} 
\usepackage{forest}
\newcounter{nextcolor}
\xdef\LstColor{{"red","yellow","green","blue","orange","magenta","cyan"}}
\tikzset{fill next color/.style={%
/utils/exec=\pgfmathsetmacro{\mycolor}{\LstColor[\number\value{nextcolor}]}
\stepcounter{nextcolor},
fill=\mycolor}}
\begin{document}
    \large
    \begin{forest}
        for tree={l sep=10pt,
        if n children=0{circle,draw,fill next color}{}}
        [36,black 
        [2]
        [18
        [2] 
        [9
        [3]
        [3]
        ]       
        ]
        ]
    \end{forest}
\end{document}

相關內容