3

I have created the following decision tree with the Forest package. It looks like this: enter image description here

There are problems associated with the lay-out and positioning of the nodes.

Problem A&B - See green line. I want the nodes (blocks) at the same height. So preferably, the bottom of the blocks should be on the green line. (Vertical alignment?)

Problem C - See blue circle. I think this problem is related to problem A&B since these are not at the same height. However, I want the arrows to be horizontal, not under a slight angle.

Problem D - See red block. I want the initial block (black) at the new position (red), i.e. I want the block to be moved slightly to the right such that it does not look too cramped.

The MWE:

\documentclass{report}
\usepackage[edges]{forest}
\usepackage{pdflscape}
\usepackage{tikz-qtree}
\usepackage{pgfkeys}
\usepackage{graphicx}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
    decorations.pathreplacing,decorations.pathmorphing,shapes,%
    matrix,quotes,shapes.symbols,plotmarks}
\begin{document}
\begin{landscape}
\tikzset{
  block/.style={rectangle, draw, fill=white!20, text width=2.75cm, text centered, rounded corners, minimum height=1.4cm},
  line/.style={draw, very thick, color=black!80, -Stealth},
}
\begin{figure}[h]
\centering
{\small
\begin{forest}
  arrow to/.style n args=2{%
    delay={%
      tikz+={%
        \draw [every edge, line] () -- (!#1) node [above, midway] {#2};
      },
    },
    !u.s sep+=30pt,
  },
  before typesetting nodes={%
    where n=1{%
      edge label/.wrap value={%
        node [left,pos=.75, anchor=mid east] {#1}
      },
    }{%
      edge label/.wrap value={%
        node [right,pos=.75, anchor=mid west] {#1}
      },
    },
  },
  for tree={%
    parent anchor=children,
    child anchor=parent,
    block,
    edge={line},
    l sep+=10pt,
  },
    forked edges
[Should we use it or not?
  [Type of wave
    [XXXXXXXX important?, edge label= HF waves
        [Not recommended, edge label=Yes
       ]
        [{XXXXXXXX characteristics}, edge label=No, arrow to={s}{Swell}
        [Use with XXXXXXXX care, edge label = Wind
        ]
        ]
    ]   
    [Which XXXXXXXX parameter?, edge label=LF waves
      [XXXXXXXXXX of XXXXX, edge label = Wave height
      [Estimation of XXXXXXXX height, edge label = Swell
      ]
      [Potential, edge label = Wind
       ] 
      ]
        [$H_{m0} \leq 100m$, edge label= Wave period
        [Potential, edge label = Yes
        ]
        [{Obliquity w.r.t. 123456 XXXXXXXX}, edge label = No, arrow to = {s}{No}
        [Not recommended, edge label = Yes
        ]
        ]
        ]
        [Yes, edge label = Resonant modes
        ]
  ]
  ]
  ]
\end{forest} 
}
\caption{Decision tree } \label{Decisiontree}
\end{figure}
\end{landscape}

\end{document}

How to proceed further from here?

This code is orignated from here and amended.

André
  • 417
  • Actually, the code is from my answer - not Zarko's. Zarko's doesn't use Forest. – cfr Aug 03 '16 at 22:28

1 Answers1

6

In order to answer this, I edited your example to remove extraneous material and to add material without which compilation failed with errors. Please ensure that examples posted compile unless the question concerns a particular error, in which case the example should produce the error in question.


Problems A, B and C can be solved by telling Forest to align all the nodes in each level to the same tier. Adding the following to for tree achieves this:

    tier/.wrap pgfmath arg={tier #1}{level()},

If problem D is just a one-time adjustment for a single node, the simplest way is probably to increase s for the node, but this needs to be delayed until the nodes have been packed. Adding, for example,

before computing xy={s'+=30pt}

to the relevant node shifts it 30pt to the right.

Here's a complete example:

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta}
\begin{document}
\tikzset{
  block/.style={draw, fill=white!20, text width=2.75cm, text centered, rounded corners, minimum height=1.4cm},
  line/.style={draw, very thick, color=black!80, -Stealth},
}

\begin{forest}
  arrow to/.style n args=2{%
    delay={%
      tikz+={%
        \draw [every edge, line] () -- (!#1) node [above, midway] {#2};
      },
    },
    !u.s sep+=30pt,
  },
  before typesetting nodes={%
    where n=1{%
      edge label/.wrap value={%
        node [left,pos=.75, anchor=mid east] {#1}
      },
    }{%
      edge label/.wrap value={%
        node [right,pos=.75, anchor=mid west] {#1}
      },
    },
  },
  for tree={%
    parent anchor=children,
    child anchor=parent,
    anchor=south,
    block,
    edge={line},
    l sep+=10pt,
    tier/.wrap pgfmath arg={tier #1}{level()},
  },
  forked edges,
  [Should we use it or not?
    [Type of wave
      [XXXXXXXX important?, edge label= HF waves
        [Not recommended, edge label=Yes
        ]
        [{XXXXXXXX characteristics}, edge label=No, arrow to={s}{Swell}
          [Use with XXXXXXXX care, edge label = Wind
          ]
        ]
      ]
      [Which XXXXXXXX parameter?, edge label=LF waves
        [XXXXXXXXXX of XXXXX, edge label = Wave height
          [Estimation of XXXXXXXX height, edge label = Swell
          ]
          [Potential, edge label = Wind
          ]
        ]
        [$H_{m0} \leq 100m$, edge label= Wave period
          [Potential, edge label = Yes
          ]
          [{Obliquity w.r.t. 123456 XXXXXXXX}, edge label = No, arrow to = {s}{No}
            [Not recommended, edge label = Yes
            ]
          ]
        ]
        [Yes, edge label = Resonant modes, before computing xy={s'+=30pt}
        ]
      ]
    ]
  ]
\end{forest}
\end{document}

which produces

adjusted tree

cfr
  • 198,882
  • Elegant solution! Could you maybe elaborate little bit more in detail what is meant with the second part of tier: arg={tier #1}{level()}. – André Aug 04 '16 at 12:30
  • @André In case you still visit, the value of level() is passed as the argument #1, so every node on level 1 ends up on tier 1 and so on. – cfr Aug 11 '23 at 00:30