
我想用不同的顏色突出顯示樹的不同區域,並用拱形線將它們分開。有沒有辦法做到這一點?
我想要實現的結果是這樣的:
我的樹的程式碼如下:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{soul}
\usepackage{tikz}
\usepackage{forest}
\usepackage{ulem}
\usetikzlibrary{positioning,tikzmark}
\begin{document}
\begin{forest}
for tree={calign=fixed edge angles},
[CP [,phantom]
[C'
[C]
[TP [,phantom]
[T'
[T]
[VP [,phantom]
[V'
[V] [,phantom]]]]]]]
\end{forest}
\end{document}
預先感謝任何有解決方案的人!
答案1
透過使用該calc
庫和fillbetween
提供的庫的一些技巧pgfplots
,您可以執行以下操作(我認為這可能不是實現此目的的最簡單方法):
\documentclass[border=10pt]{standalone}
\usepackage{forest}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{calc, backgrounds}
\begin{document}
\begin{forest}
for tree={calign=fixed edge angles},
[CP,name=cp [,phantom]
[C'
[C]
[TP,name=tp [,phantom]
[T'
[T]
[VP,name=vp [,phantom]
[V'
[V] [,phantom]]]]]]]
\draw[rotate=30, name path=cpbow] ([shift={(150:2 and 0)}]cp)
arc[start angle=150, end angle=30, x radius=2, y radius=1];
\draw[rotate=30, name path=tpbow] ([shift={(150:2 and 0)}]tp)
arc[start angle=150, end angle=30, x radius=2, y radius=1];
\draw[rotate=30, name path=vpbow] ([shift={(150:2 and 0)}]vp)
arc[start angle=150, end angle=30, x radius=2, y radius=1];
\path[rotate=30, name path=xpbow] ([shift={(150:2 and 0)}]$(vp)!-1!(tp)$)
arc[start angle=150, end angle=30, x radius=2, y radius=1];
\begin{scope}[on background layer]
\fill[red, opacity=0.25, intersection segments={
of=cpbow and tpbow, sequence={A* -- B*[reverse]}
}] -- cycle;
\fill[yellow, opacity=0.25, intersection segments={
of=tpbow and vpbow, sequence={A* -- B*[reverse]}
}] -- cycle;
\fill[green, opacity=0.25, intersection segments={
of=vpbow and xpbow, sequence={A* -- B*[reverse]}
}] -- cycle;
\end{scope}
\end{forest}
\end{document}
解釋:
- 我先繪製三個橢圓弧,它們相對於樹中的
CP
、TP
和節點定位。我透過使用提供的選項VP
命名樹中的節點來做到這一點name
forest
。 - 我創建了相對於節點(座標)的第四條弧,該弧距 VP 的距離與 VP 距 TP 的距離一樣遠,但方向相反。該計算可以使用
calc
庫和語法來實現$(vp)!-1!(tp)$
來實現。 - 我使用以下命令命名這四個弧
name path
,然後使用intersection segments
fillbetween
PGFplots 附帶的庫提供的選項(在手冊目前第 5.7 節)在它們之間創建填充物。 - 我將三個填充物放在背景圖層上,這樣它們就不會覆蓋節點。
使用陰影的變體:
\documentclass[border=10pt]{standalone}
\usepackage{forest}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{calc, backgrounds}
\begin{document}
\begin{forest}
for tree={calign=fixed edge angles},
[CP,name=cp [,phantom]
[C'
[C]
[TP,name=tp [,phantom]
[T'
[T]
[VP,name=vp [,phantom]
[V'
[V] [,phantom]]]]]]]
\draw[rotate=30, name path=cpbow] ([shift={(150:2 and 0)}]cp)
arc[start angle=150, end angle=30, x radius=2, y radius=1];
\draw[rotate=30, name path=tpbow] ([shift={(150:2 and 0)}]tp)
arc[start angle=150, end angle=30, x radius=2, y radius=1];
\draw[rotate=30, name path=vpbow] ([shift={(150:2 and 0)}]vp)
arc[start angle=150, end angle=30, x radius=2, y radius=1];
\path[rotate=30, name path=xpbow] ([shift={(150:2 and 0)}]$(vp)!-1!(tp)$)
arc[start angle=150, end angle=30, x radius=2, y radius=1];
\begin{scope}[on background layer]
\shade[left color=red!25, right color=red!0, shading angle=30, intersection segments={
of=cpbow and tpbow, sequence={A* -- B*[reverse]}
}] -- cycle;
\shade[left color=yellow!25, right color=yellow!0, shading angle=30, intersection segments={
of=tpbow and vpbow, sequence={A* -- B*[reverse]}
}] -- cycle;
\shade[left color=green!25, right color=green!0, shading angle=30, intersection segments={
of=vpbow and xpbow, sequence={A* -- B*[reverse]}
}] -- cycle;
\end{scope}
\end{forest}
\end{document}