TransWikia.com

Drawing a more connected hierarchical diagram

TeX - LaTeX Asked by Hagbard on February 22, 2021

I’m attempting to draw a hierarchical diagram. I already found this example and a corresponding question. This helps a lot but unfortunately I need a slightly more sophisticated diagram and don’t know how to achieve my goals.

My code looks as follows:

% Hierarchical diagram
% Author: cfr
documentclass[tikz,border=10pt]{standalone}
usepackage{forest}
usetikzlibrary{arrows.meta, shapes.geometric, calc, shadows}

colorlet{mygreen}{green!75!black}
colorlet{col1in}{red!30}
colorlet{col1out}{red!40}
colorlet{col2in}{mygreen!40}
colorlet{col2out}{mygreen!50}
colorlet{col3in}{blue!30}
colorlet{col3out}{blue!40}
colorlet{col4in}{mygreen!20}
colorlet{col4out}{mygreen!30}
colorlet{col5in}{blue!10}
colorlet{col5out}{blue!20}
colorlet{col6in}{blue!20}
colorlet{col6out}{blue!30}
colorlet{col7out}{orange}
colorlet{col7in}{orange!50}
colorlet{col8out}{orange!40}
colorlet{col8in}{orange!20}
colorlet{linecol}{blue!60}

begin{document}
pgfkeys{/forest,
  rect/.append style   = {rectangle, rounded corners = 2pt,
                         inner color = col6in, outer color = col6out},
  ellip/.append style  = {ellipse, inner color = col5in,
                          outer color = col5out},
  orect/.append style  = {rect, font = sffamilybfseriesLARGE,
                         text width = 325pt, text centered,
                         minimum height = 10pt, outer color = col7out,
                         inner color=col7in},
  oellip/.append style = {ellip, inner color = col8in, outer color = col8out,
                          font = sffamilybfserieslarge, text centered}}
begin{forest}
  for tree={
      font=sffamilybfseries,
      line width=1pt,
      draw=linecol,
      ellip,
      align=center,
      child anchor=north,
      parent anchor=south,
      drop shadow,
      l sep+=12.5pt,
      edge path={
        noexpandpath[color=linecol, rounded corners=5pt,
          >={Stealth[length=10pt]}, line width=1pt, ->, forestoption{edge}]
          (!u.parent anchor) -- +(0,-5pt) -|
          (.child anchor)forestoption{edge label};
        },
      where level={3}{tier=tier3}{},
      where level={0}{l sep-=15pt}{},
      where level={1}{
        if n={1}{
          edge path={
            noexpandpath[color=linecol, rounded corners=5pt,
              >={Stealth[length=10pt]}, line width=1pt, ->,
              forestoption{edge}]
              (!u.west) -| (.child anchor)forestoption{edge label};
            },
        }{
          edge path={
            noexpandpath[color=linecol, rounded corners=5pt,
              >={Stealth[length=10pt]}, line width=1pt, ->,
              forestoption{edge}]
              (!u.east) -| (.child anchor)forestoption{edge label};
            },
        }
      }{},
  }
  [Title, inner color=col1in, outer color=col1out
    [X, inner color=col2in, outer color=col2out
      [X1, inner color=col4in, outer color=col4out
          [X11, rect, name=X11
          ]
          [X12, rect, name=X12
          ]
          [X13, rect, name=X13
          ]
          [X14, rect, name=X14
          ]
          [X15, rect, name=X15
          ]
          [X16, rect, name=X16
          ]
          [X17, rect, name=X17
          ]
          [X18, rect, name=X18
          ]
      ]
      [X2, inner color=col4in, outer color=col4out
      ]
      [X3, inner color=col4in, outer color=col4out
      ]
    ]
    [Y, inner color=col3in, outer color=col3out
      [Convex Relaxation
        [Sparse SignalEstimate, rect, name=sse1
        ]
      ]
      [Greedy Pursuits
        [Sparse SignalEstimate, rect, name=sse2
        ]
      ]
      [, phantom, calign with current
        [AB, phantom
          [Our Work, orect, name=us
            [{Improved Sparse Signal Estimate!}, oellip
            ]
          ]
        ]
      ]
      [Non-ConvexMinimisation Methods
        [Sparse SignalEstimate, rect, name=sse3
        ]
      ]
      [CombinatorialAlgorithms
        [Sparse SignalEstimate, rect, name=sse4
        ]
      ]
    ]
  ]
  begin{scope}[color = linecol, rounded corners = 5pt,
    >={Stealth[length=10pt]}, line width=1pt, ->]
    draw (sse2.south) -- (us.north -| sse2.south);
    draw (sse3.south) -- (us.north -| sse3.south);
    coordinate (c1) at ($(sse1.south)!2/5!(sse2.south)$);
    coordinate (c2) at ($(sse3.south)!2/5!(sse4.south)$);
    draw (sse1.south) -- +(0,-10pt) -| (us.north -| c1);
    draw (sse4.south) -- +(0,-10pt) -| (us.north -| c2);
  end{scope}
end{forest}
end{document}

