3

This MWE code is adapted from this example on www.texample.net and was originally written by SE user cfr:

\documentclass{article}

\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, shapes.geometric, calc, shadows}
\usepackage{forest}
\usepackage{adjustbox}

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

\tikzset{
  basic/.style  = {draw, drop shadow, rectangle},
  root/.style   = {basic, rounded corners=2pt, thin, align=center,
                   fill=green!30},
  level 2/.style = {basic, rounded corners=6pt, thin,align=center, fill=blue!10},
  level 3/.style = {basic, thin, align=left, fill=green!10}
}

\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 = \sffamily\bfseries\LARGE,
                         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 = \sffamily\bfseries\large, text centered}}

\begin{figure}[h]
\begin{adjustbox}{max width=1.2\textwidth,center}    
    \begin{forest}
      for tree={
          font=\sffamily\bfseries,
          line width=1pt,
          draw=linecol,
          ellip,
          align=center,
          child anchor=north,
          parent anchor=south,
          drop shadow,
          l sep+=12.5pt,
          edge path={
            \noexpand\path[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={
                \noexpand\path[color=linecol, rounded corners=5pt,
                  >={Stealth[length=10pt]}, line width=1pt, ->,
                  \forestoption{edge}]
                  (!u.west) -| (.child anchor)\forestoption{edge label};
                },
            }{
              edge path={
                \noexpand\path[color=linecol, rounded corners=5pt,
                  >={Stealth[length=10pt]}, line width=1pt, ->,
                  \forestoption{edge}]
                  (!u.east) -| (.child anchor)\forestoption{edge label};
                },
            }
          }{},
      }
      [\Huge Total drag, yshift = 3cm, inner color=col1in, outer color=col1out
        [\huge Viscous drag, yshift = 1cm, inner color=col2in, outer color=col2out
          [\Large Displacement separation, inner color=col4in, outer color=col4out
          ]
          [\Large Boundary layer shear, inner color=col4in, outer color=col4out 
          ]
        ]
        [\huge Vortex drag, yshift = 1cm, inner color=col3in, outer color=col3out
          [\Large Trailing vortices due to lift
          ]
        ]
        [\huge Wave drag, yshift = 1cm, inner color=col3in, outer color=col3out
              [\Large Waves due to lift
              ]
              [\Large Waves due to volume
              ]
        ]  
        ]
       ]
    \end{forest}
    \end{adjustbox}
    \end{figure}
    \end{document}

results in this:

enter image description here

Note that the lines coming from the upper level parent node labeled 'Total drag' do not start from the prescribed 'South' anchor location, as prescribed in the lines:

for tree={
      ...
      parent anchor=south,
      ...

I would appreciate any help with this.

  • The value of parent anchor (which is set to south) is actually not used to draw the offending edge, as that edge is drawn using the third occurrence of edge path. – Sašo Živanović Sep 11 '15 at 10:55
  • Remarks. (i) next time, a working minimal example would be appreciated. (ii) Using where level inside for tree results in a double loop! You should use if level. (Well, in your case, the result is the same, but the if version is faster.) – Sašo Živanović Sep 11 '15 at 10:57
  • 1
    Hi, sorry I thought the entire figure code would be enough for a MWE but I realise now that the point behind it is that people must be able to simply copy and paste the code and it should run on their system. i updated the question. Can you perhaps indicate what I should add to my code under the 3rd instance of edge path? I tried a few things but don't get it to work. – Jonny Sep 11 '15 at 11:27
  • 1
    Replace (!u.east) by (!u.parent anchor) -- +(0,-5pt), as in the first occurrence of edge path. And I really urge you to clean up the code! Maybe define a style for your fancy edges ... – Sašo Živanović Sep 11 '15 at 11:54
2

Your code works as expected. edges between first node and children is defined with

     where level={1}{
        if n={1}{
          edge path=...
        }{
          edge path=...
        }
      }{},

Therefore, for all children a new parent anchor is defined. This is west for child 1 and east for all other children except 1.

If you want to distinguish between child 2 and 3, you could introduce another if level.

\documentclass{article}

\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, shapes.geometric, calc, shadows}
\usepackage{forest}
\usepackage{adjustbox}

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

\tikzset{
  basic/.style  = {draw, drop shadow, rectangle},
  root/.style   = {basic, rounded corners=2pt, thin, align=center,
                   fill=green!30},
  level 2/.style = {basic, rounded corners=6pt, thin,align=center, fill=blue!10},
  level 3/.style = {basic, thin, align=left, fill=green!10}
}

\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 = \sffamily\bfseries\LARGE,
                         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 = \sffamily\bfseries\large, text centered}}

