我正在尝试将两个 TikZ 节点彼此对齐(第二个节点位于第一个节点的右侧)。我已经实现了这一点,但是第二个节点浮动在第一个节点框内,我想避免这种情况。
\usepackage{tikz} % drawing
\usetikzlibrary{trees} % drawing nodes hierarchically
\usetikzlibrary{positioning} % helps position elements
\begin{tikzpicture}[
every node/.style = {rectangle, draw = black, thick, anchor = west},
company/.style = {fill = blue!40},
department/.style = {fill = blue!30},
person/.style = {fill = blue!20},
grow via three points = {one child at (0.5,-0.7) and two children at (0.5,-0.7) and (0.5,-1.4)},
edge from parent path = {(\tikzparentnode.south) |- (\tikzchildnode.west)}
]
\node [company] (c1) {Company 1}
child {node[department] (d1) {...}}
child {node[department] (d2) {Department}
child {node[person] (p1) {Person 1}}
child {node[person] (p2) {Person 2}}
child {node[person] (p3) {Person 3}}
};
\node [right=of c1] [company] {Company 2}
child {node[department] (d3) {Derpartment very very long department name}
child {node[person] (p4) {Person 4}}
child {node[person] (p5) {Person 5}}
};
\end{tikzpicture}
我怎样才能让第二个节点尊重第一个节点框?
答案1
这是你想要的?
\documentclass[12pt,a4paper]{article}
\usepackage{tikz} % drawing
\usetikzlibrary{trees} % drawing nodes hierarchically
\usetikzlibrary{positioning} % helps position elements
\begin{document}
\begin{tikzpicture}[
every node/.style = {rectangle, draw = black, thick, anchor = west},
company/.style = {fill = blue!40},
department/.style = {fill = blue!30},
person/.style = {fill = blue!20},
grow via three points = {one child at (0.5,-1.2) and two children at (0.5,-1.0) and (0.5,-1.9)},% <<<<<<<<<<
edge from parent path = {(\tikzparentnode.south) |- (\tikzchildnode.west)}
]
\node [company] (c1) {Company 1}
child {node[department] (d1) {...}}
child {node[department] (d2) {Department}
child {node[person] (p1) {Person 1}}
child {node[person] (p2) {Person 2}}
child {node[person] (p3) {Person 3}}
};
\node [company] (c2) at ([xshift=5cm]c1){Company 2}% changed <<<<<<<<<<
child {node[department, text width=5.5cm,text ragged] (d3) {Derpartment very very long department name}
child {node[person] (p4) {Person 4}}
child {node[person] (p5) {Person 5}}
};
\end{tikzpicture}
\end{document}
答案2
这使用具有命名本地边界框的范围。
\documentclass{standalone}
\usepackage{tikz} % drawing
\usetikzlibrary{trees} % drawing nodes hierarchically
\usetikzlibrary{positioning} % helps position elements
\begin{document}
\begin{tikzpicture}[
every node/.style = {rectangle, draw = black, thick, anchor = west},
company/.style = {fill = blue!40},
department/.style = {fill = blue!30},
person/.style = {fill = blue!20},
grow via three points = {one child at (0.5,-1.2) and two children at (0.5,-1.0) and (0.5,-1.7)},
edge from parent path = {(\tikzparentnode.south) |- (\tikzchildnode.west)}
]
\begin{scope}[local bounding box=box]
\node [company] (c1) {Company 1}
child {node[department] (d1) {...}}
child {node[department] (d2) {Department}
child {node[person] (p1) {Person 1}}
child {node[person] (p2) {Person 2}}
child {node[person] (p3) {Person 3}}
};
\end{scope}
\node (c2) [right] at (c1 -| box.east) [company] {Company 2}
child {node[department,align=left] (d3) {Derpartment very very long\\ department name}
child {node[person] (p4) {Person 4}}
child {node[person] (p5) {Person 5}}
};
\end{tikzpicture}
\end{document}