5

Following this answer, I don't understand why the parent node is aligned with the west of its child instead of center despite setting

parent anchor=south,
child anchor=north,
calign=center,

my question is how to rpoperly make each parent horizontally centered between its children?

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows.blur}
\begin{document}
\newlength\gap
\setlength\gap{10mm}
\forestset{parent color/.style args={#1}{
    {fill=#1},
    for tree={fill/.wrap pgfmath arg={#1!##1}{1/level()*80},draw=#1!80!darkgray}},
    root color/.style args={#1}{fill={{#1!60!gray!25},draw=#1!80!darkgray}}
}
\begin{forest}
  forked edges,
  for tree={%
    rounded corners,
    minimum width=(\textwidth-6*\gap)/3,
    minimum height=4ex,
    edge={-Latex},
    font=\sffamily,
    text centered,
    blur shadow,
    parent anchor=south,
    child anchor=north,
    calign=center
  },
  where={level()<=1}{%
    l sep+=10pt,
    s sep'+=10pt,
   }{%
     folder,
     grow'=0,
     l sep+=2pt,% length of edge to grand child
     if level=2{%
       !u.s sep'+=10pt,
       edge path'={%
         (!u.parent anchor) -- (.child anchor)
       },
     }{},
   },
      [Grandparent,root color={brown}
        [Parent 1, parent color={red}
            [Child 1
                [Grandchild 1]
            ]
        ]
        [Parent 2,parent color={green}
            [Child 2
                [Grandchild 2]
            ]
        ]
        [Parent 3,parent color={yellow}
            [Child 3
                [Grandchild 3]
            ]
        ]
    ]
\end{forest}
\end{document}

enter image description here

4

Note that you did not ask how to do anything. You asked for an explanation of the alignment. Sašo's answer addressed that question.

I take it from your comment that what you meant to ask in your question body was what you actually ask in the subject line i.e. how to avoid the effect described in your question.

It would probably be simpler at this point to start over. However, hacking on Alan Munn's hacking on my code, you could do the following.

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows.blur}
\begin{document}
\newlength\gap
\setlength\gap{10mm}
\forestset{parent color/.style args={#1}{
    {fill=#1},
    for tree={fill/.wrap pgfmath arg={#1!##1}{1/level()*80},draw=#1!80!darkgray}},
    root color/.style args={#1}{fill={{#1!60!gray!25},draw=#1!80!darkgray}}
}
\begin{forest}
  forked edges,
  for tree={%
    rounded corners,
    minimum width=(\textwidth-6*\gap)/3,
    minimum height=4ex,
    edge={-Latex},
    font=\sffamily,
    text centered,
    blur shadow,
    parent anchor=children,
    child anchor=parent,
  },
  where={level()<=1}{%
    l sep+=10pt,
    s sep'+=10pt,
   }{%
     folder,
     grow'=0,
     l sep+=2pt,% length of edge to grand child - NO IT IS NOT!!!
     if level=2{%
       !u.s sep'+=10pt,
       anchor=north,
       child anchor=north,
       edge path'={%
         (!u.parent anchor) -- (.child anchor)
       },
     }{
       if level=3{
         before computing xy={l=14pt-((\textwidth-6*\gap)/6)},
       }{}
     },
   },
      [Grandparent,root color={brown}
        [Parent 1, parent color={red}
            [Child 1
                [Grandchild 1]
            ]
        ]
        [Parent 2,parent color={green}
            [Child 2
                [Grandchild 2]
            ]
        ]
        [Parent 3,parent color={yellow}
            [Child 3
                [Grandchild 3 [Great-Grandchild]]
            ]
        ]
    ]
\end{forest}
\end{document}

centred children at level 2

Alternatively, adapting my version of the code from family tree and branch shade to create family tree too and branch shading, we might write the following.

\documentclass[border=10pt,multi,tikz,dvipsnames,svgnames,rgb]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows.blur}
\forestset{
  declare dimen register=gap,
  gap'=10mm,
  declare count register=twist,
  twist'=2,
  family tree too/.style={
    forked edges,
    for tree={
      rounded corners,
      minimum width/.wrap pgfmath arg={##1}{(\textwidth-6*(gap))/3},
      minimum height=4ex,
      edge={-Latex},
      font=\sffamily,
      text centered,
      blur shadow,
      edge=thick,
    },
    where={level()<(twist)}{%
      parent anchor=children,
      l sep+=5pt,
    }{%
      folder,
      grow'=0,
      if={level()==(twist)}{%
        before typesetting nodes={child anchor=north},
        anchor=north,
        edge path'={%
          (!u.parent anchor) -- (.child anchor)
        },
      }{
        l sep'+=2pt,
        if level=3{
          before computing xy={
            l/.wrap pgfmath arg={##1}{14pt-((\textwidth-6*(gap))/6)}
          },
        }{}
      },
    },
  },
  branch shading/.style args={from #1 to #2}{
    before typesetting nodes={
      tempcountc/.max={level}{current,tree},
      tempcountb/.option=level,
      tempcounta=(tempcountc)-(tempcountb)+1,
      temptoksa/.option=name,
      TeX/.wrap pgfmath arg={
        \colorlet{##1col1}{#1}
        \colorlet{##1col2}{#2}
      }{name()},
      for tree={
        rounded corners,
        top color/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb))/(tempcounta))}{(temptoksa)},
        bottom color/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb)+1)/(tempcounta))}{(temptoksa)},
        draw/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb)+1)/(tempcounta))}{(temptoksa)},
        thick,
      },
      +edge=darkgray,
      for descendants={
        +edge/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb))/(tempcounta))}{(temptoksa)},
      },
    }
  },
}
\begin{document}
\begin{forest}
  family tree too,
   [Grandparent, left color=cyan, right color=SpringGreen, middle color=Pink, draw=Silver
     [Parent 1, branch shading=from cyan to blue
       [Child 1
         [Grandchild 1]
       ]
     ]
     [Parent 2, branch shading=from Pink to WildStrawberry
       [Child 2
         [Grandchild 2]
       ]
     ]
     [Parent 3, branch shading=from SpringGreen to ForestGreen
       [Child 3
         [Grandchild 3]
       ]
     ]
   ]
