我有一个短旋转树,其中一个节点有两个子节点,这两个子节点都有同一个子节点。但是,第三级的共享子节点未正确对齐。我一直在尝试将 above=Xem 用于第三级,但总是有点不对劲。
有没有办法可以自动将其与第一级正确对齐?
\begin{tikzpicture}[level distance=20em,rotate=90,
edge from parent/.style={->,thick,draw=black,line width = 0.5em,shorten >=8pt, shorten <=8pt},
level 1/.style={sibling distance=15em},
level 2/.style={sibling distance=15em},
every node/.style = {shape=rectangle, rounded corners,thick,scale=2, align=center, line width=0.15em,text centered}]]
\node[text width=12em] {\textbf{1st level}}
child { node {\textbf{2nd level 1}}
child { node[name=3] {\textbf{3rd level}} } }
child { node[name=2] {\textbf{2nd level 2}}
};
\draw[->,thick,draw=black,line width = 0.5em] (2) edge (3);
\end{tikzpicture}
答案1
如果你希望 1 级和 3 级之间水平对齐,你可以编写代码
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc
,positioning}
\begin{document}
\begin{tikzpicture}[level distance=20em,rotate=90,
edge from parent/.style={->,thick,draw=black,line width = 0.5em,shorten >=8pt, shorten <=8pt},
level 1/.style={sibling distance=15em},
level 2/.style={sibling distance=15em},
every node/.style = {shape=rectangle, rounded corners,thick,scale=2, align=center, line width=0.15em,text centered}]]
\node[text width=12em, name=1] {\textbf{1st level}}
child { node [name=2-1]{\textbf{2nd level 1}}
% child { node[name=3] {\textbf{3rd level}} }
}
child { node[name=2-2] {\textbf{2nd level 2}}
};
% \draw[->,thick,draw=black,line width = 0.5em] (2-2) edge (3);
\node [right=20em of $(2-1)!.5!(2-2)$, anchor=center] (3) {\textbf{3rd level}};
% \node [right=20em of 1|-2-1, anchor=center] (3) {\textbf{3rd level}}; % <-- or
\draw[->,thick,draw=black,line width = 0.5em] (2-1) edge (3);
\draw[->,thick,draw=black,line width = 0.5em] (2-2) edge (3);
\end{tikzpicture}
\end{document}
答案2
编辑:
首先,让我清理一下您的代码,以便能够努力找到用纯 绘制的树的解决方案tikz
。在删除所有混乱、重复的规范和样式定义中的矛盾之后,它是:
\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
grow = east, % <--- tree is not rotate but grow to east!
arr/.style = {draw, ->,line width=1mm,
shorten >=8pt, shorten <=8pt},
edge from parent/.style = {arr},
every node/.style = {draw, rounded corners, thick, font=\bfseries, anchor=east},
level distance = 20em,
sibling distance = 15em,
]
\node {1st level}
child { node {2nd level 1}
child { node[name=3] {3rd level} } }
child { node[name=2] {2nd level 2}
};
\draw[arr] (2) edge (3);
\end{tikzpicture}
\end{document}
然而,它不能解决你的问题,但在使用树搜索解决方案时要明确起点tikz
。其他答案中已经给出了这样的解决方案,因此我建议使用 forest
包的解决方案:
\documentclass[border=3.141592]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{forest}
for tree = {
% nodes
draw, rounded corners,
minimum height=3ex,
minimum width=6em,
font=\bfseries,
% tree
grow' = 0,
parent anchor=east,
child anchor=west,
/tikz/arr/.style = {ultra thick, -{Straight Barb[angle=60:2pt 3]}, % <---
shorten >=3pt, shorten <=3pt}, % <---
edge={arr},
s sep=12mm,
l sep=18mm,
}
[1st level
[2nd level 2, name=n1]
[2nd level 1
[3rd level, name=n2]
[,phantom] % <---
]
]
\draw[arr] (n1.east) -- (n2.west);
\end{forest}
\end{document}