2

enter image description hereQuestion: I have been trying to complete the tree diagram which I started (see the figure).

Please help.enter image description here

This is what I have

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}
    
    \begin{center}
        \begin{tikzpicture}[level 1/.style={sibling distance=5cm},level 2/.style={sibling distance=4.0cm}]
        \node {1}[edge from parent fork down]
        child { node {2}}
        child { node {3}
            child {node{4} }
            child {node{5}}
        }               
        ;
    \end{tikzpicture}
    \end{center}
    
\end{document}
1
  • Since the length of the nodes' content and the width of your text block are apparently crucial (judging from your comments on answers), your MWE should reflect this.
    – cfr
    Jan 16 at 0:32

3 Answers 3

6

For comparison, here is how you could do this using forest:

enter image description here

\documentclass{article}

\usepackage[edges]{forest}

\begin{document}

\begin{forest}
for tree={forked edge, edge=->, draw, circle, minimum size=8mm, l sep=5mm}
[1, s sep=5cm
    [2]
    [3, s sep=4cm
        [4, s sep=3cm
            [6, s sep=1cm
                [9][10][11]
            ]
            [7]
            [8, s sep=2cm
                [12][13]
            ]
        ]
        [5]
    ]
]
\end{forest}

\end{document}
7
  • Thank you but when I replace 4 by any large text , circle becomes very big. Any solution for this issue.
    – SandyM
    Jan 10 at 13:28
  • You can remove circle to get rectangular nodes that may fit better. You can also remove draw if you don't want the node borders drawn at all.
    – Sandy G
    Jan 10 at 13:32
  • Please look the image what I get after correcting your codes. Do the needful. It will be great help to me.
    – SandyM
    Jan 10 at 13:44
  • I don't understand what you want. What change are you trying to make from your image?
    – Sandy G
    Jan 10 at 13:59
  • My figure not properly fit in the page and second how to increase vertical distance
    – SandyM
    Jan 10 at 14:06
5

You just have to nest your childs:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}
  
  \begin{center}
    \begin{tikzpicture}[level 1/.style={sibling distance=5cm},
      level 2/.style={sibling distance=4.0cm},
      level 3/.style={sibling distance=2.0cm},
      level 4/.style={sibling distance=1.0cm}]
      \node {1}[edge from parent fork down]
      child { node {2}}
      child { node {3}
        child {node{4}
          child {node{6}
            child {node{9}}
            child {node{10}}
            child {node{11}}}
          child {node{7}}
          child {node{8}
            child {node{12}}
            child {node{13}}}}
        child {node{5}}
      }               
      ;
    \end{tikzpicture}
  \end{center}
  
\end{document}

enter image description here

If you do want encircled numbers, in the tikzpicture's options add

every node/.style={draw, circle},
1

This is based on Sandy G's answer. I have no idea whether you are using A4 or US letter. Likely the latter, but I assume the former as it is narrower.

If you use forest, something a bit like your tree fits. It isn't quite your tree because I lost patience typing contents when the words became unfamiliar and started making stuff up instead.

Note that the reason switching to forest helps is that the package produces more compact trees by default (fit=tight).

\begin{forest}
  % ateb: https://tex.stackexchange.com/a/707154/ addaswyd o ateb Sandy G: https://tex.stackexchange.com/a/706800/
  forked edges,
  for tree={
    edge+=->,
    draw,
    font=\sffamily,
  },
  where={>Ow+P{n children}{isodd(#1)}}{%
    delay={for n=2{calign with current edge}}
  }{},
  [Fluids
    [Ideal Fluids]
    [Real Fluids
      [Non-Newtonian Fluids
        [Time-Independant Fluids
          [Vouch-saving Vanilla][Soliciting Sourdough][Advocating Avocado]
        ]
        [Timeless Thyme]
        [Time-Dependant Fluids
          [Peppermint][Spearmint]
        ]
      ]
      [Newtonian Fluids]
    ]
  ]
\end{forest}

forest version

Complete code:

\documentclass[a4paper]{article}
\usepackage[showframe]{geometry}
% ateb: https://tex.stackexchange.com/a/707154/ addaswyd o ateb Sandy G: https://tex.stackexchange.com/a/706800/
\usepackage[edges]{forest}

\begin{document}

\noindent
\begin{forest}
  % ateb: https://tex.stackexchange.com/a/707154/ addaswyd o ateb Sandy G: https://tex.stackexchange.com/a/706800/
  forked edges,
  for tree={
    edge+=->,
    draw,
    font=\sffamily,
  },
  where={>Ow+P{n children}{isodd(#1)}}{%
    delay={for n=2{calign with current edge}}
  }{},
  [Fluids
    [Ideal Fluids]
    [Real Fluids
      [Non-Newtonian Fluids
        [Time-Independant Fluids
          [Vouch-saving Vanilla][Soliciting Sourdough][Advocating Avocado]
        ]
        [Timeless Thyme]
        [Time-Dependant Fluids
          [Peppermint][Spearmint]
        ]
      ]
      [Newtonian Fluids]
    ]
  ]
\end{forest}

\end{document}

If that's not sufficient, I suggest either using sidewaysfigure from the rotating package or allowing line-breaks in nodes.

\begin{forest}
  % ateb: https://tex.stackexchange.com/a/707154/ addaswyd o ateb Sandy G: https://tex.stackexchange.com/a/706800/
  forked edges,
  for tree={
    edge+=->,
    draw,
    font=\sffamily,
    align=center,
  },
  where={>Ow+P{n children}{isodd(#1)}}{%
    delay={for n=2{calign with current edge}}
  }{},
  [Fluids
    [Ideal Fluids]
    [Real Fluids
      [Non-Newtonian Fluids
        [Time-Independant\\Fluids
          [Vouch-saving\\Vanilla][Soliciting\\Sourdough][Advocating\\Avocado]
        ]
        [Timeless\\Thyme]
        [Time-Dependant\\Fluids
          [Peppermint][Spearmint]
        ]
      ]
      [Newtonian Fluids]
    ]
  ]
\end{forest}

with multiline nodes

Better yet, avoid duplicating information. The top of the tree says these are all fluids, so why keep repeating yourself? Removing excess Fluids may make your diagram clearer, even if it doesn't reduce the width much.

Or, if you want to be more adventurous, you can even turn recalcitrant nodes, though I would only suggest using this method in appropriate cases (and I doubt this is among them).

\begin{forest}
  % ateb: https://tex.stackexchange.com/a/707154/ addaswyd o ateb Sandy G: https://tex.stackexchange.com/a/706800/
  forked edges,
  for tree={
    edge+=->,
    draw,
    font=\sffamily,
  },
  where={>Ow+P{n children}{isodd(#1)}}{%
    delay={for n=2{calign with current edge}}
  }{},
  before typesetting nodes={%
    tempcounta/.max={>O{level}}{tree},
    where={> OR= {level}{tempcounta}}{rotate=-90,anchor=parent}{},
  },
  [Fluids
    [Ideal]
    [Real
      [Non-Newtonian
        [Time-Independent
          [Vouch-saving Vanilla][Soliciting Sourdough][Advocating Avocado]
        ]
        [Timeless Thyme]
        [Time-Dependant
          [Peppermint][Spearmint]
        ]
      ]
      [Newtonian]
    ]
  ]
\end{forest}

turned final level and fewer fluids

1
  • Thank you so much for your innovative answer.
    – SandyM
    Feb 1 at 9:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .