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

  • 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 5 '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 5 '16 at 15:28
  • Could it be that you are using too wide sibling distances? – smike Mar 5 '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 5 '16 at 15:30
  • 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 5 '16 at 15:42
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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.