在接下来的M(erly)WE中,这是我的目标。
- 我想以颜色作为论据。
- 我想装饰从根到叶子的所有边和节点。
以下是代码的输出。
我正在尝试使用for nodewalk
但不知道如何使用...
- ...将节点定义为参数或使用样式,
- ...将颜色定义为参数,
- ... 在每个节点周围放一个框架,
- ... 为彩色边缘提供良好的左点。
以下是部分代码。
\documentclass[12pt,a4paper]{article}
\usepackage{forest}
\begin{document}
\section{Mark all nodes}
\begin{forest}
for tree = {
grow' = 0,
l = 2.5cm,
parent anchor=east,
s sep = 1.2cm,
},
mark/.style = {
mark node,edge=mark line
},
mark node/.style = {
draw = red,
line width = 2pt,
rounded corners
},
/tikz/mark line/.style={
red,
line width = 2.5pt,
line cap = round,
shorten >= -1pt, % this (TikZ) key deals with "links" between edges and nodes
},
[
[A, mark=red
[B]
[C,mark=blue]
]
[D, mark=red
[E, mark=red
[F]
[G, mark=red]
[H]
]
]
]
\end{forest}
\section{Try to indicate only the leaf}
\begin{forest}
for tree = {
grow' = 0,
l = 2.5cm,
parent anchor=east,
s sep = 1.2cm,
},
before typesetting nodes={
for nodewalk={
c,
every step={
tikz/.wrap pgfmath arg={
\draw[red,
line width = 2.5pt,
line cap = round] ()--(#1);
}{name("!b")}
},
211,ancestors
}{}
},
[
[A,
[B]
[C]
]
[D
[E
[F]
[G]
[H]
]
]
]
\end{forest}
\end{document}
答案1
我不完全确定你想做什么。(你是想为了智力练习还是出于某些实际原因而使用 nodewalk?例如,也许你想在序言中指定叶子,而不是在叶子中指定为参数。如果是这样,下面的方法虽然可以调整,但不能立即使用。
\documentclass{article}
\usepackage{forest}
\begin{document}
\section{Mark all nodes}
\tikzset{
mark line/.style={
#1,
line width = 2.5pt,
line cap = round,
shorten >= -1pt, % this (TikZ) key deals with "links" between edges and nodes
},
}
\forestset{
mark/.style = {
mark node=#1,edge={mark line=#1}
},
mark node/.style = {
draw = #1,
line width = 2pt,
rounded corners
},
}
\begin{forest}
for tree = {
grow' = 0,
l' = 2.5cm,
parent anchor=east,
s sep' = 1.2cm,
},
[
[A, mark=red
[B]
[C,mark=blue]
]
[D, mark=red
[E, mark=red
[F]
[G, mark=red]
[H]
]
]
]
\end{forest}
\section{Try to indicate only the leaf}
\forestset{%
mark me/.style={%
for nodewalk={while nodewalk valid={parent}{current,fake=parent}}{mark=#1}
},
}
\begin{forest}
for tree = {
grow' = 0,
l' = 2.5cm,
parent anchor=east,
s sep' = 1.2cm,
},
[
[A,
[B]
[C]
]
[D
[E
[F, mark me=green]
[G]
[H]
]
]
]
\end{forest}
\end{document}
答案2
我不知道我是否正确理解了你的问题...仅使用tikz
,我建议以下解决方案。节点和边缘有两种样式:snode
简单、cnode
彩色...
\documentclass[12pt, border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{%
node distance=1.5cm and 0.2cm,
snode/.style={rectangle, rounded corners},
cnode/.style={draw, red, rectangle, rounded corners, line width=1.75pt, text=black},
sedge/.style={black, thin},
cedge/.style={red, line width=1.75pt}
}
\begin{tikzpicture}
\node[inner sep=0pt, outer sep=0pt] (R) {};
\node[snode, above right=1.5cm and 2cm of R] (A) {A};
\node[snode, below right=1.5cm and 2cm of R] (D) {D};
\node[snode, above right=1cm and 2cm of A] (B) {B};
\node[snode, below right=1cm and 2cm of A] (C) {C};
\node[snode, right=2cm of D] (E) {E};
\node[snode, above right=1.3cm and 2cm of E] (F) {F};
\node[snode, right=2cm of E] (G) {G};
\node[snode, below right=1.3cm and 2cm of E] (H) {H};
\path
(R) edge[sedge] (A) (A.east) edge[sedge] (B)
(R) edge[sedge] (A) (A.east) edge[sedge] (C)
(R) edge[sedge] (D) (D.east) edge[sedge] (E) (E.east) edge[sedge] (F)
(R) edge[sedge] (D) (D.east) edge[sedge] (E) (E.east) edge[sedge] (G)
(R) edge[sedge] (D) (D.east) edge[sedge] (E) (E.east) edge[sedge] (H);
\end{tikzpicture}
\qquad
\begin{tikzpicture}
\node[inner sep=0pt, outer sep=0pt] (R) {};
\node[snode, above right=1.5cm and 2cm of R] (A) {A};
\node[cnode, below right=1.5cm and 2cm of R] (D) {D};
\node[snode, above right=1cm and 2cm of A] (B) {B};
\node[snode, below right=1cm and 2cm of A] (C) {C};
\node[cnode, right=2cm of D] (E) {E};
\node[cnode, above right=1.3cm and 2cm of E] (F) {F};
\node[snode, right=2cm of E] (G) {G};
\node[snode, below right=1.3cm and 2cm of E] (H) {H};
\path
(R) edge[sedge] (A) (A.east) edge[sedge] (B)
(R) edge[sedge] (A) (A.east) edge[sedge] (C)
(R) edge[sedge] (D) (D.east) edge[sedge] (E) (E.east) edge[sedge] (G)
(R) edge[sedge] (D) (D.east) edge[sedge] (E) (E.east) edge[sedge] (H)
(R) edge[cedge] (D) (D.east) edge[cedge] (E) (E.east) edge[cedge] (F);
\end{tikzpicture}
\end{document}