
有關的:「在命令後面附加」和「插入路徑」的問題,但我不明白如何在我的案例中應用這些解決方案。
我正在定義一個簡單的 tikz 樣式,它使用append after command
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit}
\begin{document}
\begin{tikzpicture}[
fit after/.style={
append after command={%
node[draw, inner sep=0pt, fit=(\tikzlastnode)] {}
}
}
]
\node[fit after] {A};%%%% WORKS
\node {B} child { node[fit after] {C} }; %%%% FAILS
\end{tikzpicture}
\end{document}
在第一個節點 ( A
) 上,它工作正常並在其周圍繪製一個矩形。
但是,在C
節點上,我收到以下錯誤訊息:
Package pgf Error: No shape named is known.
這\tikzlastnode
並不是錯誤的,因為在這種情況下,即使對節點名稱進行硬編碼仍然會失敗child
。
路徑操作是否child
混亂append after command
?有解決方法嗎?
上下文:我正在嘗試建立一個triangle fit whole subtree
命令,該命令繪製一個適合子樹中所有節點的三角形。
答案1
如果forest
是一個選項,那麼定義fit whole subtree
樣式相當簡單。我不推薦三角形。雖然我做了一些嘗試,但這不可避免地看起來很醜。但是,fit whole subtree
可以傳遞您希望設定節點樣式的任何選項。預設情況下,它繪製一個紅色矩形。
請注意, 的多次使用fit whole subtree
(包括嵌套使用)可以正常運作。
\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\usetikzlibrary{fit}
\begin{document}
\newcommand*\makenodename[1]{(#1)}
\forestset{
declare toks={fit these}{},
fit whole subtree/.style={
delay={
temptoksa=,
for tree={%
temptoksa+/.wrap pgfmath arg={ (##1)}{name()},
},
fit these/.register=temptoksa,
delay={
tikz+={
\node [draw=red, fit=\foresteoption{fit these}, inner sep=0pt, #1] {};
},
},
},
},
}
\begin{forest}
for tree={
fit=band,
child anchor=parent,
parent anchor=children,
}
[A
[B
[C, fit whole subtree
[D]
[E
[X]
[Y
[Z]
]
]
[F]
[G]
]
[H
[I
[J]
[K]
[L]
]
]
]
[M, fit whole subtree={thick, draw=blue, rounded corners}
[N
[O
[R]
]
[P]
[Q]
]
[S, fit whole subtree={green, densely dashed, rounded corners}
[T]
[U]
[V]
[W]
]
]
]
\end{forest}
\end{document}