1
\documentclass{article}

\usepackage[margin=1in]{geometry}
\usepackage{tikz}
\usetikzlibrary{trees}

\newcommand{\somevar}{center}

\tikzset{
    headset/.style={
        align=\somevar
    }
}

\tikzset{
    ideaone/.style={
        grow via three points={one child at (1.0,-1.5) and two children at (1.0,-1.5) and (1.0,-5.0)}, %is a property of children
        edge from parent path={(\tikzparentnode\tikzparentanchor) |- (\tikzchildnode\tikzchildanchor)},
        growth parent anchor = west,
        parent anchor = west,
        anchor = west
    }
}

\tikzset{
    ideatwo/.style={
        grow via three points={one child at (1.0,0.0) and two children at (1.0,0.0) and (1.0,-2.0)}, %this is working perfectly fine
        growth parent anchor = east,
        edge from parent path={(\tikzparentnode\tikzparentanchor) -- (\tikzchildnode\tikzparentanchor)}, %***this should only affect the children, why is it affecting everything?***
        %parent anchor = east, %***multiple anchorings are not allowed? Overwritten?***
        anchor = west
    }
}

\begin{document}
    
    \begin{tikzpicture}
        [
        headset
        %global and levelwise style
        ]
        
        \node {\underline{\Large {Main Head?}}}
        [ideaone]
            child
            {
                node {Idea 1 \\ center??} %drawn relative to "child" cs
                    [ideatwo]
                    child 
                    {
                        node {Idea 1-1} 
                    }
                    child 
                    {
                        node {Idea 1-2} 
                    }
                    child 
                    {
                        node {Idea 1-3}
                    }
            }
            child
            {
                node {Idea 2}
                    %[ideatwo]
            }
        ;
        
        
    \end{tikzpicture}
    
    \renewcommand{\somevar}{left}
    \newpage

    
\end{document}

I am trying to make a convenient class file for myself that I can use to create a similar type of tree each time. However, I am just not able to achieve the desirable results. I am trying to figure out the scope of each command. But it feels like, it is command dependent.I tried to follow this page instruction for the scope https://tikz.dev/tikz-trees. It says the following:

\begin{tikzpicture}
  \scoped
    [...]              % Options apply to the whole tree
    \node[...] {root}  % Options apply to the root node only
       [...]           % Options apply to all children
       child[...]      % Options apply to this child and all its children
       {
         node[...] {}  % Options apply to the child node only
         ...
       }
       child[...]      % Options apply to this child and all its children
    ;
\end{tikzpicture}

Based on this, I would assume that my tree would output something like this:

Good Tree

But I am getting the following instead: The bad tree

I don't exactly understand why is this happening or if the scopes of function are command dependent. Even the defining of anchors is not working properly as it should. Lastly I just wanna mention that I can't use levelx styles cause I might have differences in siblings which I plan to accommodate using renewcommand.

In short,
Q1) Why isn't the edge from parent path working as I am thinking it should?
Q2) Why is redefining parent anchor in ideatwo/.style messing things up even more?

3
  • 1
    Are you set on the child syntax of TikZ? Newer approaches exist with the forest package, the graphs and the graphdrawing libraries. Aug 3 at 9:06
  • Thank. I am open to looking at other libraries too. I only found tree to be the first one suitable for my needs. But I don't mind changing syntax for same output.
    – Someone
    Aug 3 at 16:59
  • If you're drawing a number of trees like this, you might consider using forest.
    – cfr
    Aug 5 at 17:08

1 Answer 1

2

Your ideatwo edge from parent path contains \tikzparentanchor twice:

edge from parent path={(\tikzparentnode\tikzparentanchor) -- (\tikzchildnode\tikzparentanchor)}

Otherwise, this seems to be a scoping issue.

All the edge settings moved to the every child style (the edges are part of the children) solves the other question.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{trees}
\newcommand{\somevar}{center}
\tikzset{
  headset/.style={align=\somevar, child anchor=west},
  edge from parent line to/.style={
    edge from parent path={(\tikzparentnode\tikzparentanchor) -- (\tikzchildnode\tikzchildanchor)}},
  edge from parent vh/.style={
    edge from parent path={(\tikzparentnode\tikzparentanchor) |- (\tikzchildnode\tikzchildanchor)}},
  ideaone/.style={
    grow via three points={one child at (1.0,-1.5) and two children at (1.0,-1.5) and (1.0,-5.0)},
    every child/.append style={
      edge from parent vh, parent anchor = west, child anchor=west},
    growth parent anchor = west,
    anchor = west},
  ideatwo/.style={
    grow via three points={one child at (1.0,0.0) and two children at (1.0,0.0) and (1.0,-2.0)},
    every child/.append style={
      edge from parent line to, parent anchor = east, child anchor = west},
    growth parent anchor = east,
    anchor = west}}
\begin{document}
\tikz[headset]
\node {\underline{\Large {Main Head?}}}
  [ideaone]
  child {
    node {Idea 1 \\ center??}
      [ideatwo]
      child {node {Idea 1-1}}
      child {node {Idea 1-2}}
      child {node {Idea 1-3}}
  }
  child {node {Idea 2}};
\end{document}

Output

enter image description here

2
  • Thank you! This worked perfectly. I still have maybe a conceptual issue of why I had to put things inside every child?
    – Someone
    Aug 3 at 17:04
  • 1
    @Someone Because the edge from parent is part of the child, if you define it before the child it will also be applied to edges on that level. I guess … Aug 3 at 21:53

You must log in to answer this question.

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