0

I have the block nodes as below figure. I want to connect them by arrow, with bold font. Could you help me to look at my code and update them? Thank you so much

This is my current result

enter image description here

This is my expected result

enter image description here

One more thing, the graph does not show the center of page. Is it possible to adjust its position?

\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}

UPDATE: Regarding the ans. of smike. I Obtained the result as below. But it has two problem 1. The figure did not center of the page

  1. The width line of block look more thick. I want all blocks have same thick

enter image description here

Jame
  • 689
  • 1
    Since you are using child nodes, you can edit the overall style for it:

    \tikzset{edge from parent/.style={draw,->}}.

    I am not sure why the centering is an issue.

    – smike Mar 05 '16 at 15:23
  • Thanks, it is correct. As I shown in figure, the whole tree looks move to right side, I want to center it in the page. – Jame Mar 05 '16 at 15:28
  • Could it be that you are using too wide sibling distances? – smike Mar 05 '16 at 15:30
  • And if I want to adjust the connected line between them more thick, I will use \tikzset{edge from parent/.style={draw,->,thick}}, Is it right? I do it but the line does not show the thick line – Jame Mar 05 '16 at 15:30
  • ultra thick might show better. Or using a custom line width can also be done: line width=1.5pt. – smike Mar 05 '16 at 15:33
  • I uses the sibling distances to control the distance between blocks. Due to having many block, I need use them more large to ignore the overlapping between blocks – Jame Mar 05 '16 at 15:34
  • The line is thick now. But the bocks at third and last level are also thick, It makes wrong. I only want the line connection is thick – Jame Mar 05 '16 at 15:37
  • For those specific blocks, you can pass the command to their nodes: line width=x, where x is the width you want. You can use scope here for that. – smike Mar 05 '16 at 15:38
  • 1
    Regarding the centring, when I use the geometry package, your graph is automatically centred. That might help.

    To be sure of that, pass the option framed to tikzpicture (remember to include backgrounds tikzlibrary). The frame around the graph will give you an idea of the placement of the figure with respect to the page.

    – smike Mar 05 '16 at 15:42
  • I had a few questions: Should the children of a node be at the same level? Are you including this graph in a document eventually, or would you be interested in having a standalone version? – smike Mar 05 '16 at 15:51
  • Yes. It need the same level. I am using the elsarticle template. I included the graph in the document. – Jame Mar 05 '16 at 15:54

1 Answers1

2

My attempt at getting the right alignment of the overall graph, along with relevant arrow styles:

\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}

The output is:

Tree Graph

You can remove the frame around your graph by deleting the framed option from tikzpicture. I had to adjust the spacing in varwidth environment to make sure all the children fit with proper page centering. Some of the tikzlibraries you included were not necessary here, but I am assuming you use it elsewhere in the document.

EDIT: This picture gives a better idea of centering. The page was coloured cyan, except for the tikzpicture.

Tree Graph Coloured

smike
  • 151