如何添加箭头连接节点 Tikz?

如何添加箭头连接节点 Tikz?

我有如下图所示的块节点。我想用箭头将它们连接起来,字体加粗。你能帮我看看我的代码并更新它们吗?非常感谢

这是我目前的结果

在此处输入图片描述

这是我的预期结果

在此处输入图片描述

还有一件事,图表没有显示页面的中心。可以调整它的位置吗?

\documentclass[preprint,12pt, sort&compress]{elsarticle}
\usepackage{tikz}
\usetikzlibrary{trees}
\usepackage{varwidth}
\usetikzlibrary{arrows,shapes,positioning,shadows,trees,calc}
\begin{document}

\begin{figure}[h]
\centering
\begin{tikzpicture}[
every node/.style={draw, rectangle},
edge from parent path={
(\tikzparentnode) |-   % Start from parent
($(\tikzparentnode)!0.5!(\tikzchildnode)$) -| % make an ortho line to mid point
(\tikzchildnode)},
second/.style   ={level distance=18ex},
third/.style   ={level distance=18ex},
fourth/.style   ={level distance=10ex},
]                            % make another ortho to the target

  \node (A){This is first block}
  [sibling distance=9cm]
  child {node (B) {\begin{varwidth}{6cm}The second block 1\end{varwidth}}
  [sibling distance=6cm]
    child [third]{node {\begin{varwidth}{3cm}Third level block left 2\end{varwidth}}
    [sibling distance=3cm]
         child [fourth]{node  {\begin{varwidth}{3cm}Last block 1\end{varwidth}}}         
         child [fourth]{node {\begin{varwidth}{3cm}Last block 2\end{varwidth}}}
    }
    child [second]{node {\begin{varwidth}{3cm}Third level block left 2\end{varwidth}}
    [sibling distance=3cm]
         child [fourth]{node  {\begin{varwidth}{3cm}Last block 3\end{varwidth}}}         
         child [fourth]{node {\begin{varwidth}{3cm}Last block 4\end{varwidth}}}
    }
  }
  child {node {The second block 2}
  [sibling distance=3.5cm]
    child [third]{node {\begin{varwidth}{3cm}Third level block right 1\end{varwidth}}}
    child [third]{node {\begin{varwidth}{3cm}Third level block right 2\end{varwidth}}}
  };


\end{tikzpicture}
\end{figure}
\end{document}

更新:关于 smike 的回答。我得到了如下结果。但它有两个问题 1. 图形没有位于页面中央

  1. 块的宽度线看起来更粗。我希望所有块都有相同的宽度

在此处输入图片描述

答案1

我尝试获得整体图形的正确对齐以及相关的箭头样式:

\documentclass[preprint,12pt,sort&compress]{elsarticle}
\usepackage{tikz}
\usepackage{geometry}
\usepackage{varwidth}
\usetikzlibrary{arrows,shapes,positioning,shadows,trees,calc,backgrounds}

\newcommand{\vtext}[2]{
\begin{varwidth}{#1}%
\centering{#2}
\end{varwidth}}

\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}[framed,
every node/.style={draw, rectangle},
edge from parent path={
(\tikzparentnode) |- ($(\tikzparentnode)!0.5!(\tikzchildnode)$) -| (\tikzchildnode)},
second/.style={level distance=18ex},%
third/.style={level distance=18ex},%
fourth/.style={level distance=10ex},%
level 1/.style={sibling distance=7cm},
level 2/.style={sibling distance=5cm},
level 3/.style={sibling distance=2.5cm},
newlevel/.style={level 2/.style={sibling distance=3cm}},
edge from parent/.style={draw,line width=1pt,->},
every node/.style={thin,draw}
]
\node (A){This is first block}[sibling distance=8cm]
child {node (B) {\vtext{6cm}{The second block 1}}
    child[third] {node {\vtext{2cm}{Third level block left 2}}
        child[fourth] {node {\vtext{2cm}{Last block 1}}}
        child[fourth] {node {\vtext{2cm}{Last block 2}}}
}
child[second] {node {\vtext{3cm}{Third level block right 2}}
    child[fourth] {node {\vtext{2cm}{Last block 3}}}
    child[fourth] {node {\vtext{2cm}{Last block 4}}}}
}
child[newlevel] {node {The second block 2}
    child[third] {node {\vtext{3cm}{Third level block right 1}}}
    child[third] {node {\vtext{3cm}{Third level block right 2}}}
};
\end{tikzpicture}
\end{figure}
\end{document}

输出为:

树形图

framed您可以通过删除中的选项来删除图表周围的框架tikzpicture。我不得不调整 中的间距varwidth环境中的间距以确保所有子项都适合正确的页面居中。您包含的一些 tikzlibraries 在这里不是必需的,但我假设您在文档的其他地方使用它。

编辑:这张图片可以更好地展示居中效果。页面颜色为青色,但 除外tikzpicture

树形图彩色

相关内容