The results can be seen below:
Resulting diagram

What I would like to have are arrows between the nodes X1, X2 and X3 and the nodes X11-X18 in order to avoid having to concatenate nodes X11-X18 three times (once for each higher level node). In short, I’m looking for a way to draw the red arrows in the diagram below:
Wanted diagram

One Answer

If you name the two nodes, you can just add draw commands at the end. The following also updates the code a little, though only a little.

documentclass[tikz,border=10pt]{standalone}
usepackage{forest}
usetikzlibrary{arrows.meta, shapes.geometric, calc, shadows}

colorlet{mygreen}{green!75!black}
colorlet{col1in}{red!30}
colorlet{col1out}{red!40}
colorlet{col2in}{mygreen!40}
colorlet{col2out}{mygreen!50}
colorlet{col3in}{blue!30}
colorlet{col3out}{blue!40}
colorlet{col4in}{mygreen!20}
colorlet{col4out}{mygreen!30}
colorlet{col5in}{blue!10}
colorlet{col5out}{blue!20}
colorlet{col6in}{blue!20}
colorlet{col6out}{blue!30}
colorlet{col7out}{orange}
colorlet{col7in}{orange!50}
colorlet{col8out}{orange!40}
colorlet{col8in}{orange!20}
colorlet{linecol}{blue!60}

begin{document}
pgfkeys{/forest,
  rect/.append style   = {rectangle, rounded corners = 2pt,
                         inner color = col6in, outer color = col6out},
  ellip/.append style  = {ellipse, inner color = col5in,
                          outer color = col5out},
  orect/.append style  = {rect, font = sffamilybfseriesLARGE,
                         text width = 325pt, text centered,
                         minimum height = 10pt, outer color = col7out,
                         inner color=col7in},
  oellip/.append style = {ellip, inner color = col8in, outer color = col8out,
                          font = sffamilybfserieslarge, text centered}}
begin{forest}
  for tree={
      font=sffamilybfseries,
      line width=1pt,
      draw=linecol,
      ellip,
      align=center,
      child anchor=north,
      parent anchor=south,
      drop shadow,
      l sep'+=12.5pt,
      edge+={color=linecol, rounded corners=5pt, >={Stealth[length=10pt]}, line width=1pt, ->},
      edge path'={(!u.parent anchor) -- +(0,-5pt) -| (.child anchor)},
      where level={3}{tier=tier3}{},
      where level={0}{l sep'-=15pt}{},
      where level={1}{
        if n={1}{
          edge path'={(!u.west) -| (.child anchor)},
        }{
          edge path'={(!u.east) -| (.child anchor)},
        }
      }{},
  }
  [Title, inner color=col1in, outer color=col1out
    [X, inner color=col2in, outer color=col2out
      [X1, inner color=col4in, outer color=col4out
          [X11, rect, name=X11
          ]
          [X12, rect, name=X12
          ]
          [X13, rect, name=X13
          ]
          [X14, rect, name=X14
          ]
          [X15, rect, name=X15
          ]
          [X16, rect, name=X16
          ]
          [X17, rect, name=X17
          ]
          [X18, rect, name=X18
          ]
      ]
      [X2, inner color=col4in, outer color=col4out, name=x2
      ]
      [X3, inner color=col4in, outer color=col4out, name=x3
      ]
    ]
    [Y, inner color=col3in, outer color=col3out
      [Convex Relaxation
        [Sparse SignalEstimate, rect, name=sse1
        ]
      ]
      [Greedy Pursuits
        [Sparse SignalEstimate, rect, name=sse2
        ]
      ]
      [, phantom, calign with current
        [AB, phantom
          [Our Work, orect, name=us
            [{Improved Sparse Signal Estimate!}, oellip
            ]
          ]
        ]
      ]
      [Non-ConvexMinimisation Methods
        [Sparse SignalEstimate, rect, name=sse3
        ]
      ]
      [CombinatorialAlgorithms
        [Sparse SignalEstimate, rect, name=sse4
        ]
      ]
    ]
  ]
  begin{scope}[color = linecol, rounded corners = 5pt, >={Stealth[length=10pt]}, line width=1pt, ->]
    draw (sse2.south) -- (us.north -| sse2.south);
    draw (sse3.south) -- (us.north -| sse3.south);
    coordinate (c1) at ($(sse1.south)!2/5!(sse2.south)$);
    coordinate (c2) at ($(sse3.south)!2/5!(sse4.south)$);
    draw (sse1.south) -- +(0,-10pt) -| (us.north -| c1);
    draw (sse4.south) -- +(0,-10pt) -| (us.north -| c2);
    draw (x2.parent anchor) -- +(0,-5pt) -| (X11.child anchor);
    draw (x2.parent anchor) -- +(0,-5pt) -| (X18.child anchor);
    draw (x3.parent anchor) -- +(0,-5pt) -| (X11.child anchor);
    draw (x3.parent anchor) -- +(0,-5pt) -| (X18.child anchor);
  end{scope}
end{forest}
end{document}

additional edges

Correct answer by cfr on February 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP