此 MWE 代码改编自这示例位于 www.texample.net,最初由 SE 用户编写成本加运费:
\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, shapes.geometric, calc, shadows}
\usepackage{forest}
\usepackage{adjustbox}
\colorlet{mygreen}{green!75!black}
\colorlet{col1in}{red!30}
\colorlet{col1out}{red!40}
\colorlet{col2in}{mygreen!40}
\colorlet{col2out}{mygreen!50}
\colorlet{col3in}{blue!30}
\colorlet{col3out}{blue!40}
\colorlet{col4in}{mygreen!20}
\colorlet{col4out}{mygreen!30}
\colorlet{col5in}{blue!10}
\colorlet{col5out}{blue!20}
\colorlet{col6in}{blue!20}
\colorlet{col6out}{blue!30}
\colorlet{col7out}{orange}
\colorlet{col7in}{orange!50}
\colorlet{col8out}{orange!40}
\colorlet{col8in}{orange!20}
\colorlet{linecol}{blue!60}
\begin{document}
\tikzset{
basic/.style = {draw, drop shadow, rectangle},
root/.style = {basic, rounded corners=2pt, thin, align=center,
fill=green!30},
level 2/.style = {basic, rounded corners=6pt, thin,align=center, fill=blue!10},
level 3/.style = {basic, thin, align=left, fill=green!10}
}
\pgfkeys{/forest,
rect/.append style = {rectangle, rounded corners = 2pt,
inner color = col6in, outer color = col6out},
ellip/.append style = {ellipse, inner color = col5in,
outer color = col5out},
orect/.append style = {rect, font = \sffamily\bfseries\LARGE,
text width = 325pt, text centered,
minimum height = 10pt, outer color = col7out,
inner color=col7in},
oellip/.append style = {ellip, inner color = col8in, outer color = col8out,
font = \sffamily\bfseries\large, text centered}}
\begin{figure}[h]
\begin{adjustbox}{max width=1.2\textwidth,center}
\begin{forest}
for tree={
font=\sffamily\bfseries,
line width=1pt,
draw=linecol,
ellip,
align=center,
child anchor=north,
parent anchor=south,
drop shadow,
l sep+=12.5pt,
edge path={
\noexpand\path[color=linecol, rounded corners=5pt,
>={Stealth[length=10pt]}, line width=1pt, ->, \forestoption{edge}]
(!u.parent anchor) -- +(0,-5pt) -|
(.child anchor)\forestoption{edge label};
},
where level={3}{tier=tier3}{},
where level={0}{l sep-=15pt}{},
where level={1}{
if n={1}{
edge path={
\noexpand\path[color=linecol, rounded corners=5pt,
>={Stealth[length=10pt]}, line width=1pt, ->,
\forestoption{edge}]
(!u.west) -| (.child anchor)\forestoption{edge label};
},
}{
edge path={
\noexpand\path[color=linecol, rounded corners=5pt,
>={Stealth[length=10pt]}, line width=1pt, ->,
\forestoption{edge}]
(!u.east) -| (.child anchor)\forestoption{edge label};
},
}
}{},
}
[\Huge Total drag, yshift = 3cm, inner color=col1in, outer color=col1out
[\huge Viscous drag, yshift = 1cm, inner color=col2in, outer color=col2out
[\Large Displacement separation, inner color=col4in, outer color=col4out
]
[\Large Boundary layer shear, inner color=col4in, outer color=col4out
]
]
[\huge Vortex drag, yshift = 1cm, inner color=col3in, outer color=col3out
[\Large Trailing vortices due to lift
]
]
[\huge Wave drag, yshift = 1cm, inner color=col3in, outer color=col3out
[\Large Waves due to lift
]
[\Large Waves due to volume
]
]
]
]
\end{forest}
\end{adjustbox}
\end{figure}
\end{document}
结果是:
请注意,来自标记为“总阻力”的上级父节点的线不是从规定的“南”锚点位置开始,如以下线所规定的那样:
for tree={
...
parent anchor=south,
...
我将非常感激任何对此提供的帮助。
答案1
您的代码按预期工作。edges
第一个节点和子节点之间定义为
where level={1}{
if n={1}{
edge path=...
}{
edge path=...
}
}{},
因此,对于所有儿童来说,新的已定义父级锚点。这west
适用于子级 1 以及east
除 1 之外的所有其他子级。
如果您想区分孩子 2 和 3,您可以引入另一个if
级别。
\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, shapes.geometric, calc, shadows}
\usepackage{forest}
\usepackage{adjustbox}
\colorlet{mygreen}{green!75!black}
\colorlet{col1in}{red!30}
\colorlet{col1out}{red!40}
\colorlet{col2in}{mygreen!40}
\colorlet{col2out}{mygreen!50}
\colorlet{col3in}{blue!30}
\colorlet{col3out}{blue!40}
\colorlet{col4in}{mygreen!20}
\colorlet{col4out}{mygreen!30}
\colorlet{col5in}{blue!10}
\colorlet{col5out}{blue!20}
\colorlet{col6in}{blue!20}
\colorlet{col6out}{blue!30}
\colorlet{col7out}{orange}
\colorlet{col7in}{orange!50}
\colorlet{col8out}{orange!40}
\colorlet{col8in}{orange!20}
\colorlet{linecol}{blue!60}
\begin{document}
\tikzset{
basic/.style = {draw, drop shadow, rectangle},
root/.style = {basic, rounded corners=2pt, thin, align=center,
fill=green!30},
level 2/.style = {basic, rounded corners=6pt, thin,align=center, fill=blue!10},
level 3/.style = {basic, thin, align=left, fill=green!10}
}
\pgfkeys{/forest,
rect/.append style = {rectangle, rounded corners = 2pt,
inner color = col6in, outer color = col6out},
ellip/.append style = {ellipse, inner color = col5in,
outer color = col5out},
orect/.append style = {rect, font = \sffamily\bfseries\LARGE,
text width = 325pt, text centered,
minimum height = 10pt, outer color = col7out,
inner color=col7in},
oellip/.append style = {ellip, inner color = col8in, outer color = col8out,
font = \sffamily\bfseries\large, text centered}}
\begin{figure}[h]
\begin{adjustbox}{max width=1.2\textwidth,center}
\begin{forest}
for tree={
font=\sffamily\bfseries,
line width=1pt,
draw=linecol,
ellip,
align=center,
child anchor=north,
parent anchor=south,
drop shadow,
l sep+=12.5pt,
edge path={
\noexpand\path[color=linecol, rounded corners=5pt,
>={Stealth[length=10pt]}, line width=1pt, ->, \forestoption{edge}]
(!u.parent anchor) -- +(0,-5pt) -|
(.child anchor)\forestoption{edge label};
},
if level={3}{tier=tier3}{},
if level={0}{l sep-=15pt}{},
if level={1}{
if n={1}{
edge path={
\noexpand\path[color=linecol, rounded corners=5pt,
>={Stealth[length=10pt]}, line width=1pt, ->,
\forestoption{edge}]
(!u.west) -| (.child anchor)\forestoption{edge label};
},
}{
if n={3}{
edge path={
\noexpand\path[color=linecol, rounded corners=5pt,
>={Stealth[length=10pt]}, line width=1pt, ->,
\forestoption{edge}]
(!u.east) -| (.child anchor)\forestoption{edge label};
},
}{}}
}{},
}
[\Huge Total drag, yshift = 3cm, inner color=col1in, outer color=col1out
[\huge Viscous drag, yshift = 1cm, inner color=col2in, outer color=col2out
[\Large Displacement separation, inner color=col4in, outer color=col4out
]
[\Large Boundary layer shear, inner color=col4in, outer color=col4out
]
]
[\huge Vortex drag, yshift = 1cm, inner color=col3in, outer color=col3out
[\Large Trailing vortices due to lift
]
]
[\huge Wave drag, yshift = 1cm, inner color=col3in, outer color=col3out
[\Large Waves due to lift
]
[\Large Waves due to volume
]
]
]
]
\end{forest}
\end{adjustbox}
\end{figure}
\end{document}
答案2
Ignasi 的回答很好。这只是一个补充。如果您还想摆脱线条中的扭结,您可以使用else...
最后的部分if...
将子元素与父元素对齐:
if level=1{
if n=1{
edge path={
\noexpand\path[\forestoption{edge}] (!u.west) -| (.child anchor)\forestoption{edge label};
},
}{
if n'=1{
edge path={
\noexpand\path[\forestoption{edge}] (!u.east) -| (.child anchor)\forestoption{edge label};
},
}{calign with current}
}
}{},
完整代码:
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{arrows.meta, shadows}
\usepackage{forest}
\colorlet{mygreen}{green!75!black}
\colorlet{col1in}{red!30}
\colorlet{col1out}{red!40}
\colorlet{col2in}{mygreen!40}
\colorlet{col2out}{mygreen!50}
\colorlet{col3in}{blue!30}
\colorlet{col3out}{blue!40}
\colorlet{col4in}{mygreen!20}
\colorlet{col4out}{mygreen!30}
\colorlet{col5in}{blue!10}
\colorlet{col5out}{blue!20}
\colorlet{linecol}{blue!60}
\begin{document}
\tikzset{
ellip/.append style = {ellipse, col in out = 5},
col in out/.style = {inner color = col#1in, outer color = col#1out},
font sizing/.style={font=\sffamily\bfseries#1},
Huge/.style={font sizing=\Huge},
huge/.style={font sizing=\huge},
Large/.style={font sizing=\Large},
}
\begin{forest}
for tree={
font=\sffamily\bfseries,
line width=1pt,
draw=linecol,
ellip,
align=center,
child anchor=north,
parent anchor=south,
drop shadow,
l sep+=10mm,
edge={color=linecol, rounded corners=5pt, >={Stealth[length=10pt]}, line width=1pt, ->},
edge path={
\noexpand\path[\forestoption{edge}] (!u.parent anchor) -- +(0,-5pt) -| (.child anchor)\forestoption{edge label};
},
if level=1{
huge,
if n=1{
col in out=2,
for children={col in out=4},
edge path={
\noexpand\path[\forestoption{edge}] (!u.west) -| (.child anchor)\forestoption{edge label};
},
}{
col in out=3,
if n'=1{
edge path={
\noexpand\path[\forestoption{edge}] (!u.east) -| (.child anchor)\forestoption{edge label};
},
}{calign with current}
}
}{},
if level=2{Large}{}
}
[Total drag, Huge, col in out=1
[Viscous drag
[Displacement separation
]
[Boundary layer shear
]
]
[Vortex drag
[Trailing vortices due to lift
]
]
[Wave drag
[Waves due to lift
]
[Waves due to volume
]
]
]
\end{forest}
\end{document}