\end{forest}
\begin{forest}
  family tree too,
   [Grandparent, fill=darkgray, text=Silver, double=Silver, draw=darkgray
     [Parent 1, branch shading=from blue!80 to blue!20
       [Child 1
         [Grandchild 1]
       ]
     ]
     [Parent 2, branch shading=from WildStrawberry!80 to WildStrawberry!20
       [Child 2
         [Grandchild 2]
       ]
     ]
     [Parent 3, branch shading=from ForestGreen!80 to ForestGreen!20
       [Child 3
         [Grandchild 3]
       ]
     ]
   ]
\end{forest}
\end{document}

other possibilities

  • I wrote the question in the title, but forgot to write it again in the body. Anyway, your answer solved my problem, but if you are happy to let me understand something else I would be thankful. 1- How can the junction between Grandparent and Parent 2 be centered between both of them? 2- I thought from the manual that applying options inside for tree will affect globally, so why did my three lines parent anchor=south, child anchor=north, calign=center have no impact on the parents alignment? – Diaa Oct 30 '16 at 6:03
  • @DiaaAbidou (1) It is centred horizontally. Do you mean vertically? You can change the distance between the parent and the forking point, is that what you mean? You can't easily centre it vertically that I know of. (2) It isn't that it has no impact. It is rather that other things also have an impact. Setting folder, for example, and setting forked edges. Moreover, folder` changes both l and s after packing nodes. If you want more detail, see the manual or ask a follow-up question. – cfr Oct 30 '16 at 16:40
2

Style folder sets calign, among other things.

Also note that at a folder node, the tree actually grows to the east!

  • I am sorry, but I don't quite understand what you mean. Could you please edit my code to show me how it can be done? – Diaa Oct 29 '16 at 15:56

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.