我正在尝试使用 tikz 制作一个显示一些信息的图形。起初看起来还不错,但过了一会儿,一切都移动了,所以没有任何东西对齐。我想可能有更好的方法来做到这一点,这样东西就不会移动,但我找不到一种方法来制作与盒子相关的列表。关于如何以更好的方式做到这一点的建议将不胜感激!
\documentclass[tikz,border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit,arrows}
\begin{document}
\begin{tikzpicture}
\node[draw, fit={(0,0)(7,1.2)}] (Meta) at (3,0) {Metaheuristic layer};
\node[draw,align=left] (Destruction) at (0,-2) {Destruction\\heuristics};
\node[draw,align=left] (Construction) at (3,-2) {Construction\\heuristics};
\node[draw,align=left] (Local) at (6,-2) {Local search\\heurstics};
\node (toDestruction) at (0,-.6){};
\node (toLocal) at (6,-.6){};
\node (topDestruction) at (-.5,-2.315){};
\node (bottomDestruction) at (-.5,-4.68){};
\node (topConstruction) at (2.5,-2.315){};
\node (bottomConstruction) at (2.5,-5.22){};
\node (topLocal) at (5.5,-2.315){};
\node (bottomLocal) at (5.5,-4.32){};
\path[->, >=triangle 45, auto, semithick]
(toDestruction) edge (Destruction)
(Meta) edge (Construction)
(toLocal) edge (Local);
\path
(topDestruction) edge (bottomDestruction)
(topConstruction) edge (bottomConstruction)
(topLocal) edge (bottomLocal);
\node[label=right:Remove K] (RemoveK) at (-.3,-3){};
\node[align=left] (RemoveKSR) at (1.0,-3.8){Remove K\\same route};
\node[align=left] (MakeK) at (1.15,-4.78){Make K\\feasible gaps};
\node[label=right:Fill the gap] (FTG) at (2.7,-3){};
\node[label=right:K-regret] (Kreg) at (2.7,-3.6){};
\node[align=left] (Solomon) at (3.6,-4.3){Solomon\\insertion};
\node[align=left] (GNN) at (3.83,-5.5){Generalized\\nearest\\neighbour};
\node[label=right:2-exchange] (2ex) at (5.7,-3){};
\node[label=right:2-interchange] (2in) at (5.7,-3.6){};
\node[label=right:Resequence] (res) at (5.7,-4.2){};
\path[->]
(-.5,-3) edge (-.1,-3)
(-.5,-3.6) edge (-.1,-3.6)
(-.5,-4.55) edge (-.1,-4.55)
(2.5,-3) edge (2.9,-3)
(2.5,-3.6) edge (2.9,-3.6)
(2.5,-4.1) edge (2.9,-4.1)
(2.5,-5.1) edge (2.9,-5.1)
(5.5,-3) edge (5.9,-3)
(5.5,-3.6) edge (5.9,-3.6)
(5.5,-4.2) edge (5.9,-4.2);
\end{tikzpicture}
\end{document}
当我将字体大小更改为 12pt 时,发生以下情况:
答案1
这里介绍一种使用的方法tikz trees
。使用了许多样式定义。
代码
\documentclass[10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[edge from parent fork down,
edge from parent/.style={draw,-latex},
every node/.style={thin, minimum height=2.5em},
supervisor/.style={level distance=1cm,text centered, text width=8cm},
teammate/.style={align=left, text width=2cm,level distance=1cm},
subordinate/.style={grow=down, xshift=1cm, align=left, text width=3cm, edge from parent path={(\tikzparentnode.205) |- (\tikzchildnode.west)}},
level1/.style ={level distance=1cm},
level2/.style ={level distance=2cm},
level3/.style ={level distance=3cm},
level4/.style ={level distance=4.5cm},
]
\node[draw,supervisor]{Metaheuristic layer}
child[sibling distance=3cm,draw]{node [draw,teammate] {Destruction\\heuristics}
child[subordinate,level1] {node {Remove K}}
child[subordinate,level2] {node {Remove K\\same route}}
child[subordinate,level3] {node {Make K\\feasible gaps}}}
%
child[sibling distance=3cm]{node [draw,teammate] {Construction\\heuristics}
child[subordinate,level1] {node {Fill the gap}}
child[subordinate,level2] {node {K-regret}}
child[subordinate,level3] {node {Solomon\\insertion}}
child[subordinate,level4] {node {Generalized\\nearest\\neighbour}}}
%
child[sibling distance=3cm]{node [draw,teammate] {Local search\\heurstics}
child[subordinate,level1] {node {2-exchange}}
child[subordinate,level2] {node {2-interchange}}
child[subordinate,level3] {node {Resequence}}};
\end{tikzpicture}
\end{document}
答案2
您始终可以使用forest
...(如果您不喜欢打字,这尤其有用!)。我还更新了箭头语法以使用arrows.meta
现在推荐的库。
几乎所有的工作都是在使用的树的设置中完成的,where level=n{}{}
其中n
是树的相关级别,从 0 处的根节点开始。绘制边的方式通过设置而变化,并且使用和键edge path
设置起点和终点。是级别之间的最小距离和同级节点之间的最小距离。parent anchor
child anchor
l sep
s sep
\documentclass[tikz,border=5,12pt,mult,varwidth]{standalone}
\usetikzlibrary{fit,arrows.meta}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
parent anchor=south,
child anchor=north,
where level=0{
align=center,
s sep+=10pt,
l sep+=10pt,
}{
align=left,
where level=1{
edge path={
\noexpand\path [semithick, -{Triangle[]}, \forestoption{edge}] (!u.parent anchor -| .child anchor) -- (.child anchor)\forestoption{edge label};
},
draw,
}{
l sep=0pt,
child anchor=west,
anchor=west,
parent anchor=west,
edge path={
\noexpand\path [->, \forestoption{edge}] (!u.parent anchor -| .child anchor) +(-15pt,0) |- (.child anchor)\forestoption{edge label};
},
},
},
}
[Metaheuristic layer, name=ml
[Destruction\\heuristics, name=dh
[Remove K
[Remove K\\same route
[Make K\\feasible gaps
]
]
]
]
[Construction\\heuristics
[Fill the gap
[K-regret
[Solomon\\insertion
[Generalized\\nearest\\neighbour
]
]
]
]
]
[Local search\\heuristics, name=lsh
[2-exchange
[2-interchange
[Resequence
]
]
]
]
]
\node [draw, inner ysep=0pt, inner xsep=20pt, fit=(ml) (dh |- ml) (lsh |- ml)] {};
\end{forest}
\end{document}
或者,如果您想要更精美的版本,这会调整线条的粗细并使用 TiKZ 样式作为方框。这在加载库方面有一些额外的开销(但如果您不想要阴影,则可以避免这些添加)。
\documentclass[tikz,border=5,12pt,mult,varwidth]{standalone}
\usetikzlibrary{fit,arrows.meta,shadows,backgrounds}
\usepackage{forest}
\begin{document}
\tikzset{
shadowed box/.style={
drop shadow,
draw,
ultra thick,
fill=white,
}
}
\begin{forest}
for tree={
parent anchor=south,
child anchor=north,
where level=0{
align=center,
s sep+=10pt,
l sep+=10pt,
}{
align=left,
where level=1{
edge path={
\noexpand\path [thick, -{Triangle[]}, \forestoption{edge}] (!u.parent anchor -| .child anchor) -- (.child anchor)\forestoption{edge label};
},
shadowed box,
}{
l sep=0pt,
child anchor=west,
anchor=west,
parent anchor=west,
edge path={
\noexpand\path [->, thick, \forestoption{edge}] (!u.parent anchor -| .child anchor) +(-15pt,0) |- (.child anchor)\forestoption{edge label};
},
},
},
}
[Metaheuristic layer, name=ml
[Destruction\\heuristics, name=dh
[Remove K
[Remove K\\same route
[Make K\\feasible gaps
]
]
]
]
[Construction\\heuristics
[Fill the gap
[K-regret
[Solomon\\insertion
[Generalized\\nearest\\neighbour
]
]
]
]
]
[Local search\\heuristics, name=lsh
[2-exchange
[2-interchange
[Resequence
]
]
]
]
]
\begin{scope}[on background layer]
\node [shadowed box, inner ysep=0pt, inner xsep=20pt, fit=(ml) (dh |- ml) (lsh |- ml)] {};
\end{scope}
\end{forest}
\end{document}