
forest
나는 다음과 같은 다이어그램에서 노드에 레이블을 지정하는 방법을 알아내려고 했습니다.이 질문자동으로. 따라서 아이디어는 다음과 같습니다.
- 레벨 0: 노드의 콘텐츠 앞에는 가 붙어야 합니다.
O.
즉, 지정된 콘텐츠가 이면P0
노드는 콘텐츠로 조판되어야 합니다O. P0
. - 레벨 1: 콘텐츠 앞에는
O.n
wheren
is the number of the child의 접두사가 붙어야 합니다. 예를 들어 첫 번째 레벨의 노드가P1
and 로 지정된 경우에는 andP3
로 조판해야 합니다 .O.1 P1
O.2 P3
- 레벨 2: 콘텐츠에는 부모의 자식 번호는
O.n.m
어디에 있고 노드의 자식 번호는 접두사가 붙어야 합니다. 예를 들어 자식이 있으면 후자가 로 나타나야 하고 자식이 있으면 후자는 로 조판되어야 합니다 .n
m
P1
P2
O.1.1 P2
P3
P4
O.2.1 P4
0레벨은 문제 없습니다. 을 사용하여 그렇게 할 수 있습니다 content={O. #1}
. 또한 관련 레이블을 구성하여 레벨 1과 2의 노드에 포함시킬 수도 있습니다. 그러나 이 레이블을 각 노드의 콘텐츠에 정확히 한 번만 추가하는 방법은 알 수 없습니다. 무엇을 하든 접두어가 없거나, 왜곡되거나 비어 있는 접두어가 있거나, 올바른 접두어의 복사본이 여러 개 있게 됩니다.
내 생각에 이것은 내가 가진 가장 가까운 것입니다.
\documentclass[tikz, border=5pt]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\begin{document}
\forestset{
tree node/.style = {align=center, inner sep=2pt, rounded corners = 2pt, text centered, font=\sffamily, rectangle, draw=black},
}
\begin{forest}
for tree={
parent anchor=south,
child anchor=north,
tree node,
l sep+=5pt,
edge path={
\noexpand\path [-{Stealth[]}, \forestoption{edge}, thick]
(!u.parent anchor) -- +(0,-5pt) -| (.child anchor)\forestoption{edge label};
},
},
set node label/.style={
content/.wrap 2 pgfmath args={O.##1 ##2}{n()}{content()},
},
set node super label/.style={
content/.wrap 3 pgfmath args={O.##1.##2 ##3}{n("!u")}{n()}{content()},
},
before typesetting nodes={
for tree={
where level=0{
content={O. #1},
}{
where level=1{
set node label,
}{
where level=2{
set node super label,
}{
},
},
},
},
}
[P0
[P1
[P2
]
]
[P3
[P4
]
]
]
;
\end{forest}
\end{document}
이렇게 하면 접두사가 올바르게 구성되지만 노드에 너무 자주 추가됩니다!
또한 원본 콘텐츠를 매크로에 저장한 다음 이를 content
. 하지만 그럴 경우 원본 내용이 모두 손실될 뿐입니다.
답변1
모든 where
s를 s로 바꿉니다 if
.
where
로 정의되므로 where/.style n args={3}{for tree={if={#1}{#2}{#3}}}
암시적으로 루프를 포함합니다 for tree
.