\begin{figure}[h]
\begin{adjustbox}{max width=1.2\textwidth,center}    
    \begin{forest}
      for tree={
          font=\sffamily\bfseries,
          line width=1pt,
          draw=linecol,
          ellip,
          align=center,
          child anchor=north,
          parent anchor=south,
          drop shadow,
          l sep+=12.5pt,
          edge path={
            \noexpand\path[color=linecol, rounded corners=5pt,
              >={Stealth[length=10pt]}, line width=1pt, ->, \forestoption{edge}]
              (!u.parent anchor) -- +(0,-5pt) -|
              (.child anchor)\forestoption{edge label};
            },
          if level={3}{tier=tier3}{},
          if level={0}{l sep-=15pt}{},
          if level={1}{
            if n={1}{
              edge path={
                \noexpand\path[color=linecol, rounded corners=5pt,
                  >={Stealth[length=10pt]}, line width=1pt, ->,
                  \forestoption{edge}]
                  (!u.west) -| (.child anchor)\forestoption{edge label};
                },
            }{
              if n={3}{
                edge path={
                    \noexpand\path[color=linecol, rounded corners=5pt,
                    >={Stealth[length=10pt]}, line width=1pt, ->,
                    \forestoption{edge}]
                    (!u.east) -| (.child anchor)\forestoption{edge label};
                    },
                }{}}
          }{},
      }
      [\Huge Total drag, yshift = 3cm, inner color=col1in, outer color=col1out
        [\huge Viscous drag, yshift = 1cm, inner color=col2in, outer color=col2out
          [\Large Displacement separation, inner color=col4in, outer color=col4out
          ]
          [\Large Boundary layer shear, inner color=col4in, outer color=col4out 
          ]
        ]
        [\huge Vortex drag, yshift = 1cm, inner color=col3in, outer color=col3out
          [\Large Trailing vortices due to lift
          ]
        ]
        [\huge Wave drag, yshift = 1cm, inner color=col3in, outer color=col3out
              [\Large Waves due to lift
              ]
              [\Large Waves due to volume
              ]
        ]  
        ]
       ]
    \end{forest}
    \end{adjustbox}
    \end{figure}
    \end{document}

enter image description here

3

Ignasi's answer is good. This is just a supplement. If you also want to get rid of the kink in the line, you can use the else... part of the final if... to align the child with the parent:

  if level=1{
    if n=1{
      edge path={
        \noexpand\path[\forestoption{edge}] (!u.west) -| (.child anchor)\forestoption{edge label};
        },
    }{
      if n'=1{
        edge path={
          \noexpand\path[\forestoption{edge}] (!u.east) -| (.child anchor)\forestoption{edge label};
        },
      }{calign with current}
    }
  }{},

less kinky line

Complete code:

\documentclass[tikz,border=10pt]{standalone}

\usetikzlibrary{arrows.meta, shadows}
\usepackage{forest}

\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{linecol}{blue!60}

\begin{document}

\tikzset{
  ellip/.append style  = {ellipse, col in out = 5},
  col in out/.style = {inner color = col#1in, outer color = col#1out},
  font sizing/.style={font=\sffamily\bfseries#1},
  Huge/.style={font sizing=\Huge},
  huge/.style={font sizing=\huge},
  Large/.style={font sizing=\Large},
}

\begin{forest}
  for tree={
      font=\sffamily\bfseries,
      line width=1pt,
      draw=linecol,
      ellip,
      align=center,
      child anchor=north,
      parent anchor=south,
      drop shadow,
      l sep+=10mm,
      edge={color=linecol, rounded corners=5pt, >={Stealth[length=10pt]}, line width=1pt, ->},
      edge path={
        \noexpand\path[\forestoption{edge}] (!u.parent anchor) -- +(0,-5pt) -| (.child anchor)\forestoption{edge label};
        },
      if level=1{
        huge,
        if n=1{
          col in out=2,
          for children={col in out=4},
          edge path={
            \noexpand\path[\forestoption{edge}] (!u.west) -| (.child anchor)\forestoption{edge label};
            },
        }{
          col in out=3,
          if n'=1{
            edge path={
              \noexpand\path[\forestoption{edge}] (!u.east) -| (.child anchor)\forestoption{edge label};
            },
          }{calign with current}
        }
      }{},
      if level=2{Large}{}
  }
  [Total drag, Huge, col in out=1
    [Viscous drag
      [Displacement separation
      ]
      [Boundary layer shear
      ]
    ]
    [Vortex drag
      [Trailing vortices due to lift
      ]
    ]
    [Wave drag
          [Waves due to lift
          ]
          [Waves due to volume
          ]
    ]
   ]
\end{forest}
\end{document}

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.